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

SelectedValue lost on postback in dynamically added user controls

14 Answers 928 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 29 Jun 2012, 10:20 AM

We recently upgraded to the lates version of Telerik Ajax Controls (from 2010 Q2), and have an issue with the ComboBox.

We are dynaimcally loading user controls that contains comboboxes with autopostback set to true.
The problem is that the comboboxes selected values are lost on postback.

This works well when we use the old dll (2010 Q2).

I have tried the dll from Q3 2011 and i have the same problem with that.
Do you have any idea what would cause this?

Regards, Richard

Here is the code for the aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Modules_DEV_ComboboxBug_Default" %>
<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="rsm"></telerik:RadScriptManager>
    <div>
        <asp:PlaceHolder runat="server" ID="phContent" />
    </div>
    </form>
</body>
</html>

aspx.cs:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
public partial class Modules_DEV_ComboboxBug_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl uc = (WebUserControl)Page.LoadControl("WebUserControl.ascx");
uc.ID = "WebUserControl";
phContent.Controls.Add(uc);
}
}

ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
 
<telerik:radComboBox runat="server" ID="RadComboBox1" AutoPostBack="true">
</telerik:radComboBox>
<telerik:radComboBox runat="server" ID="RadComboBox2" AutoPostBack="true">
</telerik:radComboBox>

ascx.cs:

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] dataSource = new string[] {"UnSelected", "First", "Second", "Third" };
        RadComboBox1.DataSource = dataSource;
        RadComboBox1.DataBind();
 
        RadComboBox2.DataSource = dataSource;
        RadComboBox2.DataBind();
    }
}

14 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 02 Jul 2012, 11:24 AM
Hello Richard,

RadComboBox performs a Post Back every time a different item is selected and this causes a new page load. When the Load stage of the page life cycle is reached the RadComboBox is rebound, and this action overwrites the previous selected values. You could consider using 2 approaches in order to  resolve this issue. The first one is to bind the control only upon initial load as shown below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
  
public partial class WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] dataSource = new string[] { "UnSelected", "First", "Second", "Third" };
        if (!Page.IsPostBack)
        {
            RadComboBox1.DataSource = dataSource;
            RadComboBox1.DataBind();
  
            RadComboBox2.DataSource = dataSource;
            RadComboBox2.DataBind();
        }
    }
}

The second approach is to perform data binding at the Init stage of the life cycle, because at that point the ViewState is still not applied.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
public partial class WebUserControl : System.Web.UI.UserControl
{
 
