2015-09-28 87 views
2

这是我的html标记,并在div标记中将可见性添加到false隐藏实际数据本身,但仅留下空白列。我试图访问div标签(是我加入了runat="server"标签到HTML),并试图访问它像这样hideme.Visible = true;该扔的隐藏网格列

编译错误在目前情况下不存在。

我该修改/修改哪些内容以确保该列完全隐藏在我的网格中?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="true" 
     onrowdatabound="GridView1_RowDataBound" onrowcreated="GridView1_RowCreated"> 
    <Columns> 
     <asp:BoundField DataField="abcd" HeaderText="abcd" /> 
     <asp:BoundField DataField="def" HeaderText="def" /> 
     <asp:BoundField DataField="hij" HeaderText="hij" /> 
     <asp:BoundField DataField="xyz" HeaderText="xyz" /> 
     <asp:BoundField DataField="eee" HeaderText="eee" /> 
     <asp:BoundField DataField="era" HeaderText="era" /> 
     <asp:BoundField DataField="nai" HeaderText="nai" /> 
     <asp:BoundField DataField="fac" HeaderText="fac" /> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <div runat="server" style="visibility:hidden" id="hideme"> 
        <asp:Label ID="lbllunch" runat="server" Text='<%# Eval("hij") %>' /> 
        <asp:Label ID="lbllunchoverage" runat="server" Text='<%# Eval("xyz") %>' /> 
        <asp:Label ID="lbleee" runat="server" Text='<%# Eval("eee") %>' /> 
        <asp:Label ID="lblera" runat="server" Text='<%# Eval("era") %>' /> 
        <asp:Label ID="lblnai" runat="server" Text='<%# Eval("nai") %>' /> 
        <asp:Label ID="lblfac" runat="server" Text='<%# Eval("fac") %>' /> 
       </div> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView>  

编辑 我加入了.Visible命令到我的网页加载事件(在这里我总是躲在我的网页上的任何东西),像这样:

protected void Page_Load(object sender, EventArgs e) 
{ 
    hideme.Visible = false; 
    /More here 
} 
+1

你把hideme.Visible哪里= TRUE;码?你可以发布吗? –

+0

你真正应该学习的是隐藏内容的'visibility:hidden'与CSS之间的CSS差异,但是**保留了它的空间**和'display:none',它完全隐藏了这个元素。请注意,在服务器上设置“visible = false”将导致html元素甚至不会呈现给页面,因此您将失去与该对象的任何客户端javascript交互。然后您必须回发到服务器才能显示该项目 –

回答

1

由于hideme是在GridView的TemplateField内,你不能在Page_Load方法来访问它,但是你可以在GridView1_RowDataBound方法来访问它,如下

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // find the hideme div 
     HtmlGenericControl div = (HtmlGenericControl)e.Row.FindControl("hideme"); 

     // set the visible property 
     div.Visible = false; 
    } 
} 
0

可以隐藏在数据绑定事件的列网格视图。

protected void GridView_DataBound(object sender, GridViewRowEventArgs e) 
{  
    GridView.Columns[8].Visible = false;  

} 
0

如果要隐藏模板字段列,为什么要将其添加到标记。是的,如果您保留style="visibility:hidden"它将仅显示空白列,因为项目模板仍然以列的形式存在。

我的建议是如果不需要列dnt添加标记。