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

getDataKeyValue returns null on client-side when grid is created dynamically server-side

1 Answer 198 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark DeMichele
Top achievements
Rank 1
Mark DeMichele asked on 24 Oct 2008, 01:54 AM
Hi there,

I have a page where the grid is being created dynamically at the Page_Init stage. I am using the DataKeyNames property in order to specify the fields whose key values are to be saved, but this property seems to have no effect when using Javascript to obtain the values client-side. A view of the page source in the browser reveals that the client script that is normally generated containing the values isn't generated in this case.

I've been able to replicate the problem with a simple test page, the code of which is posted below. Maybe I'm doing something wrong?

Thanks,
Mark

ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestProgrammaticRadGrid.aspx.cs" Inherits="Test_TestProgrammaticRadGrid" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title></title
    <telerik:RadCodeBlock runat="server"
    <script language="javascript" type="text/javascript"
        function TestButton_onclick() 
        { 
            var radGrid = $find('<%=TestRadGrid.ClientID %>'); 
            var rows = radGrid.get_masterTableView().get_dataItems(); 
            alert('# items: ' + rows.length); 
            alert('first PK: ' + rows[0].getDataKeyValue('ID')); 
        } 
      
    </script> 
    </telerik:RadCodeBlock> 
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server"
        </asp:ScriptManager> 
        <asp:PlaceHolder ID="GridPlaceholder" runat="server" />        
         
        <input type="button" id="TestButton" value="Click Here" onclick="return TestButton_onclick();" /> 
    </div> 
    </form> 
</body> 
</html> 
 

Code behind:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
public partial class Test_TestProgrammaticRadGrid : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
            PopulateGrid(); 
    } 
 
    private void PopulateGrid() 
    { 
        List<TestData> list = new List<TestData>(); 
         
        for (int i = 0; i < 11; i++) 
            list.Add(new TestData(i, "test " + i.ToString())); 
 
        TestRadGrid.DataSource = list; 
        TestRadGrid.DataBind(); 
    } 
 
    protected void Page_Init(object sender, EventArgs e) 
    { 
        DefineGridStructure(); 
    } 
 
    private void DefineGridStructure() 
    { 
        TestRadGrid = new RadGrid(); 
         
        TestRadGrid.AutoGenerateColumns = false
 
        InitializeProblemGrid(TestRadGrid); 
 
        AddCenteredColumn(TestRadGrid, "ID""ID""ID"); 
        AddCenteredColumn(TestRadGrid, "Name""Name""Name"); 
 
        GridPlaceholder.Controls.Add(TestRadGrid); 
 
    } 
 
    private void InitializeProblemGrid(RadGrid radGrid) 
    { 
        radGrid.MasterTableView.DataKeyNames = new string[] { "ID" }; 
        radGrid.ClientSettings.Selecting.AllowRowSelect = true
    } 
 
    private void AddCenteredColumn(RadGrid radGrid, string uniqueName, string headerText, string dataField) 
    { 
        GridBoundColumn column = new GridBoundColumn(); 
        column.UniqueName = uniqueName; 
        column.HeaderText = headerText; 
        column.DataField = dataField; 
        column.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; 
        column.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
        radGrid.MasterTableView.Columns.Add(column); 
    } 
 
 
    private RadGrid _TestRadGrid; 
    protected RadGrid TestRadGrid 
    { 
        get { return _TestRadGrid; } 
        set { _TestRadGrid = value; } 
    } 
 
 
public class TestData 
    public TestData(int id, string name) 
    { 
        _ID = id; 
        _Name = name; 
    } 
 
 
    public int ID 
    { 
        get { return _ID; } 
        set { _ID = value; } 
    } 
 
    public string Name 
    { 
        get { return _Name; } 
        set { _Name = value; } 
    } 
 
 
    private int _ID; 
    private string _Name; 
 


1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Oct 2008, 03:40 AM
Hi Mark,

For getting the key value on the client side you need to set the ClientDataKeyNames property for the Grid. Refer the following help article for getting more details.
Extracting key values client-side

Thanks
Shinu
Tags
Grid
Asked by
Mark DeMichele
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or