    protected void Page_Init(object sender, EventArgs e)
    {
        string[] dataSource = new string[] { "UnSelected", "First", "Second", "Third" };
        RadComboBox1.DataSource = dataSource;
        RadComboBox1.DataBind();
 
        RadComboBox2.DataSource = dataSource;
        RadComboBox2.DataBind();
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

Regards,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Richard
Top achievements
Rank 1
answered on 19 Jul 2012, 11:01 AM
Hi.

The problem is that we create nested usercontrols dynamically where the user can create one usercontrol containing a form with comboboxes, fill in the values, create one or more controls, and then save all of them.

This works with asp:DropDownList and with telerik:radComboBox from 2010 Q2.
We have to bind the control in Page_Load and we have to do it on every page load.

I would like to know what have changed in the ComboBox control that causes this.

Regards, Richard
0
Nencho
Telerik team
answered on 23 Jul 2012, 05:30 PM
Hello Richard,

If you perform data binding of the RadComboBoxes at the Page_Init event, PostBack will not cause any changes of the selected index, because ASP.NET loads its ViewState after Page_Init and before Page_Load. Please consider trying the aforementioned approach so you could persist the selection of each control at every PostBack.

Regards,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Richard
Top achievements
Rank 1
answered on 26 Jul 2012, 03:46 PM
Our current structure does not allow us to create the controls in the Page_Init event.
The controls must be created in the Page_Load event, and it cannot be in a if(!IsPostBack) statement because i create the controls on a button click, in a Post Back.

As I said below, the selected value is not overridden in the Telerik DLL from 2010 Q2.

What i want to know is what have been changed in the RadComboBox that overrides the selected value when the control databinds?

/Richard
0
Dimitar Terziev
Telerik team
answered on 01 Aug 2012, 02:51 PM
Hi Richard,

There have been a lot of bug fixes, improvements and new features added for the passed two years which might has caused a change in the behavior of the controls.

A simple solution of your problem would be to cache the selection before binding the control and applied it afterwards. Here is a sample implementation:
protected void Page_Load(object sender, EventArgs e)
   {
       var cachedSelectionFirstComboBox = RadComboBox1.SelectedValue;
       var cachedSelectionSecondComboBox = RadComboBox2.SelectedValue;
 
       string[] dataSource = new string[] { "UnSelected", "First", "Second", "Third" };
       RadComboBox1.DataSource = dataSource;
       RadComboBox1.DataBind();
 
       RadComboBox2.DataSource = dataSource;
       RadComboBox2.DataBind();
 
       RadComboBox1.SelectedValue = cachedSelectionFirstComboBox;
       RadComboBox2.SelectedValue = cachedSelectionSecondComboBox;
   }


All the best,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Justin
Top achievements
Rank 1
answered on 25 Oct 2012, 04:41 PM
I would consider this non-persistence of selectedvalue a bug.  I can't imagine what "bug" was fixed that warranted forcing your users to cache data and use hackish work-arounds to perform what is a simple and essential task.  Please fix this, it is causing your users to write more code than what the built in ASP.NET control requires.
0
Dimitar Terziev
Telerik team
answered on 30 Oct 2012, 11:41 AM
Hello Justin,

In general the selected value will be persisted when the control is being bound to the data source only once. When binding is performed the previous selection is cleared since you might be filling different data and therefore it is not valid to keep selection for something which might not be in the current data set. Since in this particular scenario the control is being bound upon every post back, this caching is necessary because the selection is cleared after the binding is performed. In case the binding is performed in Page_Init event thus the selection will be persisted since the ViewState is not applied yet.

Greetings,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Kevin
Top achievements
Rank 1
answered on 06 Nov 2012, 03:10 PM
Could you link us to a release change document that shows this change in behavior, and what version it was changed in?
0
Ruben Markosyan
Top achievements
Rank 1
answered on 15 Jan 2013, 10:13 AM
I am upgrading one of our projects from ver. 2011.1.519.35  to 2012.3.1205.35 and have the same problem. So the issue is still exist in the latest version.
0
Dimitar Terziev
Telerik team
answered on 18 Jan 2013, 08:25 AM
Hi Ruben,

Please ether bind the control in the Page_Init event or use the approach with the caching.

Greetings,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Martin Rouen
Top achievements
Rank 1
answered on 14 Apr 2015, 01:19 PM

Hi,

 

I have recently upgraded my Telerik controls from ver 2011.1.519 to 2014.3.1209 

We are generating dynamic controls for the form in On_Init() page lifecycle method, the DataBind() is performed before the Page_Load() method is called. Also, I have kept the EnableViewState property of RadComboBox to 'true', still my control loses values when there's a postback on the same page. 

 

Please suggest a remedy for this, reverting the Telerik dll version to old version resolves this issue, but I want to use the newer version without breaking this functionality.

0
Martin Rouen
Top achievements
Rank 1
answered on 20 Apr 2015, 06:19 AM

Hi,

 

I am still stuck with this issue. Can anyone please help me resolve it?

 

I am not able to figure out what's going wrong here because reverting the controls to older version resolves the issue. Its the upgrade which has introduced this bug in my application. 

0
Dimitar Terziev
Telerik team
answered on 22 Apr 2015, 07:15 AM
Hi,

I have prepared a sample page implementing a similar scenario to the describe one using the  2014.3.1209  version of our controls and the selection of the RadComboBox is correctly preserved across postbacks. You could find the sample attached.

Regards,
Dimitar Terziev
Telerik
 

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

 
0
Martin Rouen
Top achievements
Rank 1
answered on 30 Apr 2015, 12:55 PM

I have found a solution which helped me resolve this issue. Posting it here so that it may be of some use to the community:

 Adding OnClientLoad event handler for the combobox to update the clientstate helped me. Here's how to do it:

<telerik:RadComboBox ID="RadComboBox2" runat="server" Height="200px" Width="220px"    OnClientLoad="function (sender, args){sender.updateClientState();}" ></telerik:RadComboBox>

Tags
ComboBox
Asked by
Richard
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Richard
Top achievements
Rank 1
Dimitar Terziev
Telerik team
Justin
Top achievements
Rank 1
Kevin
Top achievements
Rank 1
Ruben Markosyan
Top achievements
Rank 1
Martin Rouen
Top achievements
Rank 1
Share this question
or