R
Richard Lionheart
Hi All
I'm following the tutorial at http://www.15seconds.com/issue/020319.htm to
build a custom control consisting of three built-in ASP.NET controls: a
textbox, button and calendar. The custom control builds correctly and
displays itself correctly in a parent webform.
However, the calendars SelectionChanged event is not caught by the
appropriated routine in the code-behind file and I can't figure out what
I've done wrong. Specifically, selecting a date in the calendar does not
result in the display of the date in the textbox. The code for the custom
control and its code-behind are presented below.
Any help in getting the event to be caught would be much appreciated.
--
Regards,
Richard
========= MyWebUserControl.aspx ============
<%@ Control Language="c#" AutoEventWireup="true"
Codebehind="MyWebUserControl.ascx.cs"
Inherits="TestUserControl_Calendar_15Secs.MyWebUserControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<asp:TextBox id="txtDate" runat="server"></asp:TextBox>
</td>
<td valign="top">
<asp:Button id="btnSelect" runat="server" Text="Button"></asp:Button>
</td>
</tr>
<tr>
<td valign="top" colspan="2">
<asp:Calendar id="calDate" runat="server"></asp:Calendar>
</td>
</tr>
</table>
======== MyWebUserControl.aspx.cs ============
namespace TestUserControl_Calendar_15Secs
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for MyWebUserControl.
/// </summary>
public abstract class MyWebUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox txtDate;
protected System.Web.UI.WebControls.Calendar calDate;
protected System.Web.UI.WebControls.Button btnSelect;
private void Page_Load(object sender, System.EventArgs e)
{
}
// Web Form Designer generated code
// Catches the event when a date is selected and inserts that
// date into the text box
protected void calDate_SelectionChanged(Object sender, EventArgs e)
{
System.DateTime dtDate = calDate.SelectedDate;
txtDate.Text = dtDate.ToShortDateString();
}
// Catches when the select button is clicked and hides or displays
// calendar control
private void btnSelect_Click(Object sender, System.EventArgs e)
{
if (calDate.Visible == false)
{
calDate.Visible = true;
}
else
{
calDate.Visible = false;
}
}
}
}
I'm following the tutorial at http://www.15seconds.com/issue/020319.htm to
build a custom control consisting of three built-in ASP.NET controls: a
textbox, button and calendar. The custom control builds correctly and
displays itself correctly in a parent webform.
However, the calendars SelectionChanged event is not caught by the
appropriated routine in the code-behind file and I can't figure out what
I've done wrong. Specifically, selecting a date in the calendar does not
result in the display of the date in the textbox. The code for the custom
control and its code-behind are presented below.
Any help in getting the event to be caught would be much appreciated.
--
Regards,
Richard
========= MyWebUserControl.aspx ============
<%@ Control Language="c#" AutoEventWireup="true"
Codebehind="MyWebUserControl.ascx.cs"
Inherits="TestUserControl_Calendar_15Secs.MyWebUserControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<asp:TextBox id="txtDate" runat="server"></asp:TextBox>
</td>
<td valign="top">
<asp:Button id="btnSelect" runat="server" Text="Button"></asp:Button>
</td>
</tr>
<tr>
<td valign="top" colspan="2">
<asp:Calendar id="calDate" runat="server"></asp:Calendar>
</td>
</tr>
</table>
======== MyWebUserControl.aspx.cs ============
namespace TestUserControl_Calendar_15Secs
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for MyWebUserControl.
/// </summary>
public abstract class MyWebUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox txtDate;
protected System.Web.UI.WebControls.Calendar calDate;
protected System.Web.UI.WebControls.Button btnSelect;
private void Page_Load(object sender, System.EventArgs e)
{
}
// Web Form Designer generated code
// Catches the event when a date is selected and inserts that
// date into the text box
protected void calDate_SelectionChanged(Object sender, EventArgs e)
{
System.DateTime dtDate = calDate.SelectedDate;
txtDate.Text = dtDate.ToShortDateString();
}
// Catches when the select button is clicked and hides or displays
// calendar control
private void btnSelect_Click(Object sender, System.EventArgs e)
{
if (calDate.Visible == false)
{
calDate.Visible = true;
}
else
{
calDate.Visible = false;
}
}
}
}