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

Client-side bound RadGrid does not provide server-side SelectedValue property

1 Answer 206 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ronald
Top achievements
Rank 1
Ronald asked on 07 May 2009, 09:48 AM
Hi,

I have a RadGrid that I bind client-side using data from a call to a WCF web service. The call is done via jQuery. Populating the grid with data works ok. However, I want a server-side SelectedIndexChanged event. I do receive the event, but I get an empty SelectedValue property.

The grid is configured as follows:

<telerik:RadGrid ID="gridEmployees" runat="server" GridLines="None" AutoGenerateColumns="False" 
    AllowMultiRowSelection="False" OnSelectedIndexChanged="gridEmployees_SelectedIndexChanged" 
    ShowHeader="False" AllowPaging="true" PageSize="20"
    <MasterTableView DataKeyNames="EmployeeId"
        <Columns> 
            <telerik:GridNumericColumn DataField="EmployeeId" UniqueName="EmployeeId" Visible="false" /> 
            <telerik:GridBoundColumn DataField="FullName" UniqueName="FullName" /> 
        </Columns> 
    </MasterTableView> 
    <ClientSettings EnablePostBackOnRowClick="true"
        <ClientEvents OnCommand="gridEmployees_Command" OnMasterTableViewCreated="gridEmployees_MasterTableViewCreated" /> 
        <Selecting AllowRowSelect="true" /> 
    </ClientSettings> 
    <PagerStyle Mode="NumericPages" /> 
</telerik:RadGrid> 

I use the following AJAX call via jQuery:

$.ajax({ 
    cache: false
    contentType: "application/x-www-form-urlencoded"
    data: searchData, 
    dataType: "json"
    type: "GET"
    url: "/Services/EmployeeService.svc/FindEmployees"
    success: function(data, textStatus) { 
        var gridEmployees = $find("<%= gridEmployees.ClientID %>"); 
        var employeesTable = gridEmployees.get_masterTableView(); 
        employeesTable.set_dataSource(data.Employees); 
        employeesTable.dataBind(); 
        employeesTable.set_virtualItemCount(data.VirtualItemCount); 
    }, 
}); 

And finally my service. I have left out the FindEmployeesResult class, but it's a simple POCO with the correct DataContract and DataMember attributes.

[ServiceContract(Name="EmployeeService", Namespace="Provisior.Services")] 
[AspNetCompatibilityRequirements( 
    RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class EmployeeService 
    [OperationContract] 
    [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] 
    public FindEmployeesResult FindEmployees(string searchTerm, int startIndex, int maximumRows) 
    { 
        var employees = new List<EmployeeInfo>(); 
        for (int i = 1; i <= 20; i++) 
        { 
            employees.Add(new EmployeeInfo 
                          { 
                              EmployeeId = i, 
                              FullName = "Employee " + i 
                          }); 
        } 
        return new FindEmployeesResult { Employees = employees, VirtualItemCount = 500 }; 
    } 
 

Since the grid has a MasterTableView with DataKeyNames="EmployeeId" and I provide a data source with this property, I would expect the value of EmployeeId to be the SelectedValue of the RadGrid in the gridEmployees_SelectedIndexChanged method. However, it is always empty (to be more precise, it is an instance of System.Object).

How do I make this work as it should?

Kind regards,
Ronald Wildenberg

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 11 May 2009, 10:24 AM
Hello Ronald,

Unfortunately the functionality you are searching is not supported. When the grid is bound to Web service data provider, the only place where you can retrieve the key value of the selected row, is on the client-side. You need to set the ClientDataKeyName property correctly in order to achieve this goal.

When the grid is not bound on the server the DataKeyValue is not populated, because grid is not aware about the client binding. You can overcome this issue by retrieving the key value on the client side and then send it to the server. Here is a code snippet showing how to achieve this:
        function gridEmployees_RowSelecting(sender, args) { 
            debugger
            var dataKeyValue = args.getDataKeyValue("EmployeeId"); 
        } 
You can send the retrieved value with the RadAjaxManager, or you can simply set a hidden field which will be post back to the server.

Regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Ronald
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or