-3

hear i am post the my grid view.

                <Columns>
                    <asp:TemplateField HeaderText="Item">
                            <ItemTemplate>
                                <p> <asp:Label ID="lblproductname"  display="Dynamic" runat="server" Text='<%# Bind("productname") %>'></asp:Label></p>
                                <p><asp:Label ID="lblProductWeight" display="Dynamic" runat="server" Text='<%# Bind("groupvalue") %>'></asp:Label></p> 
                                 <p> <asp:Label ID="lblProductType" display="Dynamic" runat="server" Text='<%# Bind("groupname") %>'></asp:Label></p> 
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" />
                            <ItemStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                    <asp:BoundField DataField="Quantity" HeaderText="Qty">
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Price" HeaderText="Price">
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="SubTotal" HeaderText="Sub Total">
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:BoundField>
                </Columns>
                <FooterStyle CssClass="datatable" />
            </asp:GridView>

so how to make the visible false ItemTemplate of lables programaticaly in xyz method

4

3 回答 3

3

在网格中的 Rowcommand 中,您可以隐藏或显示项目模板,例如:

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      //Your Condition
      Label lblproductname = (Label)e.Row.FindControl("lblproductname")
      If(a > B)
         lblproductname.Visible = true;
      //Others Lables
      ....

    }

  }

我希望有帮助。

于 2013-08-30T10:33:40.950 回答
0
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
         e.Row.Cells[columnIndex].Visible = false;
}

我可以建议的唯一解决方法是在 ASPX 页面上提供一个 HeaderText,然后使用它进行查找。

protected void xyz()
{
    ((DataControlField)youGridView.Columns
            .Cast<DataControlField>()
            .Where(fld => fld.HeaderText == "your header text")
            .SingleOrDefault()).Visible = false;
}
于 2013-08-30T10:34:38.157 回答
0

我相信你的问题在这里得到了回答:

如何在 GridView 的 TemplateField 中找到控件?

从答案复制:

foreach(GridViewRow row in GridView1.Rows) {
    if(row.RowType == DataControlRowType.DataRow) {
        Label myLabel = row.FindControl("myLabelID") as Label;
        myLabel.Visible = false;
    }
}

来自 RowDataBoundEvent:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        Label myLabel = e.Row.FindControl("myLabelID") as Label;
        myLabel.Visible = false;
    }
}
于 2013-08-30T10:52:17.520 回答