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

Dynamically created combo box losing value after multiple postbacks

9 Answers 408 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 07 May 2008, 04:27 PM
I've run into a problem using the asp.net ajax RadComboBox that is dynamically added to the page, which is caused by it losing its value if more than one postback occurs.

I select an item in the combobox and then trigger a postback.  After the first postback the initially selected value is still selected, but after a second postback it is not.  The old style RadComboBox and the built in DropDownList both retain their selected value.

Here's a simple test example that shows the problem.  The aspx simply contains a repeater with a placeholder inside it.

        <asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager> 
        <asp:Repeater ID="repTest" runat="server"
            <ItemTemplate> 
                <div> 
                    <asp:PlaceHolder ID="plcTest" runat="server"></asp:PlaceHolder> 
                </div> 
            </ItemTemplate> 
        </asp:Repeater> 
 
        <asp:Button ID="btnTest" runat="server" Text="PostBack" /> 

In the ItemDataBound event handler the combo boxes are created and added to the placeholder.  If I select Item 2 in each combo box and then click the button twice then at the end the first two combo boxes still have Item 2 selected but the asp.net ajax RadComboBox has reverted to Item 1.

        protected void Page_Load(object sender, EventArgs e) 
        { 
            repTest.DataSource = new int[] { 1, 2, 3 }; 
            repTest.DataBind(); 
        } 
 
        protected override void OnInit(EventArgs e) 
        { 
            base.OnInit(e); 
            repTest.ItemDataBound += new RepeaterItemEventHandler(repTest_ItemDataBound); 
        } 
 
        private void repTest_ItemDataBound(object sender, RepeaterItemEventArgs e) 
        { 
            PlaceHolder plcTest = (PlaceHolder)e.Item.FindControl("plcTest"); 
 
            switch ((int)e.Item.DataItem) 
            { 
                case 1: 
                    DropDownList cbo1 = new DropDownList(); 
                    cbo1.ID = "cbo1"
                    cbo1.DataSource = GetComboData(); 
                    cbo1.DataBind(); 
                    plcTest.Controls.Add(cbo1); 
                    break; 
                case 2: 
                    Telerik.WebControls.RadComboBox cbo2 = new Telerik.WebControls.RadComboBox(); 
                    cbo2.ID = "cbo2"
                    cbo2.DataSource = GetComboData(); 
                    cbo2.DataBind(); 
                    plcTest.Controls.Add(cbo2); 
                    break; 
                case 3: 
                    Telerik.Web.UI.RadComboBox cbo3 = new Telerik.Web.UI.RadComboBox(); 
                    cbo3.ID = "cbo3"
                    cbo3.DataSource = GetComboData(); 
                    cbo3.DataBind(); 
                    plcTest.Controls.Add(cbo3); 
                    break; 
            } 
        } 
 
        private string[] GetComboData() 
        { 
            return "Item 1;Item 2;Item 3".Split(';'); 
        } 

9 Answers, 1 is accepted

Sort by
0
Tim
Top achievements
Rank 1
answered on 07 May 2008, 04:34 PM
I forgot to say: I'm running under VS 2008 using the Q1 2008 3.5 version of asp.net ajax RadControls (2008.1.415.35).  The old style RadComboBox I'm comparing against is a slightly older one, Q3 2007 (2.8.4.0).
0
Elmar
Top achievements
Rank 1
answered on 09 May 2008, 12:09 PM
I am running on the same config and have basically the same issue.
Dynamically added ComboBox - value gone after Postback.
I initially thought it might be an issue with the RequiredField Validator I am attaching to it but Tim´s sample seems to show that it´s not a validator only issue.

Thanks,
Elmar
0
Tim
Top achievements
Rank 1
answered on 09 May 2008, 01:09 PM
A rather hacky workaround is to reselect the selected item on client load using the following function attached to the OnClientLoad event:

    function ReselectSelectedComboItem(sender) 
    { 
        sender.findItemByValue(sender.get_value()).select(); 
    } 

