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

[Solved] Client-side selection and server-side postback

1 Answer 275 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 04 Jul 2009, 06:14 PM
I have a grid control which I have enabled client-side binding using a web service as the datasource. 

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">   
    <script type="text/javascript">  
        function RadGrid1_DataBinding(sender, args) {  
            // This event will be raised twice by default!   
            // The first call will retrieve data and the second call will retrieve total items count.  
            // If you want to cancel the event you can use:  
            // args.set_cancel(true);  
 
            // get data source location, method name and arguments  
            var dataSourceLocation = args.get_location();  
            var selectMethodName = args.get_methodName();  
            var methodArguments = args.get_methodArguments();  
 
            // set data source location and method name  
            // args.set_location("url to your data source");  
            // args.set_methodName("your method name");  
 
            // The grid will always pass by default four arguments for the SelectMethod.  
            // SelectCountMethod will be called with no arguments by default!  
 
            // get names for select parameters  
            var startRowIndexParameterName = sender.ClientSettings.DataBinding.StartRowIndexParameterName;  
            // default is "startRowIndex".   
 
            var maximumRowsParameterName = sender.ClientSettings.DataBinding.MaximumRowsParameterName;  
            // default is "maximumRows"  
 
            var sortParameterName = sender.ClientSettings.DataBinding.SortParameterName;  
            // default is "sortExpression"  
 
            var filterParameterName = sender.ClientSettings.DataBinding.FilterParameterName;  
            // default is "filterExpression"  
 
            // construct your own arguments  
            // var myMethodArguments = new Object();  
            // myMethodArguments.myArgumentName = "myArgumentValue";  
            // args.set_methodArguments(myMethodArguments);  
        }  
 
 
 
     function RowSelected(row, eventArgs) {  
        
         var grid = $find("<%=RadGridService.ClientID %>").get_masterTableView();  
         grid.rebind();  
      
 
     }   
 </script> 
</telerik:RadCodeBlock> 




<telerik:RadGrid ID="RadGrid1" AllowSorting="True" AllowPaging="True" 
            AutoGenerateColumns="False" runat="server" GridLines="None" Width="653px"   
            onneeddatasource="RadGrid1_NeedDataSource" > 
              
                <MasterTableView> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" DataType="System.Int32" /> 
                    <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" /> 
                    <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" /> 
                             </Columns> 
            </MasterTableView> 
            <PagerStyle AlwaysVisible="true" /> 
            <ClientSettings EnablePostBackOnRowClick="True" > 
              
              
                <ClientEvents OnDataBinding="RadGrid1_DataBinding" /> 
 
              
                <DataBinding Location="Services.asmx" 
                    SelectMethod="GetData" SelectCountMethod="GetCount" /> 
                <Selecting AllowRowSelect="True" /> 
              
              
                 <ClientEvents OnRowSelected = "RowSelected"/>  
 
              
            </ClientSettings> 
        </telerik:RadGrid> 

At the bottom of the form, I have a button to post the results of the client-side selection.  How can I retrieve the results of the client-side selection on the server postback?

I have attempted the following code, but text returns "&nbsp;".  If the Grid has no selection, the SectedItems count is 0 and if one item is selected the count is 1, but I am not sure how to retrieve the PrimaryKey or cell from the selected item.

 protected void Button1_Click(object sender, EventArgs e)     
        {     
           GridItemCollection selectedItems = RadGrid1.SelectedItems;     
           foreach (GridItem selectedItem in selectedItems)     
            {     
              string text = ((GridDataItem)selectedItem)["LastName"].Text;     
                                   
            }                  
        }     
 

Any ideas what I am missing?

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 08 Jul 2009, 10:13 AM
Hello Michael,

You can try to save the hierarchical index of the selected row on the client in a hidden field. Thus when the client post backs you can retrieve the value of the hidden field and hence the selected items.

Let me know whether this helps.

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
Michael
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or