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

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

3 Answers 129 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ali
Top achievements
Rank 1
Ali asked on 01 Aug 2012, 08:51 AM
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.

3 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 02 Aug 2012, 02:14 PM
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.
0
Ali
Top achievements
Rank 1
answered on 03 Aug 2012, 04:49 AM
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.

0
Eyup
Telerik team
answered on 07 Aug 2012, 08:27 AM
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.
Tags
General Discussions
Asked by
Ali
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Ali
Top achievements
Rank 1
Share this question
or