ASP.NET: Page.Init woudln't fire - asp.net

I have a custom ASP.NET control. In its Init handler I add a delegate to the Page's Init like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if(someCondition())
{
this.Page.Init += delegate(object sender, EventArgs ee)
{
//some stuff
};
}
}
Now, if I add this custom control to the HTML of the page declaratively, every thing works fine, the Page's Init delegate gets called. But if I add this control to the page programmatically like:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
MyControl myControl = new MyControl { ID = "myControl" };
this.Page.Form.Controls.Add(myControl);
}
The Init if the control get's called but the delegate that I attached to the Page.Init does not. What am I doing wrong here?

IT's because when adding the controls in OnLoad the Page's Init has already executed

Move the declaration of your custom control to page's OnInit instead of OnLoad. Instantiate your control and add it to the form before calling base.OnInit(e). This will give the page chance to load your control and actually attach your delegate to page's Init event before the Init gets called by the ASP.NET run time. Your problem is that page's Init was already called when your control's Init gets executed.

Instead of adding control in PageLoad, add it in Page_PreInit event handler.

Related

asp.net web forms constructor?

I want to load a dictionary with some data and use it in the button event handler of a web form. Where is the appropriate place to put this code? There is a Page_Load method, but I don't want it to run every time the page loads.
in a Page Load event handler, call the method only once when the page first loads.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// your code to load dictionary here, or a method call.
}
}

.NET Public Events not wiring up in nested web user controls

I have C# Web Application that has an aspx page hosting a user control (Review.ascx). Inside that user control there are 5 more user controls, one of which has a public event (Review_Summary.ascx). The problem is no matter what i do I cannot get the event wired up in the parent ascx control (Review.ascx).
Here is what I have in the child control (Review_Summary.ascx)
public event EventHandler forwardStatusChanged;
#region methods
protected void btnForward_Click(object sender, EventArgs e)
{
if (btnForward.Text == "Return")
{
if (forwardStatusChanged != null)
{
forwardStatusChanged(sender, e);
}
removeForward();
}
}
In the parent control (Review.ascx) I have this
public void initReview(string EmployeeNumber)
{
RevSummary.forwardStatusChanged += new EventHandler(RevSummary_forwardStatusChanged);
<more code here>
}
protected void RevSummary_forwardStatusChanged(object sender, EventArgs e)
{
lblReadOnly.Visible = false;
}
RevSummary is the ID of the child control in the parent control. InitReveiw is a method that is called by the aspx page in its Page_Load event.
I get no errors on compile or at runtime. But when I click the button the forwardStatusChanged event is null. The "removeForward()" method that is called after that executes properly. So that fact that the event is always null leads me to believe that the wire up in the parent control is not working. However, I am sure it is executing becasue all of the code after that executes.
How can I figure out why this event is not wiring up?
Where is initReview being called from? Are you sure it's being called because the only reason this happens is that the event handler wasn't truly setup. I've never found a reason other than this, the several times I did this myself.
HTH.

asp.net page's preinit event

I am new to asp.net. I have an aspx page and i have to write some code in its PreInit event.
From where i find PreInit event on the page.
As we double click on button to get button click event(or selecting button and select event from property pane)
Plz reply me ASAP.
Man, why do you need the mouse?
If you need to write some code into PreInit just write the code:
protected virtual void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
//your code
}
or in class constructor add a event handler for it:
...
PreInit += new EventHandler(SomeMethodName)
...
and define the event handler method
private void SomeMethodName(object sender, EventArgs e)
{
//your code
}
And by the way, check a .Net Framework book and a Visual Studio manual.
You need to do some reading:
http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events

How to subscribe to page events in Web Application Page

I am migrating from ASP.NET 1.1 and I used to subscribe to page events using designer which adds subscriptions to InitializeComponent method on the page.
In the ASP.NET 2.0, how can I subscribe to Page events from the designer?
Not using designer this seems to work.
protected void Page_Init(object sender, EventArgs e) {
// The code, no safety here, just convention
}
// OR
protected override void OnInit(EventArgs e) {
base.OnInit(e);
// The code
}
What is the recommended way of subscribing to page events? Same question applies to the MasterPage.
Additionally what is the best way to subscribe to events on the UserControl? When I declare the Page_XXX on the UserControl the event gets called many times.
Pages support auto event-wire-up, meaning that ASP.NET looks for methods with particular names and automatically runs those methods when certain events are raised. If the AutoEventWireUp attribute of the Page directive is set to true (bydefault it is true - asp.net 2.0), page events are automatically bound to methods that use the naming convention of Page_Event, such as Page_Load, Page_PreInit, Page_Init etc.
void Page_PreInit(){
}
void Page_Init(){
}
EDIT:
#Dmitriy : So only the method name matters?
Yes
#Dmitriy : Also how do you subscribe to events from designer?
You can't subscribe page events through designer.
See this example: How to add event handler for page events if AutoEventWireUp is false?
public _Default() //Constructor
{
this.Load += new EventHandler(_Default_Load);
}
void _Default_Load(object sender, EventArgs e)
{
}

Can I bubble up event from Master page to ASPX

Can I bubble up a button click event of a button in master page to be handled by an event handler in the aspx page ?
You can expose the event handler and hookup to it, like this:
In the master:
public event EventHandler ButtonClick
{
add { ButtonThatGetsClicked.Click += value; }
remove { ButtonThatGetsClicked.Click -= value; }
}
In the page:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
((MyMasterType)Master).ButtonClick += MyHandler;
}
private void MyHandler(object sender, EventArgs e)
{
//Do Something
}
Also, you can avoid the Master type cast and have it already appear in intellisense as your Master's type by using the #MasterType directive in the aspx markup.
You can rebroadcast the event. Declare a new corresponding event in your master page, such as HelpClicked and then aspx pages that use this master can subscribe to the event and handle it appropriately. The master can also take a default action if there are no subscribers (or use an EventArgs with a Handled property or something like that).

Resources