Prevent dynamically generated radcombobox in a RadGrid to lose its selected value at time of databind.

4 posts, 0 answers
  1. Ali
    Ali avatar
    6 posts
    Member since:
    Jul 2012

    Posted 01 Aug 2012 Link to this post

    I have a RadGrid which has a label, a combobox and a check box. I fill up the combobox on ItemDatabound Event of the RadGrid. What i do is create a new row on CheckedChange event of the row's CheckBox. When the grid is loaded for the first time I am storing the datasource in a session variable. When the row's checkbox is checked I am manipulating the datatable stored in session variable to add the new row in the session's datatable . I then bind the datasource of the RadGrid with the new Session Variable. I get the required RadGrid with the new row but  I am losing the selected value of all combo boxes in which a value had been selected before the checkbox was checked . I know this is the standard behaviour and there is nothing wrong with it but what i need to know is a way I can retain all the previously selected values of the comboboxes of RadGrid after the databind.

    Thanks in advance.
  2. Eyup
    Admin
    Eyup avatar
    3663 posts

    Posted 02 Aug 2012 Link to this post

    Hello Ali,

    You will need to save the selected text in the created Session, or create a new Session to hold a HashTable with the DataKeyID of the row and the selected text of the related combo box. Therefore, using the ItemDataBound event you will be able to restore the selected items:
     
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        RadComboBox combo = dataItem["YourColumn"].FindControl("YourCombo") as RadComboBox;
        Hashtable selectedItems = (Hashtable)Session["selectedItems"];
        string dataKeyID = dataItem["YourDataKeyColumn"].Text;
        string selectedItem = selectedItems[dataKeyID];
        combo.Items.FindItemByText(selectedItem).Selected = true;
    }

    I hope this will prove helpful.

    All the best,
    Eyup
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. Ali
    Ali avatar
    6 posts
    Member since:
    Jul 2012

    Posted 02 Aug 2012 Link to this post

    Hi Eyup,

    Even I thought of something like this but the problem is that there is a combo box in each row having the same set of values for every row and I don't know how many rows would be there in the grid. On post back each and every combo box will lose the selected value, and so i would have to store each and every value by saving it in session. Is there a better approach to this scenario or would I have to continue with saving each and every value. 
    I have to bind same data in combobox for each and every row, even for the newly generated row. Can anything be done there ? 

    Thanks for your help.

  4. Eyup
    Admin
    Eyup avatar
    3663 posts

    Posted 07 Aug 2012 Link to this post

    Hello Ali,

    One possible approach for performance optimization would be to attach a SelectedIndexChanged event for the generated combos and save only the ones to which any modification has been made:
          ... 
         combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
       }
    protected void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
       {
           // make the required saving operation
       }

    That should do the trick.

    Regards,
    Eyup
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top