开发者

Retrieve value from GridTemplateColumn in server side (Telerik RadGrid)

开发者 https://www.devze.com 2023-04-12 19:03 出处:网络
I have the following code in my aspx page (simplified): <telerik:RadGrid ID=\"rgd_grid\" runat=\"server\">

I have the following code in my aspx page (simplified):

<telerik:RadGrid ID="rgd_grid" runat="server">
<MasterTableView>
<Columns> 
     <telerik:GridTemplateColumn UniqueName="Unique" HeaderText="Header" DataField="dataField">
     <ItemTemplate>
          <%# DataBinder.Eval(Container.DataItem, "expression") %>
     </ItemTemplate>
     </telerik:GridTemplateColumn> 
</Columns>
</MasterTableView>

I jus开发者_StackOverflowt need to do a loop in the grid to retrieve the values of the cells in the code-behind, but I've find no way to get the value in the "Eval" expression... I've try the following:

rgd_grid.MasterTableView.Items[0]["Unique"].Text;

But the text property is empty, while all the others are correct. Actually, I've tried a lot of other things, but this seems to be the most close to the objective.

Regards, I appreciate every help!


Are you sure the item returned is not the header or something like that? I think the header is included in the results, but could be wrong. Add a check like:

var item = rgd_grid.MasterTableView.Items[0] as GridDataItem;
if (item != null)
    string text = item["Unique"].Text;

If that doesn't work, you can always resort to using a Label control within the template, and finding the control by ID.


You should be using datakeys to retrieve values from the grid.

You can use the DataKeyNames property on the MasterTableView to specify the columns you need, like this:

<telerik:RadGrid ID="RadGrid1" runat="server" ...>
    <MasterTableView DataKeyNames="Col1, Col2, Col3" ...>

And then in the code-behind:

string col1 = RadGrid1.Items[0].GetDataKeyValue("Col1").ToString();


Here's another way, as I used the DataBinder to get my literal content as well as display it:

 protected void RadGrid_OnItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            var test = DataBinder.Eval(dataItem.DataItem, "Column").ToString();                
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消