Sunday, March 11, 2012

CalendarExtender and setting TextBox.Text to Date

Unfortunately, the calendar extender is a bit picky about formats... Try using a more explicit format such as MM/dd/yyyy... should work a treat then!!

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { txtDate.Text = DateTime.Today.ToString("MM/dd/yyyy"); } }</script><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> <asp:TextBox ID="txtDate" CssClass="TextBox" runat="server"></asp:TextBox> <asp:Image ID="imgCal" runat="server" style="margin-left: 5px;"/> <ajaxtoolkit:CalendarExtender id="ctrlCalendar" TargetControlID="txtDate" Format="MM/dd/yyyy" PopupButtonID="imgCal" runat="server"> </ajaxtoolkit:CalendarExtender> </div> </form></body></html>

thanks very much

it works :)


I've tried your code but still get a javascript error. I have the TextBox and CalendarExtender inside a UserControl, so I don't know if that is the issue.

What's the javascript error?


Ok, I took it out of the User Control and put it into a page by itself. It's still not working. Here's my code. What am I doing wrong?

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="ManagerAtAGlance.aspx.cs" Inherits="ManagerAtAGlance" Culture="auto" UICulture="auto" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="scriptManager" runat="server" /> <asp:TextBox ID="txtDate" runat="server"></asp:TextBox><ajaxtoolkit:CalendarExtender ID="ctrlCalExt" runat="server" Format="MM/dd/yyyy" TargetControlID="txtDate"></ajaxtoolkit:CalendarExtender> </form></body></html>

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Collections.Generic;public partialclass ManagerAtAGlance : System.Web.UI.Page{protected new void Page_Load(object sender, EventArgs e) {if (!IsPostBack) { txtDate.Text = DateTime.Today.ToString("MM/dd/yyyy"); } }}

You say you've tried my code, but then start mentioning a UserControl... so which is it??

a) You've tried my code AS-IS and it still doesn't work or

b) You've amended my code, put it inside a UserControl and then it doesn't work?

If it's a) then I would suggest that you need to download the latest version of the toolkit and try again.

If it's b) then it could either that you stilll need to download the latest version of the toolkit, or that you've come across the toolkit bug where you can't put the textbox inside <p></p> tags.

Check outhttp://forums.asp.net/thread/1570444.aspx and http://www.codeplex.com/AtlasControlToolkit/WorkItem/View.aspx?WorkItemId=7697 for details!


Just some more information. If I clear out what's in the textbox when the page loads, remove focus from the textbox, then re-focus the textbox, the calendar pops up and works normally.
I've tried it both ways, neither work. I posted my code above. I have the latest version of AJAX and the toolkit. Not sure what the problem could be, it has to be a configuration problem, but the toolkit DLL is up to date and I just re-installed AJAX to make sure it's the latest version.

After rebuilding my solution it works now.

I think the problem was having the newest version of the Toolkit and not having the newest version of the AJAX Framework.

Thanks for your help!


Hi Rob,

Looks like our posts crossed! I've taken your code and it works fine for me!!

I know you've said that you've got the latest version of the toolkit... but are you sure!! The latest version was released on 1st February, which patched the initial RTM, fixing several bugs in the calendar extender.

If you're running this under IIS, trying doing an iisreset... an old version of the toolkit might be cached in there!


,hi,,

another problem occured here,, after I set the date from the database,, I have a "Change Date" Button ,, when I choose another date from the calendar,, and press change date,, ALWAYS it takes the prevous value ( which is set in the page load) ,,, so I coudn't change the date in the DB,,,,

:(


any help please !

Sorry, but I just don't understand your question. I would suggest that you raise a new post and include some code/markup so people can understand what you are trying to do.

No comments:

Post a Comment