【问题标题】:Hide a gridView row`s item in asp.net在 asp.net 中隐藏 gridView 行的项目
【发布时间】:2018-01-10 08:58:20
【问题描述】:

我试图用指定的参数在 gridview 中隐藏几个行的项目。如果该行中的标签“ProductDetail”具有数组中的任何值,它应该隐藏“btnDuplicateRow”按钮

protected void grdEstimateDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{           
    String[] arr = new String[6];
    arr[0] = "Malarkey";                

    for (int i = 0; i < grdEstimateDetails.Rows.Count; i++)
    {
        Label lblctrl = (Label)grdEstimateDetails.Rows[i].FindControl("ProductDetail");

        var btnDuplicateRow1 = (ImageButton)grdEstimateDetails.Rows[i].Cells[1].FindControl("btnDuplicateRow");

        if (arr.Contains(lblctrl.Text))
        {
             btnDuplicateRow1.Visible = false;
        }               
    }
}

<ItemTemplate>
    <asp:Label ID="lblProductDetail" runat="server" Text='<%# Eval("ProductDetail") %>'></asp:Label>
</ItemTemplate>

<asp:ImageButton runat="server" ID="btnDuplicateRow" ImageUrl="~/Web/Images/add-18x18.png" 
    CommandName="DuplicateRow" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" 
    ToolTip="Duplicate"/>

现在我得到的对象引用未设置为对象的实例。错误。

我尝试的另一件事是隐藏行

e.Row.Visible = false;

但还是不行。

谢谢。

已编辑:
网格视图:


            <asp:TemplateField HeaderText="Product/Detail" >
                <ItemTemplate>
                    <asp:Label ID="lblProductDetail" runat="server" Text='<%# Eval("ProductDetail") %>'></asp:Label>
                                            <asp:Label ID="Label1" runat="server" Text='Friday'></asp:Label>

                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField Visible="true">
                <ItemTemplate>
                    <table cellpadding="0" cellspacing="0">
                        <tr>


                            <td>
                                <asp:ImageButton runat="server" ID="btnDuplicateRow" ImageUrl="~/Web/Images/add-18x18.png" 
                                    CommandName="DuplicateRow" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" 
                                    ToolTip="Duplicate"/>
                            </td>

                        </tr>
                    </table>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
        <SelectedRowStyle BackColor="Khaki"></SelectedRowStyle>
    </asp:GridView>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我认为这应该可行。可能需要调整才能找到按钮控件

       protected void grdEstimateDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {   
            String[] arr = new String[6];
            arr[0] = "Malarkey";
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
               // Following line may not be necessary but could prove to be usefull 
               //System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
               e.Row.Cells[0].Style["display"] = "none";
               Label lblctrl = (Label)e.Row.FindControl("lblProductDetail");
               ImageButton btnDuplicateRow1 = (ImageButton)e.Row.FindControl("btnDuplicateRow");
                if (arr.Contains(lblctrl.Text))
                {
                   btnDuplicateRow1.Visible = false;
                }
           }
        }
    

    ......

    【讨论】:

    • 我收到此错误:错误 CS1061:'System.Web.UI.WebControls.GridViewRowEventArgs' 不包含 'Item' 的定义,并且没有扩展方法 'Item' 接受类型的第一个参数'可以找到 System.Web.UI.WebControls.GridViewRowEventArgs'(您是否缺少 using 指令或程序集引用?)
    • 对不起,@Fresher,我的错。我更正为“e.Row”应该可以正常工作。我从几个来源拼凑代码,所以忽略了这一点。
    • 我之前试过这个,但我仍然得到对象引用未设置为对象的实例。错误..
    • 哪个控件给出了这个错误?两者都看起来像您使用模板化的 Column 吗?您可以发布整个 Grid 或至少包含控件的列吗?
    • 我发现了一个错误,可以解决您尝试使用数据字段“ProductDetail”定位标签控件“lblProductDetail”的问题,我修改了我的代码,以便它正在寻找标签。您还可以直接从数据字段评估值。如果您仍然遇到错误,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2010-09-17
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 2023-03-28
    • 2016-05-27
    相关资源
    最近更新 更多