2013-06-21 94 views
5

我有一個隱藏Id列的radgrid。現在我想通過linkbutton點擊來獲得它的價值。如果列是可見的,它工作正常,但在asp.net中獲取radgrid telerik隱藏列的值

它顯示空白值時,它是不可見的。我的代碼是

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e) 
{ 
    if (e.CommandName == "Detail") 
    { 
     GridDataItem dataItm = e.Item as GridDataItem; 

     string value = dataItm["Id"].Text; 
    } 
} 
+1

你可以發佈你的網格代碼請 – Sora

+0

http://www.telerik.com/forums/breaking-change-hidden-column-cell-text-is-not-persisted-in-viewstate – Chiramisu

回答

16

請與下面的代碼片段嘗試。

ASPX

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource" 
    OnItemCommand="RadGrid1_ItemCommand"> 
    <MasterTableView DataKeyNames="ID"> 
     <Columns> 
      <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"> 
      </telerik:GridBoundColumn> 
      <telerik:GridBoundColumn DataField="ID" UniqueName="ID1" HeaderText="ID" Visible="false"> 
      </telerik:GridBoundColumn> 
      <telerik:GridBoundColumn DataField="ID" UniqueName="ID2" HeaderText="ID" Display="false"> 
      </telerik:GridBoundColumn> 
      <telerik:GridTemplateColumn> 
       <ItemTemplate> 
        <asp:Button ID="Button1" runat="server" CommandName="Detail" CommandArgument='<%# Eval("ID") %>' /> 
       </ItemTemplate> 
      </telerik:GridTemplateColumn> 
     </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

ASPX.CS

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) 
{ 
    dynamic data = new[] { 
     new { ID = 1, Name ="Name1"}, 
     new { ID = 2, Name = "Name2"}, 
     new { ID = 3, Name = "Name3"}, 
     new { ID = 4, Name = "Name4"}, 
     new { ID = 5, Name = "Name5"} 
    }; 

    RadGrid1.DataSource = data; 

} 

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e) 
{ 
    if (e.CommandName == "Detail") 
    { 
     GridDataItem item = e.Item as GridDataItem; 

     string strID = item.GetDataKeyValue("ID").ToString(); // We are able to get ID field value here 
     string strID1 = item["ID1"].Text; // We are NOT able to get ID field value here Because column is Visible false 
     string strID2 = item["ID2"].Text; // We are able to get ID field value here 
     string strCommandArgument = e.CommandArgument.ToString(); // We are able to get ID field value here 

    } 
} 

請使用顯示財產佈局的可見財產。

+1

這是最好的方法。注意他使用Visible =「false」而不是使用Display =「false」。 – msigman

1

列是否始終不可見?如果是這樣,你可以把DataKeyNames屬性的ID爲:

DataKeyNames="ID" 

,然後通過訪問:

var id = (int)dataItm.getDataKeyValue("ID"); 
2

最簡單的方法是設置Visible = true和Display = false,你應該沒問題。

+0

upvote爲這一個。確實是最簡單的方法,並且完美地工作。 – ddaniel

相關問題