Grid selection lost on postback

8 posts, 1 answers
  1. CBARS
    CBARS avatar
    55 posts
    Member since:
    Sep 2008

    Posted 18 Sep 2008 Link to this post

    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?



  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Sep 2008 Link to this post

    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.
  3. CBARS
    CBARS avatar
    55 posts
    Member since:
    Sep 2008

    Posted 18 Sep 2008 Link to this post

    EnableViewState is true. I haven't changed it from the default, and I specified it explicitly in the RadGrid tag.

    Still loses the selection.
  4. Answer
    Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 18 Sep 2008 Link to this post

    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.
  5. CBARS
    CBARS avatar
    55 posts
    Member since:
    Sep 2008

    Posted 18 Sep 2008 Link to this post

    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 :

    1 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.


  6. CBARS
    CBARS avatar
    55 posts
    Member since:
    Sep 2008

    Posted 19 Sep 2008 Link to this post

    Ended up using the client side solution in the article. Couldn't get the server side solution going... Some JavaScript, no problem though.

    Thanks.
  7. Frank
    Frank avatar
    4 posts
    Member since:
    Jun 2014

    Posted 28 Jun 2014 Link to this post

    got the same issue, but NEED the server-side postback ... :(
    EnableViewState = true, but the Load_ViewState Event is not fired ...
  8. Frank
    Frank avatar
    4 posts
    Member since:
    Jun 2014

    Posted 28 Jun 2014 in reply to Frank Link to this post

    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.
Back to Top