User control init event in C#

Discussion in 'ASP .Net Building Controls' started by cheloman12, Apr 18, 2006.

  1. cheloman12

    cheloman12 Guest

    Hi everybody
    I’m using user controls in a web application, and i’m triyng to make use of
    its init event.
    This is the sample code I’m using in the user control
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class Controls_MyControlC : System.Web.UI.UserControl
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void InitializeComponent()
    {
    this.Init += new System.EventHandler(this.Controls_MyControlC_Init);

    }

    private void Controls_MyControlC_Init(object sender, EventArgs e)
    {
    Session["UserID"] = 2;
    }
    }

    The problem is that neither init nor IntializaComponent events are never
    fired when MyControl runs in any page.

    But when I do the same in Visual Basic, the event occur so I can use it:
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Init
    Session("UserID") = 1
    End Sub

    What am I missing in the C# version. I’m really disconcerted.

    Thank you very much,

    Marcelo
     
    cheloman12, Apr 18, 2006
    #1
    1. Advertising

  2. using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class WebUserControl : System.Web.UI.UserControl
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
    // code here
    }
    }
    have a good day,
    Alessandro Zifiglio
    "cheloman12" <> ha scritto nel messaggio
    news:...
    > Hi everybody
    > I'm using user controls in a web application, and i'm triyng to make use
    > of
    > its init event.
    > This is the sample code I'm using in the user control
    > using System;
    > using System.Data;
    > using System.Configuration;
    > using System.Collections;
    > using System.Web;
    > using System.Web.Security;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.WebControls.WebParts;
    > using System.Web.UI.HtmlControls;
    >
    > public partial class Controls_MyControlC : System.Web.UI.UserControl
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    >
    > }
    >
    > private void InitializeComponent()
    > {
    > this.Init += new
    > System.EventHandler(this.Controls_MyControlC_Init);
    >
    > }
    >
    > private void Controls_MyControlC_Init(object sender, EventArgs e)
    > {
    > Session["UserID"] = 2;
    > }
    > }
    >
    > The problem is that neither init nor IntializaComponent events are never
    > fired when MyControl runs in any page.
    >
    > But when I do the same in Visual Basic, the event occur so I can use it:
    > Protected Sub Page_Init(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Init
    > Session("UserID") = 1
    > End Sub
    >
    > What am I missing in the C# version. I'm really disconcerted.
    >
    > Thank you very much,
    >
    > Marcelo
    >
     
    Alessandro Zifiglio, Apr 18, 2006
    #2
    1. Advertising

  3. Seems like you have missed 'Bubble Method'
    Events in ASP.NET follow the Bubble Model

    please try ASP.NET automatelly generated code:
    #region Web フォーム デザイナã§ç”Ÿæˆã•ã‚ŒãŸã‚³ãƒ¼ãƒ‰
    override protected void OnInit(EventArgs e)
    { InitializeComponent();
    base.OnInit(e); // be care of this
    }

    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
     
    Richard Troy-Rex, Apr 21, 2006
    #3
    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. jorge
    Replies:
    2
    Views:
    1,339
    jorge
    May 26, 2004
  2. Tony Cheng
    Replies:
    1
    Views:
    8,305
    Juan T. Llibre
    Feb 24, 2006
  3. Replies:
    1
    Views:
    692
    Jules
    Aug 18, 2005
  4. Jess
    Replies:
    4
    Views:
    468
  5. news.aon.at
    Replies:
    11
    Views:
    673
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page