Radcombobox not retaining selected value after postback

4 posts, 0 answers
  1. Sandeep
    Sandeep avatar
    5 posts
    Member since:
    Oct 2014

    Posted 13 Apr 2015 Link to this post

    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

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 14 Apr 2015 Link to this post

    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.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sandeep
    Sandeep avatar
    5 posts
    Member since:
    Oct 2014

    Posted 14 Apr 2015 in reply to Viktor Tachev Link to this post

    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? 
  5. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 17 Apr 2015 Link to this post

    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.

     
Back to Top