Creating own controls

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

Am after going through this tutorial at: http://www.developer.com/net/asp/article.php/3327181 that allows you to create your own Dropdown Calendar Control. Now I have got the thing working perfectly in that it does most of the things I want it to do. There is one little problem, in that when you click the forward or back button to move to a different month then the control considers the action as a OnClick() action and simple closes the calendar.

Could anyone suggest a way of getting around this problem

Mike55
 
Hi, i tried this example and it really has this problem. You can fix it by including "runat=server" in the div control. The problem with this is that if you look at the page's client code you'll see that the div's id has changed to usercontrolname_divCalendar. So the client script will not work this way.

My suggestion is to put the client script on the codebehind file and register it on the page. So I changed the code a little bit and its working fine.

On the codebehind
PHP:
private void Page_Load(object sender, System.EventArgs e)
{
	//client function
	System.Text.StringBuilder script = new System.Text.StringBuilder();
	script.Append("<script language='javascript'>");
	script.Append("function OnClick(){");
	script.Append("	if(document.getElementById('" + this.ID + "_divCalendar').style.display == 'none') ");
	script.Append("		document.getElementById('" + this.ID + "_divCalendar').style.display = ''; "); 
	script.Append("	else ");
	script.Append("		document.getElementById('" + this.ID + "_divCalendar').style.display = 'none'; ");
	script.Append(" } </script>");

	//Register the client script to the page
	Page.RegisterClientScriptBlock("CalendarScript", script.ToString());
			
}

private void Calendar1_SelectionChanged(object sender, EventArgs e)
{
	TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();
            System.Web.UI.Control div = this.FindControl("divCalendar");
	//hide the calendar
            if(div is HtmlGenericControl)
		((HtmlGenericControl) div).Style.Add("display", "none");
}

private void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
	System.Web.UI.Control div = this.FindControl("divCalendar");		//show the calendar				
	if(div is HtmlGenericControl)
		((HtmlGenericControl) div).Style.Add("display", "");			
}
 
Back
Top