Events fired by custom controls not caught

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;
}
}
}
}
 
R

raghu

Hi,

May be you have to catch the event like

protected void calDate_SelectionChanged(Object sender, EventArgs e)
handles calDate.SelectionChanged {}

hope this helps
 
R

Richard Lionheart

Hi Raghu,

Thanks for responding to my question.
May be you have to catch the event like

protected void calDate_SelectionChanged(Object sender, EventArgs e)
handles calDate.SelectionChanged {}

When I changed
protected void calDate_SelectionChanged(Object sender, EventArgs e) {}
to
protected void calDate_SelectionChanged(Object sender, EventArgs e)
handles calDate.SelectionChanged {}
VS.NET flagged the "handles" token and suggested that a sem-colon was
expected.

When I first encountered this problem, I thought the cause might be
AutoEventWireup="false" in the Control header at the top of the ascx file.
But changing it from false to true accomplished nothing.

So I'm still stuck.

Again, thanks for the feedback,

Regards,
Richard
 
R

raghu

Hi Richard,

Sorry for that. Actually you donot have to include "handles" in c#.

You just have to make sure that the control is declared in that
particular class before using it.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace demoCSharp
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.TextBox textBox1;

private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

static void Main()
{
Application.Run(new Form1());
}
private void dateTimePicker1_ValueChanged(object sender,
System.EventArgs e)
{

//write your code here ....
textBox1.Text = "Hello!";

}
}
}

works for me. hope this helps.

thanks
 
R

raghu

//this is the actual c# application. sorry for the mess. I am kinda
into vb right now.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace csharpdemo
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Calendar Calendar1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Calendar1.SelectionChanged += new
System.EventHandler(this.Calendar1_SelectionChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Calendar1_SelectionChanged(object sender,
System.EventArgs e)
{
TextBox1.Text = "Hello!";
}
}
}
 
R

raghu

you have to add handler for each and every control in
InitializeComponent() like (for calendar)

this.Calendar1.SelectionChanged += new
System.EventHandler(this.Calendar1_SelectionChanged);
this.Load += new
System.EventHandler(this.Page_Load);

hope this helps
regards
 
R

raghu

you have to add handler for each and every control in
InitializeComponent() like (for calendar)

this.Calendar1.SelectionChanged += new
System.EventHandler(this.Calendar1_SelectionChanged);
this.Load += new
System.EventHandler(this.Page_Load);

hope this helps
regards
 
R

Richard Lionheart

Hi Raghu,

Thanks for all the info you supplied. I'm got distracted with some other
problems, but I'm ready to tackle this again. I'll post again when I have
some result or an additional question.

Regards,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top