User control init event in C#

C

cheloman12

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
 
A

Alessandro Zifiglio

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
 
R

Richard Troy-Rex

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
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top