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

[Solved] Grid selection lost on postback

6 Answers 725 Views
Grid
This is a migrated thread and some comments may be shown as answers.
CBARS
Top achievements
Rank 2
CBARS asked on 18 Sep 2008, 11:31 AM
Hi

I have a RadGrid being filled from an ObjectDataSource. I've enabled client side selection, and EnablePostBackOnRowClick. The RadGrid is also added to a RadAjaxManager. It is set to update a Panel on the same page that contains some Label controls. I'm handling the ItemCommand eventhandler of the RadGrid and then checking if the RowCommand == "RowClick". All this works pretty well.

However, when the update takes place, and the Label controls are filled, the RadGrid loses its selection. I'm only selecting one row at a time.

How can I have the row selection persist through the postback?



6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Sep 2008, 12:33 PM
Hello cbars,
In order to persist the client selection across postback or ajax requests, you need to ensure that viewstate is enabled for your selectable grid.
Princy.
0
CBARS
Top achievements
Rank 2
answered on 18 Sep 2008, 01:12 PM
EnableViewState is true. I haven't changed it from the default, and I specified it explicitly in the RadGrid tag.

Still loses the selection.
-1
Accepted
Daniel
Telerik team
answered on 18 Sep 2008, 01:19 PM
Hello Marcel,

Depending on your scenario these articles may be helpful:
Persisting the selected rows client-side on sorting/paging/filtering/grouping
Persisting the selected rows server-side on sorting/paging/filtering/grouping

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
CBARS
Top achievements
Rank 2
answered on 18 Sep 2008, 02:46 PM
Thanks for those articles... Had a look at them, and used the principle that is discussed there (save the selected items in a session variable, repopulate after postback).

Since I only want one item selected, I have this code in my ItemCommand handler :

appID = Convert.ToInt32(RadGrid1.SelectedValue);
Session["selectedItem"] = appID;  

and this in my PreRender handler :

if (Session["selectedItem"] != null) 
            { 
                int selecteditem = int.Parse(Session["selectedItem"].ToString()); 
                foreach (Telerik.Web.UI.GridItem item in RadGrid1.MasterTableView.Items) 
                { 
                    if (item is Telerik.Web.UI.GridDataItem) 
                    { 
                        Telerik.Web.UI.GridDataItem dataItem = (Telerik.Web.UI.GridDataItem)item; 
 
                        if (dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["ID"].ToString() == selecteditem.ToString()) 
                        { 
                            dataItem.Selected = true
                            break; 
                        } 
                    } 
                } 
            } 

When I follow the code, one of the items' Selected property is set to true, but on the page it isn't selected.


0
CBARS
Top achievements
Rank 2
answered on 19 Sep 2008, 03:17 PM
Ended up using the client side solution in the article. Couldn't get the server side solution going... Some JavaScript, no problem though.

Thanks.
0
Frank
Top achievements
Rank 1
answered on 28 Jun 2014, 09:35 AM
Got it: Adding (bold line)
    <telerik:RadGrid ID="RadGrid1" runat="server"
        DataKeyValues="Id"
    </telerik:RadGrid>
to RadGrid does the Trick, so the selected Item DataKey (Field "Id") will be stored as selectedItem.
Tags
Grid
Asked by
CBARS
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
CBARS
Top achievements
Rank 2
Daniel
Telerik team
Frank
Top achievements
Rank 1
Share this question
or