2013-06-21 11 views
5

Ho un radgrid in cui nascondo la colonna Id. Ora voglio ottenere il suo valore sul clic di linkbutton. Se la colonna è visibile funziona bene maOttieni il valore della colonna nascosta di RadiusR Telerik in asp.net

mostra un valore vuoto quando è invisibile. il mio codice è

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

puoi inserire il tuo codice html della griglia per favore – Sora

+0

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

risposta

16

Si prega di provare con il frammento di codice di seguito.

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 

    } 
} 

Utilizzare display proprietà al posto-di visibile proprietà.

+1

Questo è l'approccio migliore Si noti che sta usando Visible = "false" invece di usare Display = "false". – msigman

1

La colonna è sempre invisibile? Se è così, si può mettere l'ID nella proprietà DataKeyNames come in:

DataKeyNames="ID" 

E poi accedervi tramite:

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

Il modo più semplice è impostare Visible = true e Display = false e si dovrebbe andare bene.

+0

upvote per questo. è davvero il modo più semplice e funziona perfettamente. – ddaniel

Problemi correlati