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

RadComboBox inside CommandItemTemplate doesn't keep the SelectedValue property after grid rebind

1 Answer 246 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ricard Bertran Vall
Top achievements
Rank 1
Ricard Bertran Vall asked on 14 Jul 2010, 10:58 AM
Hi,
I have a RadGrid with a custom CommandItemTemplate. In my CommandItemTemplate I have a RadComboBox with the 'Autopostback' property enabled. I would like to change the grid's data according the RadComboBox selected value. But after the RadGrid rebind, the RadComboBox doesn't keep the 'SelectedValue' property.
If I don't call the RadGrid Rebind method it works fine! But the grid doesn't change so I have to rebind the grid.
What I need to do to keep the RadComboBox SelectedValue after a RadGrid rebind?
Here is a simple sample:

ascx:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
    GridLines="None" DataSourceID="XmlDataSource1">
    <MasterTableView  CommandItemDisplay="Top" EditMode="InPlace" 
        DataSourceID="XmlDataSource1">
        <RowIndicatorColumn>
            <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn>
            <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridEditCommandColumn>
            </telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn DataField="CustomerId" HeaderText="CustomerId" 
                SortExpression="CustomerId" UniqueName="CustomerId">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" UniqueName="FirstName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" UniqueName="LastName">
            </telerik:GridBoundColumn>
        </Columns>
        <CommandItemTemplate>
            <asp:Label ID="Label1" runat="server" Text="Auto PostBack RadComboBox"></asp:Label>
            <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="True" 
                onselectedindexchanged="RadComboBox1_SelectedIndexChanged">
                <Items>
                    <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem1" Value="RadComboBoxItem1" />
                    <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem2" Value="RadComboBoxItem2" />
                    <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem3" Value="RadComboBoxItem3" />
                    <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem4" Value="RadComboBoxItem4" />
                    <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem5" Value="RadComboBoxItem5" />
                </Items>
            </telerik:RadComboBox>
        </CommandItemTemplate>
    </MasterTableView>
</telerik:RadGrid>



Code behind:
protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
{
    this.RadGrid1.Rebind();
      
}


1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Jul 2010, 11:37 AM
Hello Richard,

One suggestion is storing the SelectedValue of RadComboBox in a Session variable. Then in PreRender event reset the SelectedValue of RadComboBox by using that Session value .

C#:
protected void Page_Load(object sender, EventArgs e)
   {
       Session["combo"]=0;
   }
protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
   {
       RadComboBox combo = (RadComboBox)o;
       Session["combo"] = combo.SelectedValue;
       this.RadGrid1.Rebind();
   }
 protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       GridCommandItem cmdItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
       RadComboBox combo = (RadComboBox)cmdItem.FindControl("RadComboBox1");
       combo.SelectedValue = Session["combo"].ToString();
   }

Thanks,
Princy.
Tags
Grid
Asked by
Ricard Bertran Vall
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or