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

2 posts, 0 answers
  1. Ricard Bertran Vall
    Ricard Bertran Vall avatar
    18 posts
    Member since:
    Sep 2009

    Posted 14 Jul 2010 Link to this post

    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:

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
        GridLines="None" DataSourceID="XmlDataSource1">
        <MasterTableView  CommandItemDisplay="Top" EditMode="InPlace" 
                <HeaderStyle Width="20px"></HeaderStyle>
                <HeaderStyle Width="20px"></HeaderStyle>
                <telerik:GridBoundColumn DataField="CustomerId" HeaderText="CustomerId" 
                    SortExpression="CustomerId" UniqueName="CustomerId">
                <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" 
                    SortExpression="FirstName" UniqueName="FirstName">
                <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" 
                    SortExpression="LastName" UniqueName="LastName">
                <asp:Label ID="Label1" runat="server" Text="Auto PostBack RadComboBox"></asp:Label>
                <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="True" 
                        <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" />

    Code behind:
    protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)

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

    Posted 14 Jul 2010 Link to this post

    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 .

    protected void Page_Load(object sender, EventArgs e)
    protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
           RadComboBox combo = (RadComboBox)o;
           Session["combo"] = combo.SelectedValue;
     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();

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top