2011-03-15 40 views
0

我有一個gridview,有一些列。我想隱藏一列,但在選擇記錄時仍然可以訪問它的值。如何隱藏列但仍然可以訪問它的值?

有人可以幫助我實現這一目標嗎?

任何幫助表示讚賞。

這是我的gridview:OutlookID是要隱藏的列! <asp:GridView ID="gvOutlookMeldingen" runat="server" AllowSorting="True" AutoGenerateColumns="False" AutoGenerateSelectButton="True" onselectedindexchanged="GridView_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="Melder" HeaderText="Melder" /> <asp:BoundField DataField="Onderwerp" HeaderText="Onderwerp" /> <asp:TemplateField HeaderText="Omschrijving"> <ItemTemplate> <div style="overflow:auto; width: 500px; height: 150px;"> <asp:Label ID="lblOmschrijving" runat="server" Text='<%# Bind("Omschrijving")%>'></asp:Label> </div> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Meldingsdatum" HeaderText="Meldingsdatum" /> <asp:BoundField DataField="OutlookID" HeaderText="OutlookID" Visible="false" /> </Columns> </asp:GridView>

這是代碼,當我選擇一個記錄:

Label lblOmschrijving = (Label)gvOutlookMeldingen.SelectedRow.FindControl("lblOmschrijving"); 
      //Label lblOutlookID = (Label)gvOutlookMeldingen.SelectedRow.FindControl("lblOutlookID"); 

      Response.Redirect("Detailscherm.aspx?" 
       + "melder=" + Server.UrlEncode(gvOutlookMeldingen.SelectedRow.Cells[1].Text) 
       + "&meldingsdatum=" + gvOutlookMeldingen.SelectedRow.Cells[4].Text 
       + "&onderwerp=" + Server.UrlEncode(gvOutlookMeldingen.SelectedRow.Cells[2].Text) 
       + "&outlookid=" + Server.UrlEncode(gvOutlookMeldingen.SelectedRow.Cells[5].Text) 
       + "&omschrijving=" + Server.UrlEncode(lblOmschrijving.Text) 
       + "&niv1=" + "" 
       + "&niv2=" + ""); 
+2

您可以使用TemplateColumn中,並希望項目顯示的設置CSS屬性:無; – eugeneK 2011-03-15 14:05:51

+2

在客戶端代碼或服務器端代碼中訪問其值?在什麼情況下? – David 2011-03-15 14:06:45

+0

@eugeneK這不起作用,因爲我仍然可以看到只有值消失的列。並且我無法獲得價值 – SamekaTV 2011-03-15 14:15:36

回答

4

你綁定數據後設置此代碼。爲了得到這個功能,我這樣做:

MyGridView.Columns[0].visible = true; 
MyGridView.DataBind(); 
MyGridView.Columns[0].visible = false; 

有了這個第一列是隱藏的,但你應該能夠訪問它的價值。

+0

我已經試過了。不起作用! – SamekaTV 2011-03-15 14:25:03

+0

它總是爲我工作。也許你應該發佈你的代碼,以便我們可以確切地看到你正在嘗試做什麼。 – 2011-03-15 14:27:01

+0

我編輯了我的問題。 – SamekaTV 2011-03-15 14:48:25

0

如果您不希望數據在客戶端可用,則必須設置服務器端Visible = "False"屬性,無論您使用的是哪種DataControlField(最好在標記中)。您仍然可以從服務器端訪問該列。

您可能想要考慮使用GridView的DataKeys屬性 - 它可能更適合您的需要。

+0

我已經做到了! – SamekaTV 2011-03-15 14:26:04

+0

您確定在回發期間行是否正確填充?在你提供的代碼中設置一個斷點,並觀看'gvOutlookMeldingen.Rows'和'gvOutlookMeldingen.SelectedRow'。 如果你正在做的是重定向到頁面並傳遞QueryString參數,爲什麼不讓Select列成爲[HyperLinkField](http://msdn.microsoft.com/zh-cn/library/system.web.ui .webcontrols.hyperlinkfield.aspx),DataNavigateUrlFields和DataNavigateUrlFormatString設置爲在QueryString中提供您需要的數據? – 2011-03-15 15:03:28

0

創建模板列而不是您的SELECT按鈕。設置

一項PostBackUrl = '<%# 的eval( 「SomePage.aspx頁面?ID = {0}」, 「想 柱」)%>'

。通過設計師刪除列。

0

您也可以在客戶端將其設置爲不可見。用Javascript。

document.getElementById(myObject).visible = "false"; 
0

當我們設置控制的可見性在設計時錯誤將被無法呈現。嘗試到 集的可見=假在GridView控件rowCreated事件。在下面的代碼我設置 第二列的知名度=假

保護無效grid_RowCreated(對象發件人,GridViewRowEventArgs E) { 如果(e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells [2]。可見= FALSE;

 } 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      e.Row.Cells[2].Visible = false; 

     } 
     if (e.Row.RowType == DataControlRowType.Footer) 
     { 
      e.Row.Cells[2].Visible = false; 

     } 
    } 

現在嘗試獲取價值。確實你會得到價值。

0

我有同樣的問題。

您無法隱藏該列並將代碼保留在後面。

你必須用JavaScript直接在客戶端隱藏它。

我這樣做:

在我的CSS或頁面:

<style type="text/css"> 
.hiddencol 
{ 
    display: none; 
} 
.viscol 
{ 
    display: block; 
} 
</style> 

然後添加樣式到gridViewer的綁定列。

例如:

 <asp:BoundField DataField="AgentGUID" HeaderText="AgentGUID" ReadOnly="True" SortExpression="AgentGUID" 
      meta:resourcekey="BoundFieldResource1"> 
      <HeaderStyle CssClass="hiddencol" /> 
      <ItemStyle CssClass="hiddencol" /> 
      <FooterStyle CssClass="hiddencol" /> 
     </asp:BoundField> 
相關問題