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

    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();
          
    }


  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 .

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top