【问题标题】:How to hide a TemplateField column in a GridView如何在 GridView 中隐藏 TemplateField 列
【发布时间】:2011-06-24 16:50:50
【问题描述】:

如何在 GridView 中隐藏 TemplateField 列?

我尝试了以下方法:

<asp:TemplateField ShowHeader="False" Visible='<%# MyBoolProperty %>' >
<ItemTemplate>
    <asp:LinkButton ID="attachmentButton" runat="server" ... />
</ItemTemplate>

但它不起作用并给出以下错误:

只有具有 DataBinding 事件的对象才支持数据绑定表达式。 System.Web.UI.WebControls.TemplateField 没有 DataBinding 事件。

我也尝试以编程方式隐藏它,但似乎无法通过名称获取列,因为TemplateField 列没有名称。

【问题讨论】:

    标签: asp.net .net data-binding gridview exception-handling


    【解决方案1】:
    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
             e.Row.Cells[columnIndex].Visible = false;
    }
    


    如果您不喜欢硬编码索引,我可以建议的唯一解决方法是GridViewColumn 提供HeaderText,然后使用该HeaderText 查找列
    protected void UsersGrid_RowCreated(object sender, GridViewRowEventArgs e)
    {
        ((DataControlField)UsersGrid.Columns
                .Cast<DataControlField>()
                .Where(fld => fld.HeaderText == "Email")
                .SingleOrDefault()).Visible = false;
    }
    

    【讨论】:

    • 谢谢,但我想知道如何获得它的索引,我不喜欢硬编码的索引。
    • @naveen e.Row.Cells[columnIndex].Visible = false;完美隐藏列数据....如果我也想隐藏相应的标题模板怎么办??
    • @Gk_999:它也隐藏了标题模板
    • @naveen 使用 DataControlField 的解决方案在数据绑定后也可以工作,唯一的建议是以这种方式放置列的标题:
    • @A-Sharabiani:有一段时间了。我有时间会检查
    【解决方案2】:
    For Each dcfColumn As DataControlField In gvGridview.Columns
        If dcfColumn.HeaderText = "ColumnHeaderText" Then
            dcfColumn.Visible = false                    
        End If
    Next
    

    【讨论】:

      【解决方案3】:

      如果在我看来 Visible 设置为 false 的行将无法访问,它们是从 DOM 中删除而不是隐藏,所以我也使用了 Display: None 方法。就我而言,我想要一个包含行键的隐藏列。对我来说,这种声明式方法比其他一些使用代码的方法要干净一些。

      <style>
         .HiddenCol{display:none;}                
      </style>
      
      
       <%--ROW ID--%>
            <asp:TemplateField HeaderText="Row ID">
             <HeaderStyle CssClass="HiddenCol" />
             <ItemTemplate>
             <asp:Label ID="lblROW_ID" runat="server" Text='<%# Bind("ROW_ID") %>'></asp:Label>
             </ItemTemplate>
             <ItemStyle HorizontalAlign="Right" CssClass="HiddenCol" />
             <EditItemTemplate>
             <asp:TextBox ID="txtROW_ID" runat="server" Text='<%# Bind("ROW_ID") %>'></asp:TextBox>
             </EditItemTemplate>
             <FooterStyle CssClass="HiddenCol" />
            </asp:TemplateField>
      

      【讨论】:

        【解决方案4】:
        GridView1.Columns[columnIndex].Visible = false;
        

        【讨论】:

          【解决方案5】:

          试试这个

          .hiddencol
              {
                  display:none;
              }
              .viscol
              {
                  display:block;
              }
          

          在 GridView 的 RowCreated 事件上添加以下代码

          protected void OnRowCreated(object sender, GridViewRowEventArgs e)
          {
               if (e.Row.RowType == DataControlRowType.DataRow)
               {
                   e.Row.Cells[0].CssClass = "hiddencol";
               }
               else if (e.Row.RowType == DataControlRowType.Header)
               {
                   e.Row.Cells[0].CssClass = "hiddencol";
               }
          }
          

          【讨论】:

          • 如果您只关心屏幕上的可见性,这很好,但是如果您显示/隐藏私人信息(IE stackoverflow 实名),那么您不能只在客户端上隐藏它,它可以'根本不会发送给客户端
          【解决方案6】:

          我错过了什么吗?

          如果您无法在 TemplateField 上设置可见性,请在其内容上设置它

          <asp:TemplateField>
            <ItemTemplate>
              <asp:LinkButton Visible='<%# MyBoolProperty %>' ID="foo" runat="server" ... />
            </ItemTemplate>
          </asp:TemplateField> 
          

          或者如果您的内容很复杂,则将其包含在 div 中并在 div 上设置可见性

          <asp:TemplateField>
            <ItemTemplate>
              <div runat="server" visible='<%# MyBoolProperty  %>' >
                <asp:LinkButton ID="attachmentButton" runat="server" ... />
              </div>
            </ItemTemplate>
          </asp:TemplateField> 
          

          【讨论】:

          • 问题在于您不能将
            放置在模板字段之外。
          • 对我来说似乎很明显 div 在模板字段内。无论如何,我编辑使它比明显更明显
          【解决方案7】:

          这可以是另一种方法来验证空值

          DataControlField dataControlField = UsersGrid.Columns.Cast<DataControlField>().SingleOrDefault(x => x.HeaderText == "Email");
                      if (dataControlField != null)
                          dataControlField.Visible = false;
          

          【讨论】:

            【解决方案8】:

            使用列名的轻微改进,恕我直言:

                Private Sub GridView1_Init(sender As Object, e As System.EventArgs) Handles GridView1.Init
                For Each dcf As DataControlField In GridView1.Columns
                    Select Case dcf.HeaderText.ToUpper
                        Case "CBSELECT"
                            dcf.Visible = Me.CheckBoxVisible
                            dcf.HeaderText = "<small>Select</small>"
                    End Select
                Next
            End Sub
            

            这允许控制多列。 我最初使用“技术”列名,匹配其中的控件名称。 这使得在 ASCX 页面中很明显它是一个控制列。 然后根据需要换出名称以进行演示。 如果我在生产中发现奇怪的名字,我知道我跳过了一些东西。 “ToUpper”避免了大小写问题。

            最后,这会在任何帖子上运行一次,而不是在行创建期间捕获事件。

            【讨论】:

              【解决方案9】:
              protected void gvLogMessageDetail_RowDataBound(object sender, GridViewRowEventArgs e)  
                  { 
                    if (e.Row.RowType == DataControlRowType.Header)   
                      {  
                          if (rdlForImportOrExport.SelectedIndex == 1)  
                          {  
                              e.Row.Cells[3].Visible = false;  
                              e.Row.Cells[4].Visible = false;  
                              e.Row.Cells[5].Visible = false;  
                          }  
                          else  
                          {  
                              e.Row.Cells[3].Visible = true;  
                              e.Row.Cells[4].Visible = true;  
                              e.Row.Cells[5].Visible = true;  
                          }  
                      }    
                      if (e.Row.RowType == DataControlRowType.DataRow) //skip header row  
                      {  
                          try  
                          {  
                              if (rdlForImportOrExport.SelectedIndex == 1)  
                              {  
                                  e.Row.Cells[3].Visible = false;  
                                  e.Row.Cells[4].Visible = false;  
                                  e.Row.Cells[5].Visible = false;  
                              }  
                              else  
                              {  
                                  e.Row.Cells[3].Visible = true;  
                                  e.Row.Cells[4].Visible = true;  
                                  e.Row.Cells[5].Visible = true;  
                              }  
                              }  
                          catch  
                          {  
                              ClientScript.RegisterStartupScript(GetType(), "Expand", "<SCRIPT   LANGUAGE='javascript'>alert('There is binding problem in child grid.');</script>");  
                          }  
                      }  
                  }  
              

              【讨论】:

                猜你喜欢
                相关资源
                最近更新 更多
                热门标签