Problem with RadDatePicker in dynamically loaded user control.

5 posts, 0 answers
  1. Scott R
    Scott R avatar
    63 posts
    Member since:
    Jun 2008

    Posted 17 Jul 2008 Link to this post

    I'm having a problem with the RadDatePicker when it is used inside a dynamically loaded user control. Also, the dynamically loaded user control is displayed inside a RadPageView.

    The problem is this: I set the initial value for the RadDatePicker in server-side code when the user control is first created. The initial value displays correctly when the page is displayed. However, once a postback occurs the value is "lost" (i.e. the RadDatePicker is blank). If you type in a date or select a date using the calendar then the value is persisted across postbacks. It's only the initial value, set on the server side when the user control is created, that is not persisted across postbacks.

    Here is some sample code. First, the user control:

    WebUserControl.ascx
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %> 
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    <telerik:RadDatePicker ID="RadDatePicker1" runat="server">  
    </telerik:RadDatePicker> 
     

    WebUserControl.ascx.cs
    public partial class WebUserControl : System.Web.UI.UserControl  
    {  
        public void DisplayInformation()  
        {  
            RadDatePicker1.SelectedDate = DateTime.Now.Date;  
        }  

    Default.aspx
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %> 
     
    <!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>Untitled Page</title> 
    </head> 
    <body> 
        <form id="form1" runat="server">  
        <div> 
            <asp:ScriptManager ID="ScriptManager1" runat="server">  
            </asp:ScriptManager> 
            <telerik:RadMultiPage ID="RadMultiPage1" runat="server" OnPageViewCreated="RadMultiPage1_PageViewCreated">  
            </telerik:RadMultiPage> 
            <asp:Button ID="Button2" runat="server" Text="Create Page View" OnClick="Button2_Click" /> 
            <asp:Button ID="Button1" runat="server" Text="PostBack" /> 
        </div> 
        </form> 
    </body> 
    </html> 
     

    Default.aspx.cs
    public partial class _Default : System.Web.UI.Page   
    {  
        bool DoDisplay = false;  
     
        protected void Button2_Click(object sender, EventArgs e)  
        {  
            // Display initial values the first time the user  
            // control is created.  
            DoDisplay = true;  
     
            RadPageView pv = new RadPageView();  
            pv.ID = "RadPageView1";  
            RadMultiPage1.PageViews.Add(pv);  
        }  
     
        protected void RadMultiPage1_PageViewCreated(object sender, Telerik.Web.UI.RadMultiPageEventArgs e)  
        {  
            WebUserControl wuc = Page.LoadControl("WebUserControl.ascx") as WebUserControl;  
            wuc.ID = "WebUserControl1";  
            e.PageView.Selected = true;  
     
            // Only display initial values the first time the  
            // user control is created. On subsequent postbacks  
            // we want the value from the viewstate.  
            if (DoDisplay)  
                wuc.DisplayInformation();  
     
            e.PageView.Controls.Add(wuc);  
        }  
     
  2. Scott R
    Scott R avatar
    63 posts
    Member since:
    Jun 2008

    Posted 17 Jul 2008 Link to this post

    By the way, if you set the ID of the user control to a Guid the RadDatePicker will throw javascript errors. I think the user control ID must start with a letter and not contain any dashes. Is this a general rule for naming HTML controls? It doesn't seem to break anything except the RadDatePicker.

    Change

    wuc.ID = "WebUserControl1";

    to

    wuc.ID = Guid.NewGuid().ToString();

  3. Plamen Peshev
    Admin
    Plamen Peshev avatar
    885 posts

    Posted 21 Jul 2008 Link to this post

    Hello Scott R,

    Thank you for the question.

    The ID property uniquely identifies a server control. Only combinations of alphanumeric characters and the underscore character ( _ ) are valid values for this property.


    Kind regards,
    Plamen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  4. Scott R
    Scott R avatar
    63 posts
    Member since:
    Jun 2008

    Posted 21 Jul 2008 Link to this post

    Thanks. Interestingly, the only control that had problems with the Guid "ID" was the RadDatePicker.

    Regardless, that problem was easy to fix. The larger problem is the loss of the selected date when a postback occurs. Any help with that situation is greatly appreciated.
  5. Scott R
    Scott R avatar
    63 posts
    Member since:
    Jun 2008

    Posted 22 Jul 2008 Link to this post

    Just in case anyone else comes across this problem, I will post the answer I got to my support ticket.

    Unlike most other controls, the RadDatePicker relies on viewstate in order to maintain its selected value. Since viewstate is not tracked on controls until they are part of the page, the answer is to simply add the user control to the page before setting the selected date property. So, in my case, it's like this:

       protected void RadMultiPage1_PageViewCreated(object sender, Telerik.Web.UI.RadMultiPageEventArgs e)     
        {     
            WebUserControl wuc = Page.LoadControl("WebUserControl.ascx") as WebUserControl;     
     
            // Add the user control to the page view early so that viewstate is tracked  
            // on its child controls.  
            e.PageView.Controls.Add(wuc);     
     
            wuc.ID = "WebUserControl1";     
            e.PageView.Selected = true;     
        
            // Only display initial values the first time the     
            // user control is created. On subsequent postbacks     
            // we want the value from the viewstate.     
            if (DoDisplay)     
                wuc.DisplayInformation();     
          }    
     

    Simple fix. Thanks, Plamen!
Back to Top