all groups > asp.net webcontrols > october 2005 >
You're in the

asp.net webcontrols

group:

Events fired by custom controls not caught


Events fired by custom controls not caught Richard Lionheart
10/31/2005 12:43:00 AM
asp.net webcontrols:
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;
}
}
}
}

Re: Events fired by custom controls not caught raghu
10/31/2005 10:57:41 AM
Hi,

May be you have to catch the event like

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

hope this helps



[quoted text, click to view]
Re: Events fired by custom controls not caught Richard Lionheart
11/1/2005 1:48:01 AM
Hi Raghu,

Thanks for responding to my question.

[quoted text, click to view]

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

Re: Events fired by custom controls not caught raghu
11/1/2005 11:35:04 AM
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

[quoted text, click to view]
Re: Events fired by custom controls not caught raghu
11/1/2005 11:42:42 AM
//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!";
}
}
}
Re: Events fired by custom controls not caught raghu
11/1/2005 11:45:16 AM
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
Re: Events fired by custom controls not caught raghu
11/1/2005 11:45:20 AM
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
Re: Events fired by custom controls not caught Richard Lionheart
11/3/2005 5:05:05 PM
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

AddThis Social Bookmark Button