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">
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
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
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
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
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
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
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
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
Please ether bind the control in the Page_Init event or use the approach with the caching.
Greetings,
Dimitar Terziev
the Telerik team
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.
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.
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.
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>