【问题标题】:how to make grid-view label visible false [closed]如何使网格视图标签可见错误[关闭]
【发布时间】:2026-01-30 02:40:01
【问题描述】:

听说我发布了我的网格视图。

                <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>

那么如何在 xyz 方法中以编程方式使标签的可见 false ItemTemplate

【问题讨论】:

  • 我不确定这是否正确,所以不想编辑问题,但我相信问题是:“如何更改 ItemTemplate 中的三个标签可见性?”

标签: c# asp.net


【解决方案1】:

在网格中的 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
      ....

    }

  }

希望对你有所帮助。

【讨论】:

    【解决方案2】:
    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;
    }
    

    【讨论】:

      【解决方案3】:

      我相信您的问题在这里得到了解答:

      How to find control in TemplateField of GridView?

      复制自答案:

      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;
          }
      }
      

      【讨论】: