Events fired by custom controls not caught

Discussion in 'ASP .Net Web Controls' started by Richard Lionheart, Oct 31, 2005.

  1. 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;
    }
    }
    }
    }
    Richard Lionheart, Oct 31, 2005
    #1
    1. Advertising

  2. Richard Lionheart

    raghu Guest

    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.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;
    > }
    > }
    > }
    > }
    raghu, Oct 31, 2005
    #2
    1. Advertising

  3. 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
    Richard Lionheart, Nov 1, 2005
    #3
  4. Richard Lionheart

    raghu Guest

    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
    raghu, Nov 1, 2005
    #4
  5. Richard Lionheart

    raghu Guest

    //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!";
    }
    }
    }
    raghu, Nov 1, 2005
    #5
  6. Richard Lionheart

    raghu Guest

    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
    raghu, Nov 1, 2005
    #6
  7. Richard Lionheart

    raghu Guest

    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
    raghu, Nov 1, 2005
    #7
  8. 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
    Richard Lionheart, Nov 3, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SammyBar
    Replies:
    1
    Views:
    598
    Erjan Gavalji
    Jul 21, 2009
  2. Justin

    How do I capture events fired in child user controls?

    Justin, Jan 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    235
    Justin
    Jan 27, 2004
  3. Richard Huang
    Replies:
    1
    Views:
    133
  4. Replies:
    5
    Views:
    188
    Alessandro Zifiglio
    Aug 2, 2006
  5. SammyBar

    Custom event from a custom web control is fired twice

    SammyBar, Jul 14, 2009, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    939
    Erjan Gavalji
    Jul 21, 2009
Loading...

Share This Page