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

Radcombobox not retaining selected value after postback

4 Answers 1254 Views
Persistence Framework
This is a migrated thread and some comments may be shown as answers.
Sandeep
Top achievements
Rank 1
Sandeep asked on 13 Apr 2015, 07:22 PM

Hi, I have a Radcombobox as a data item within a RadGrid. I have needdatasource and itemdatabound methods for the RadGrid during which I load the Radcomboboxes in the Grid. 

 

 <telerik:RadGrid  
                            id="testMappingGrid" 
                            runat="server" 
                            HorizontalAlign="Center" 
                            Visible="true" 
                            Width="782px" 
                            OnNeedDataSource="testMappingGrid_NeedDataSource" 
                            OnItemDataBound="testMappingGrid_ItemDataBound"
                            EnableViewState="true"
                            Skin="Silk" MasterTableView-EnableViewState="true">
                            <MasterTableView runat="server" AutoGenerateColumns="false" DataKeyNames="prodID">
                                <Columns>
                                    <telerik:GridBoundColumn DataField="prodID" DataType="System.Int32" 
                                        HeaderText="ID" SortExpression="prodID" 
                                        UniqueName="prodID" Visible="True" Display="true">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="prodName" HeaderText="Product" 
                                        SortExpression="prodName" UniqueName="prodName" ItemStyle-Width="200px">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridTemplateColumn HeaderText="Test Tool" SortExpression="testTool"  UniqueName="testTool" ItemStyle-Width="120px">
                                        <ItemTemplate>
                                            <telerik:RadComboBox ID="testTool"
                                                runat="server" 
                                                DataSourceID="testtoolsSqlDataSource"  
                                                Skin="Metro" 
                                                OnSelectedIndexChanged="testTool_OnSelectedIndexChanged"   
                                                DataTextField="Name" 
                                                DataValueField="ToolsID" 
                                                Width="120px"
                                                Height="200px" 
                                                AutoPostBack="true" />
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Test Project" SortExpression="testToolProject"  UniqueName="testToolProject" ItemStyle-Width="142px">
                                        <ItemTemplate>
                                            <telerik:RadComboBox ID="testProjName"
                                                CausesValidation="false"
                                                Height="200px"
                                                runat="server"
                                                Width="142px"
                                                DropDownWidth="200px"
                                                Skin="Metro" 
                                                AutoPostBack="true" EnableViewState="true" ViewStateMode="Enabled"
                                                >
                                            </telerik:RadComboBox>

<...>

-----------------------------------------------------------------------------------------------

 

The Radcomboxes are losing selected values after postback. Is there a way to retain selected values in a Radcombobox across postbacks?

 

Thanks,

Sandeep

4 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 14 Apr 2015, 01:05 PM
Hello Sandeep,

In order to set the selected item for RadComboBox you can define the SelectedValue property of the control. You should ensure that the value from the corresponding DataField  is used as SelectedValue.

The code snippet below illustrates how the markup for the first GridTemplateColumn would look like:

<telerik:GridTemplateColumn HeaderText="Test Tool" SortExpression="testTool"  UniqueName="testTool" ItemStyle-Width="120px">
    <ItemTemplate>
        <telerik:RadComboBox ID="testTool"
            runat="server"
            DataSourceID="testtoolsSqlDataSource" 
            Skin="Metro"
            OnSelectedIndexChanged="testTool_OnSelectedIndexChanged"  
            DataTextField="Name"
            DataValueField="ToolsID"
            Width="120px"
            Height="200px"
            AutoPostBack="true"
            SelectedValue='<%# Eval("TestToolID") %>'
            />
    </ItemTemplate>
</telerik:GridTemplateColumn>



Regards,
Viktor Tachev
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Sandeep
Top achievements
Rank 1
answered on 14 Apr 2015, 03:49 PM
The selectedValue should ideally be set when we actually select an item from the RadComboBox isn't it? And when we actually select an item from the RadComboBox, how do we ensure the selection remains even after postback? Are you saying we 'must' initially have a selected value even before the user makes a selection from the RadComboBox? 
0
Viktor Tachev
Telerik team
answered on 17 Apr 2015, 08:44 AM
Hello Sandeep,

When the SelectedValue is defined the RadComboBox will initially display the value from the datasource for the current field. 

With this said, note that with the current setup the selected values would not be persisted to the datasource as the dropdown controls are placed in ItemTemplate. In case you would like to persist the changes to the datasource you should use EditItemTemplate.

I also tried to replicate the behavior you describe, however, I was not able to. I am attaching a sample project I used for testing. The selected item in the RadComboBox is persisted after postback on my end. Would you give the sample a try and let me know what is different in your scenario?

I would appreciate it if you could modify the sample in a way that the issue is replicated and send it back. Thus, we will be able to examine the problem locally and look for its cause.

Regards,
Viktor Tachev
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Ghezzo
Top achievements
Rank 1
answered on 01 Sep 2017, 10:20 AM

Dear Viktor to me happen a most strange thing. i have a radcombo inside radgrid (inside RadAjaxPanel)  in edittemplate like u said.

 <EditItemTemplate>
       <telerik:RadComboBox ID="rcbStatoRecUpd" runat="server"
                    OnSelectedIndexChanged="rcbStatoRecUpd_SelectedIndexChanged"
                    DataSourceID="ObjLoadStatoRecAF"
                    DataValueField="cod_stato"
                     DataTextField="descr_stato"
                      SelectedValue='<%# Bind("stato_rec") %>'                   
                      AppendDataBoundItems="false" DropDownWidth="150px" AutoPostBack="true">
                       <Items>
  <telerik:RadComboBoxItem Text="Seleziona" Value=""/>
                        </Items>
                                 </telerik:RadComboBox>
                                      </EditItemTemplate>

in the event rcbStatoRecUpd_SelectedIndexChanged all work good and i see new selectedvalue and the new text.

BUT.....

in RadGrid1_UpdateCommand    HAPPEN THE STRANGE THING.....

   Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)

   Dim rcbStatoRecUpd As RadComboBox = CType(editedItem.FindControl("rcbStatoRecUpd"), RadComboBox)
    Dim statorec As String = rcbStatoRecUpd.SelectedValue().ToString
    Dim descrstatorec As String = rcbStatoRecUpd.Text.ToString()

    **************************************************************************

    descrstatorec   HAVE THE NEW TEXT BUT statorec  STILL HAVE the OLDVALUE.

    WHERE I MAKE ERROR ? what's wrong ?

   ty

Maurizio Brusini

 

 

Tags
Persistence Framework
Asked by
Sandeep
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Sandeep
Top achievements
Rank 1
Ghezzo
Top achievements
Rank 1
Share this question
or