2015-09-28 130 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添加標記。