This is a migrated thread and some comments may be shown as answers.

Retrieving DataKeyNames (Name itself) and Value of Edited Row .. in Server PageLoad POstback

1 Answer 165 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Neal
Top achievements
Rank 1
Neal asked on 07 Aug 2008, 11:20 AM
Hi,

We need to retriev the DataKeyNames (Name itself) i.e the DataItem(Dsources Col Name, ..and  the Value of it  as/per Edited Row .. in Server Code,...but
 at the PageLoad event (IspOstback = true) as opposed to in the Event Handlers provided for  Insert Delete Edit Update,..
Reason being, we override the Page Load ,  referencing our   Userlog  obj in it, so all pages inheriting from the overridden Page Load, have it by default, 

1)  We now want to trap the Row Id (stored in DataKeyNames ), but since not all grids in the system use a std named "Id" as the DataKeyName,   ..!!
And
2) retrieve the value of the datakeyname of the "edited/deleted" row,..also in the Page load

Is this possible ? and if so how.

TIA
Neal

1 Answer, 1 is accepted

Sort by
0
plamen
Top achievements
Rank 1
answered on 12 Aug 2008, 07:14 AM
hi Neal :)


When the DataKeyNames property is set, the RadGrid control automatically populates its DataKeys collection with the values from the specified field or fields, which provides a convenient way to access the primary keys of each item.

The following code example demonstrates how to achieve your goal:

.aspx
            <telerik:RadGrid  
                DataSourceID="AccessDataSource1"  
                ID="RadGrid1"  
                runat="server"
                <MasterTableView  
                    AutoGenerateColumns="false"  
                    DataKeyNames="CustomerID,CompanyName"
                    <Columns> 
                        <telerik:GridBoundColumn UniqueName="CompanyName" HeaderText="CompanyName" DataField="CompanyName"
                        </telerik:GridBoundColumn> 
                         
                        <telerik:GridEditCommandColumn> 
                        </telerik:GridEditCommandColumn> 
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid> 


.cs
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
 
 
public partial class _Default : System.Web.UI.Page  
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (IsPostBack) 
        { 
            foreach (Telerik.Web.UI.DataKey dataKey in RadGrid1.MasterTableView.DataKeyValues) 
            { 
                foreach (String str in RadGrid1.MasterTableView.DataKeyNames) 
                { 
                    Response.Write(dataKey[str].ToString() + "</br>");    
                } 
            } 
        } 
    } 




Regards...
<John:Peel />
Tags
Grid
Asked by
Neal
Top achievements
Rank 1
Answers by
plamen
Top achievements
Rank 1
Share this question
or