Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Events fired by custom controls not caught

Reply
Thread Tools

Events fired by custom controls not caught

 
 
Richard Lionheart
Guest
Posts: n/a
 
      10-31-2005
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.MyWebUse rControl"
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;
}
}
}
}


 
Reply With Quote
 
 
 
 
raghu
Guest
Posts: n/a
 
      10-31-2005
Hi,

May be you have to catch the event like

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

hope this helps



Richard Lionheart wrote:
> 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.MyWebUse rControl"
> 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;
> }
> }
> }
> }


 
Reply With Quote
 
 
 
 
Richard Lionheart
Guest
Posts: n/a
 
      11-01-2005
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


 
Reply With Quote
 
raghu
Guest
Posts: n/a
 
      11-01-2005
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

Richard Lionheart wrote:
> 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


 
Reply With Quote
 
raghu
Guest
Posts: n/a
 
      11-01-2005
//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_SelectionChange d);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

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

 
Reply With Quote
 
raghu
Guest
Posts: n/a
 
      11-01-2005
you have to add handler for each and every control in
InitializeComponent() like (for calendar)

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

hope this helps
regards

 
Reply With Quote
 
raghu
Guest
Posts: n/a
 
      11-01-2005
you have to add handler for each and every control in
InitializeComponent() like (for calendar)

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

hope this helps
regards

 
Reply With Quote
 
Richard Lionheart
Guest
Posts: n/a
 
      11-03-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedded <divs> with events: How to prevent the parent div's eventfrom being fired when the embedded div's event is fired? Num GG Javascript 2 11-17-2008 08:56 PM
Events fired from dynamically created controls-VB.net ebown5@gmail.com ASP .Net Web Controls 5 08-02-2006 06:14 AM
Events not fired for Dynamically Created Controls in VS2005 ASP.NET 2.0 Richard Huang ASP .Net Web Controls 1 01-09-2006 03:16 AM
DVD Verdict reviews: COPS: SHOTS FIRED, COPS: CAUGHT IN THE ACT, and more! DVD Verdict DVD Video 0 04-15-2004 09:04 AM
How do I capture events fired in child user controls? Justin ASP .Net Web Controls 4 01-27-2004 09:51 PM



Advertisments