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

DatePicker.Clear() doesn't seem to work

6 Answers 111 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 21 Aug 2009, 03:43 PM
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.

6 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 24 Aug 2009, 03:13 PM
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.
0
Phil
Top achievements
Rank 1
answered on 25 Aug 2009, 12:42 PM
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.
0
Mira
Telerik team
answered on 28 Aug 2009, 11:04 AM
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.
0
Phil
Top achievements
Rank 1
answered on 28 Aug 2009, 12:10 PM
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

0
Accepted
Mira
Telerik team
answered on 02 Sep 2009, 01:01 PM
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.
0
Phil
Top achievements
Rank 1
answered on 02 Sep 2009, 01:42 PM
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.


Tags
Calendar
Asked by
Phil
Top achievements
Rank 1
Answers by
Mira
Telerik team
Phil
Top achievements
Rank 1
Share this question
or