DatePicker.Clear() doesn't seem to work

7 posts, 1 answers
  1. Phil
    Phil avatar
    13 posts
    Member since:
    Jun 2009

    Posted 21 Aug 2009 Link to this post

    Hi,

    I'm using the RadDatePicker object for its client side functionality but not interested in the server side. I have it on a page where ViewState is switched off, being created dynamically inside a repeater.

    Once a form is submitted successfully, I want the entire form cleared so that another new record can be added. However I am hitting a problem with DatePicker, DateTimePicker, and TimePicker that I cannot seem to resolve.

    Here is the code I am using to create the object (there is a placeholder inside a Repeater item and this is being called at Repeater_OnItemDataBound)

    private void DoDatePicker(FormColumn column, PlaceHolder ph)
            {
                var picker = new RadDatePicker {ID = column.FieldName};
                
                ph.Controls.AddAt(0, picker);
                picker.Clear();
            }

    I've tried putting the picker.Clear() before and after the AddAt statement but regardless of what I set, the date from the previous form is still being displayed. I've also tried picker.SelectedDate = null, that didn't work either.

    What do I need to do to clear the previous date, short of hacking into HttpContext.Current.Request and deleting all the form values connected to the Date/TimePicker?

    Thanks for any assistance.
  2. Mira
    Admin
    Mira avatar
    1124 posts

    Posted 24 Aug 2009 Link to this post

    Hi Phil,

    I suggest that you modify your code in the following way to clear the entire placeholder controls before adding the RadDatePicker:
    private void DoDatePicker(FormColumn column, PlaceHolder ph) 
            { 
                ph.Controls.Clear(); 
                var picker = new RadDatePicker {ID = column.FieldName}; 
                ph.Controls.AddAt(0, picker); 
            } 
     

    Please let me know whether it helps.

    Best wishes,
    Mira
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Phil
    Phil avatar
    13 posts
    Member since:
    Jun 2009

    Posted 25 Aug 2009 Link to this post

    Hi Mira

    That won't work, in fact there is no way the controls can persist because ViewState has been switched off. The problem is I believe in the Request.Form variables that the DatePicker is creating. When I submit a form, the following parameters are being passed via the form:

    "_rptAddForm$ctl02$dteDate"
    "_rptAddForm_ctl02_dteDate_dateInput_text"
    "_rptAddForm$ctl02$dteDate$dateInput"
    "_rptAddForm_ctl02_dteDate_dateInput_ClientState"
    "_rptAddForm_ctl02_dteDate_calendar_SD"
    "_rptAddForm_ctl02_dteDate_calendar_AD"
    "_rptAddForm_ctl02_dteDate_ClientState"

    I understand that these variables must be necessary for the control, but I can't understand why when I clear the picker that the variables aren't overwritten with blank values. The only way I can clear these is with hacking into the Request.Form object and removing these values from the form. I'd prefer not to do it that way if there's any way I can do this using the picker.
  4. Mira
    Admin
    Mira avatar
    1124 posts

    Posted 28 Aug 2009 Link to this post

    Hello Phil,

    I am attaching a sample project following your scenario - it is a page with ViewState disabled and on it there are a PlaceHolder, a button and a RadDatePicker inside the PlaceHolder. When the button is clicked, the following method is called
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            var picker = new RadDatePicker { ID = "RadDatePicker1" }; 
            PlaceHolder1.Controls.Clear(); 
            PlaceHolder1.Controls.AddAt(0, picker); 
        } 
    and the RadDatePicker is cleared properly on my side.

    Please tell me is this the functionality you want or am I missing something out?

    Sincerely yours,
    Mira
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Phil
    Phil avatar
    13 posts
    Member since:
    Jun 2009

    Posted 28 Aug 2009 Link to this post

    Hi Mira,

    I can't seem to attach files myself but I can explain how to change your file to recreate the functionality (and recreates the error as I have attempted this using your code as a base). Note that the repeater appears to be important.

    First, remove the telerik.Web.UI assembly from the web.config assemblies section.

    Second, this is the code for the aspx page:

    <%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
     
    <!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"
        <div> 
     
            <asp:ScriptManager ID="ScriptManager1" runat="server"
            </asp:ScriptManager> 
             
            <asp:Repeater ID="_rptTest" runat="server" OnItemDataBound="TestDataBound"
                <ItemTemplate> 
                    <asp:PlaceHolder ID="_ph" runat="server"
     
                    </asp:PlaceHolder> 
                </ItemTemplate> 
            </asp:Repeater> 
             
             
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
             
        </div> 
        </form> 
    </body> 
    </html> 

    Thirdly, this is the code behind file.

    public partial class _Default : System.Web.UI.Page 
        
        protected void Page_Load(object sender, EventArgs e) 
        { 
            var rptload = new int[] {1}; 
            _rptTest.DataSource = rptload; 
            _rptTest.DataBind(); 
        } 
     
     
        protected void Button1_Click(object sender, EventArgs e) 
        { 
             
        } 
     
        protected void TestDataBound(object sender, RepeaterItemEventArgs e) 
        { 
            var ph = e.Item.FindControl("_ph"); 
     
            var picker = new RadDatePicker { ID = "RadDatePicker1" }; 
            picker.Clear(); 
            ph.Controls.Clear(); 
             
            ph.Controls.AddAt(0, picker); 
        } 


    This fairly recreates the circumstance I am experiencing. Incidentally I am using Q1 2009, if Q2 2009 has got rid of this I will upgrade to that.

    Thanks

    Phil

  6. Answer
    Mira
    Admin
    Mira avatar
    1124 posts

    Posted 02 Sep 2009 Link to this post

    Hi Phil,

    I recommend that you set EnableViewState to true - otherwise neither RadDatePicker, nor any other control is cleared after postback - please see the attached project with a RadDatePicker and a TextBox in a Repeater's item template.

    All the best,
    Mira
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. Phil
    Phil avatar
    13 posts
    Member since:
    Jun 2009

    Posted 02 Sep 2009 Link to this post

    I have to disable viewstate for the placeholder and enable for the page. Interestingly this does not happen with my bespoke user controls!

    It looks like it's a weird bug with repeaters, apologies and thanks for your time.


Back to Top