SelectedValue lost on postback in dynamically added user controls

15 posts, 0 answers
  1. Richard
    Richard avatar
    11 posts
    Member since:
    Jul 2012

    Posted 29 Jun 2012 Link to this post

    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();
        }
    }
  2. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 02 Jul 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Richard
    Richard avatar
    11 posts
    Member since:
    Jul 2012

    Posted 19 Jul 2012 Link to this post

    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
  5. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 23 Jul 2012 Link to this post

    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.
  6. Richard
    Richard avatar
    11 posts
    Member since:
    Jul 2012

    Posted 26 Jul 2012 Link to this post

    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
  7. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 01 Aug 2012 Link to this post

    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.
  8. Justin
    Justin avatar
    4 posts
    Member since:
    Sep 2012

    Posted 25 Oct 2012 Link to this post

    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.
  9. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 30 Oct 2012 Link to this post

    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.
  10. Kevin
    Kevin avatar
    2 posts
    Member since:
    Oct 2012

    Posted 06 Nov 2012 Link to this post

    Could you link us to a release change document that shows this change in behavior, and what version it was changed in?
  11. Ruben Markosyan
    Ruben Markosyan avatar
    7 posts
    Member since:
    Apr 2010

    Posted 15 Jan 2013 Link to this post

    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.
  12. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 18 Jan 2013 Link to this post

    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.
  13. Martin Rouen
    Martin  Rouen avatar
    3 posts
    Member since:
    Jul 2012

    Posted 14 Apr 2015 Link to this post

    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.

  14. Martin Rouen
    Martin  Rouen avatar
    3 posts
    Member since:
    Jul 2012

    Posted 20 Apr 2015 Link to this post

    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. 

  15. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 22 Apr 2015 Link to this post

    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.

     
  16. Martin Rouen
    Martin  Rouen avatar
    3 posts
    Member since:
    Jul 2012

    Posted 30 Apr 2015 Link to this post

    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>

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017