Unfortunately you can't do this if the combo box has AutoPostBack set to true, since this triggers a postback.  The only way I found to get round this was to set AutoPostBack to false, and trigger the postback myself from OnClientSelectedIndexChanged, but to ensure that this didn't happen when the OnClientSelectedIndexChanged was triggered by the code above.  Hopefully Telerik will get back to us with a better solution soon.
0
Simon
Telerik team
answered on 10 May 2008, 10:52 AM
Hi guys,

This problem has been resolved in the latest build of RadComboBox for ASP.NET AJAX.

I have attached the trial version of the build to this post.

Please download it and give it a go.

All the best,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Tim
Top achievements
Rank 1
answered on 02 Jun 2008, 09:42 AM
Sorry for the delay in my response...

This does the trick.  Thanks for your help.
0
cesarzam
Top achievements
Rank 1
answered on 06 Jun 2008, 07:31 PM

I have the same issue with VS 2005

'ddlStatus' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: 'ddlStatus' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentOutOfRangeException: 'ddlStatus' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value]
   System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +965
   System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +80
   System.Web.UI.WebControls.ListControl.PerformSelect() +32
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
   System.Web.UI.Control.DataBindChildren() +236
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
   System.Web.UI.Control.DataBind() +31
   System.Web.UI.Control.DataBindChildren() +236
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
   System.Web.UI.Control.DataBind() +31
   System.Web.UI.Control.DataBindChildren() +236
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
   System.Web.UI.Control.DataBind() +31
   Telerik.WebControls.GridEditFormItem.SetupItem(Boolean dataBind, Object dataItem, GridColumn[] columns, ControlCollection rows) +298
   Telerik.WebControls.GridItemBuilder.CreateItems(GridGroupingContext group) +797
   Telerik.WebControls.GridTableView.CreateItems(IEnumerator enumerator, GridColumn[] columns, ControlCollection controls) +167
   Telerik.WebControls.GridTableView.CreateControlHierarchy(Boolean useDataSource) +1012
   Telerik.WebControls.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +351
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +90
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +126
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +98
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +153
   Telerik.WebControls.GridTableView.PerformSelect() +24
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
   Telerik.WebControls.GridTableView.DataBind() +243
   Telerik.WebControls.GridTableView.Rebind() +79
   Telerik.WebControls.GridCommandEventArgs.ExecuteCommand(Object source) +326
   Telerik.WebControls.RadGrid.OnBubbleEvent(Object source, EventArgs e) +171
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
   Telerik.WebControls.GridItem.OnBubbleEvent(Object source, EventArgs e) +147
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
   System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e) +107
   System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +203
   System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3825


Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433
0
Simon
Telerik team
answered on 09 Jun 2008, 06:12 AM
Hi cesarzam,

Try clearing the selection of RadComboBox before binding it.

The reason for this error is that you have selected a value in the RadComboBox and some time afterwards you bind the control to a data source which does not have this selected value in it.

Kind regards,
Simon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ajay
Top achievements
Rank 1
answered on 13 Apr 2012, 10:53 AM
Hi

I am facing the same problem in version "Telerik.Web.UI_2012_1_215_Dev_hotfix".

Is this resolved issue have reappear ?

Please any one having any workaround for this ?

Thank you
Ajay
0
Cat Cheshire
Top achievements
Rank 1
answered on 13 Apr 2012, 03:42 PM

This thread is very old - from 2008.
You can explain what the exact issue is and paste here some working code.
Tags
ComboBox
Asked by
Tim
Top achievements
Rank 1
Answers by
Tim
Top achievements
Rank 1
Elmar
Top achievements
Rank 1
Simon
Telerik team
cesarzam
Top achievements
Rank 1
Ajay
Top achievements
Rank 1
Cat Cheshire
Top achievements
Rank 1
Share this question
or