Is there an after Page_Load event in ASP.net - asp.net

Is there an event that is triggered after all Page_Load events have completed?
How can i have more than one Page_Load?
When you have user controls.
Before my page can render, i need my page (and all embedded controls) to have initialized themselves by completing their Page_Load events.
The problem, of course, is that if i put code in my page's Page_Load handler:
MyPage.aspx
--> Page_Load
---> DoSomethingWithUserControl()
UserControl1.ascx
--> Page_Load
---> initialize ourselves now that viewstate has been restored
then i begin to access my UserControl1 control before it is ready.
i need a way to run code after all Page_Load events have fired, but before any postback events (e.g. Click events) have fired:
MyPage.aspx
--> Page_Load
UserControl1.ascx
--> Page_Load
---> initialize ourselves now that viewstate has been restored
MyPage.aspx
--> Page_AfterLoad
---> DoSomethingWithUserControl()
Looking at the page lifecycle in MSDN it looks like there is no way to raise an event after all Page_Loads have been completed:
Is there a way to raise an after after all Page_Loads have completed?

Page_LoadComplete is the event that is raised after all controls have been loaded
Remember that the Init event is first triggered by all child controls, and just when all controls have been initialized, the Init event of the page is raised. The Load event works the other way around, the page first raises the Load event and then each child control raises its own Load event. At the end the LoadComplete is raised. Note that this is true only when the controls are created at design time, when the controls are created dynamically they (sadly) do not follow this approach strictly.
From MSDN:
If controls are created dynamically at run time or declaratively within templates of data-bound controls, their events are initially not synchronized with those of other controls on the page. For example, for a control that is added at run time, the Init and Load events might occur much later in the page life cycle than the same events for controls created declaratively. Therefore, from the time that they are instantiated, dynamically added controls and controls in templates raise their events one after the other until they have caught up to the event during which it was added to the Controls collection.
Take a look:
(source: http://msdn.microsoft.com/en-us/library/ms178472.aspx)
Edit 1
In order to fulfill all your requirements:
i need a way to run code after all Page_Load events have fired, but before any postback events (e.g. Click events) have fired:
I think the easiest way is to declare a custom event in the User Control and fire it after the control has been loaded, then jus subscribe to that event in your ASPX
User Control
public event Action LoadCompleted = delegate { };
protected void Page_Load(object sender, EventArgs e)
{
this.LoadCompleted();
}
ASPX page
protected void Page_Load(object sender, EventArgs e)
{
this.myUserControl.LoadCompleted += () =>
{
// do somethign interesting
this.lblMessage.Text = DateTime.Now.ToString();
};
}

It seems that there really is no way to reliably get your code to execute between the time when all (statically declared, see Jupaols comment about dynamically added) controls are loaded and the time when their postback events are raised.
I even went so far as to disassemble the Page class using reflector and as far as I could tell, the page isn't calling any methods that you could tap into between these times.
So for the general case it seems like your just out of luck.
But your statement
then i begin to access my UserControl1 control before it is ready
is questionable. By the time the Page_Load method is called, the viewstate and control state have been set on all controls (at least the statically declared ones). So unless you have some more state initialization going on in the OnLoad method of a control, the control is ready to be tinkered with. If you DO have something going on in OnLoad, then why not do the tinkering there?

Related

Using Page_Load and Page_PreRender in ASP.Net

I see some people are using Page_Load and Page_PreRender in same aspx page. Can I exactly know why do we need to invoke both the methods in same asp.net page?
Please see the code below,
protected void Page_Load(object sender, EventArgs e)
{
try
{
dprPager.ButtonClickPager += new EventHandler(dprPager_ButtonClickPager);
if (!Page.IsPostBack)
{
InitPager();
}
}
catch (Exception ex)
{
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
erMsg.Visible = !string.IsNullOrEmpty(lblError.Text);
}
The major difference between Page_Load and Page_PreRender is that in the Page_Load method not all of your page controls are completely initialized (loaded), because individual controls Load() methods has not been called yet. This means that tree is not ready for rendering yet. In Page_PreRender you guaranteed that all page controls are loaded and ready for rendering. Technically Page_PreRender is your last chance to tweak the page before it turns into HTML stream.
It depends on your requirements.
Page Load : Perform actions common to all requests, such as setting up a database query. At this point, server controls in the tree are created and initialized, the state is restored, and form controls reflect client-side data. See Handling Inherited Events.
Prerender :Perform any updates before the output is rendered. Any changes made to the state of the control in the prerender phase can be saved, while changes made in the rendering phase are lost. See Handling Inherited Events.
Reference: Control Execution Lifecycle MSDN
Try to read about
ASP.NET Page Life Cycle Overview ASP.NET
Control Execution Lifecycle
Regards
Page_Load happens after ViewState and PostData is sent into all of your server side controls by ASP.NET controls being created on the page. Page_Init is the event fired prior to ViewState and PostData being reinstated. Page_Load is where you typically do any page wide initilization. Page_PreRender is the last event you have a chance to handle prior to the page's state being rendered into HTML. Page_Load
is the more typical event to work with.
Well a big requirement to implement PreRender as opposed to Load is the need to work with the controls on the page. On Page_Load, the controls are not rendered, and therefore cannot be referenced.
Processing the ASP.NET web-form takes place in stages. At each state various events are raised. If you are interested to plug your code into the processing flow (on server side) then you have to handle appropriate page event.
The main point of the differences as pointed out #BizApps is that Load event happens right after the ViewState is populated while PreRender event happens later, right before Rendering phase, and after all individual children controls' action event handlers are already executing.
Therefore, any modifications done by the controls' actions event handler should be updated in the control hierarchy during PreRender as it happens after.

se the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation

guys, I have a usercontrol in my asp.net 3.5 application and I am passing some plain text on button click event. button is situated in the usercontrol. but when I fire the event, I am getting the following error;
Invalid postback or callback argument.
Event validation is enabled using
in configuration or <%# Page
EnableEventValidation="true" %> in a
page. For security purposes, this
feature verifies that arguments to
postback or callback events originate
from the server control that
originally rendered them. If the data
is valid and expected, use the
ClientScriptManager.RegisterForEventValidation
method in order to register the
postback or callback data for
validation.
when I set the EnableEventValidation="false" to web form page like blow. it fires the event;
<%# Page EnableEventValidation="false" %>
but I am thinking that it shouldn't be a good idea to set that false. So, what is the alternative here? there error saying that 'use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.' but where am I gonna register that thing? thanks !
Also, I am using some AjaxControlToolkit controls inside my usercontrol and some jquery stuff.
try
if (!Page.IsPostBack)
before load and bind data in datagrid
The problem with ASP.NET 2.0 event validation is that it is all or nothing and a bit heavy handed. I read an article that explains what you should do here.
Basically you need to register the child controls of the user control with the event validation engine.
C#
protected override void Render(HtmlTextWriter writer)
{
// Register controls for event validation
foreach (Control c in this.Controls)
{
this.Page.ClientScript.RegisterForEventValidation(
c.UniqueID.ToString()
);
}
base.Render(writer);
}
VB
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
For Each aControl As Control In Me.Controls
Me.Page.ClientScript.RegisterForEventValidation(aControl.UniqueID.ToString)
Next aControl
MyBase.Render(writer)
End Sub
Now this may not solve your problem because I am not sure how you are returning the string. Another article explains that the event validation is a combination of the control's unique ID and all the values it can pass back. If you are using the command value of a button to pass the text you may have trouble with this task.
If the event that is causing the issue is the click, then registering the controls of the user control should do the trick.
In my situation I was trying to update a GridViews datasource on page_load and I forgot to check if the request was a postback, thus my source was trying to change and it threw this error. Once I did the check for post back it worked fine. Hope this helps someone in the future.
if (Page.IsPostBack == false)
{
this.sqlObj = new SqlServer(ConfigurationManager.ConnectionStrings["PdfReceiverConnectionString"].ToString());
this.populateDataGrid();
}
You can leave it enabled then register your control for event validation. Add the following call in the PreRender or Render page life cycle then your control should work without having to turn off eventValidation:
Page.ClientScript.RegisterForEventValidation(this.UniqueID);
Check your HTML, nested from Tags will also cause that

What calls Page_Load and how does it do it?

Page_Load isn't a virtual method. What calls this method and how does it do it? Is it reflection or some other technique? Also how many events are handled this way?
Also is it preferable to handle things in an overloaded OnLoad or Page_Load? How are they different?
ASP.NET has a feature called "AutoEventWireup" - this feature allows you to create methods that have the EventHandler signature with names like Page_Load and the runtime will wire up the event from the parent page to the method in your class. Basically the runtime does this on your behalf:
this.Load += this.Page_Load;
Now it is better to disable AutoEventWireup and either create these event handlers yourself in the pages OnInit method or simply override the parent page's OnLoad method.
Edit (in response to the OP's comment below): This process doesn't cover button clicks and such but the process is similar.
In order for a method like MyButton_Click to work without you explicitly creating an event handler you would have to set the OnClick attribute on the control in the aspx file like this:
<asp:button
id="MyButton"
onClick="MyButton_Click"
runat="server" />
This would prompt ASP.NET to create the button click delegate for you and attach it to the button's Click event.
The order in which the virtual methods (OnLoad) and event handlers (Page_Load) are called is defined by the so called page lifecycle. This is just the way how the ASP.NET runtime processes an incoming request (e.g. with the Init, Load, Render stages).
You can use either OnLoad or Page_Load but you have to be aware of what happens:
inside OnLoad you must call base.OnLoad
inside base.OnLoad the Load event will be raised
Page_Load is a handler for the Load event (which is automatically wired-up) and will therefore be invoked as a result of the Load event that's being raised.
If you do not call base.OnLoad in your OnLoad override, then the Load event will not be raised.
Update: you can use an empty page with the following code-behind to see what happens:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
base.Load += new EventHandler(My_Page_Load);
}
void My_Page_Load(object sender, EventArgs e)
{
Response.Write("My_Page_Load<br/>");
}
protected override void OnLoad(EventArgs e)
{
Response.Write("Start of OnLoad<br/>");
base.OnLoad(e);
Response.Write("End of OnLoad<br/>");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load<br/>");
}
Try commenting the base.OnLoad(e) call and see again.
The OnLoad method somewhere up the Page hierarchy calls the events assigned to Load (via +=).
The naming Page_Load is just a convention. In AutoEventWireUp mode (i.e. no event handler explicitly declared) this convention is used to find event handlers by their names.
If you have .Net1 available, you can see how the designer adds code to the page's OnInit() to add all components of the page and set
this.Load += new System.EventHandler(this.Page_Load);
.Net2 still does that, but in a separate file which is hidden somewhere under the Windows\Microsoft.Net\Framework\v*\Temporary ASP.Net Files.
I find this chart on ASP.Net Page Life Cycle very useful.
Take a look at the ASP.NET page lifecycle, there is a section for lifecycle events where it describes load.
Load
The Page calls the OnLoad event
method on the Page, then recursively
does the same for each child control,
which does the same for each of its
child controls until the page and all
controls are loaded. Use the OnLoad
event method to set properties in
controls and establish database
connections.
Further Quote:
Note that when creating an event
handler using the Page_event syntax,
the base implementation is implicitly
called and therefore you do not need
to call it in your method. For
example, the base page class's OnLoad
method is always called, whether you
create a Page_Load method or not.
However, if you override the page
OnLoad method with the override
keyword (Overrides in Visual Basic),
you must explicitly call the base
method. For example, if you override
the OnLoad method on the page, you
must call base.Load (MyBase.Load in
Visual Basic) in order for the base
implementation to be run.
In the page directive it says: Autoeventwireup="true"
Thats what happens, its automatically wired up to the Load event... (and some other events like PreInit, Init, Unload etc.)

UserControl events not working for first time

I am calling my user control in a web page dynamically.
For the first time when I click the button on user control, the event is not firing. When I click the same for a second time, the events are firing..
Can anyone help me?
Sounds like the hookup to the button OnClick event is not occurring on every page load, which is required.
Can you guarantee that the hookup is being performed when you add the control and after a poastback to the page? I always put my event hooks in the Page_Init or Page_Load event handlers and outside of any Postback check. Try putting a breakpoint on the Handler hook up and see if the breakpoint gets "hit" twice.
An event hookup for a button would look similar to:
protected void Page_Load(object sender, EventArgs e)
{
btnSearch.Click += new EventHandler(btnSearch_Click); // breakpoint on this line
}
The client id of the control is derived from the ID of all containing controls that are marked with the INamingContainer interface. So, make sure that ALL controls in the hierarchy have a fixed ID.
For example if you have a Button control inside a Repeater, the ID of the button and the repeater are concatenated to make the client ID for the button. So both the repeater and the button should have the same ID across postbacks.
You can compare the HTML for the first request with the HTML of the second request to quickly determine if this is the problem.
Assign an ID for your dynamically created control -- otherwise no events will be fired.
Are you waiting for the download of full page before pressing the button?
The events are javascript functions hidden by ASP.Net in the page, that maybe not present at the time of your clicking.

Catching events from dynamically added ASP.NET User Controls

I have an ASP.NET web form which I am adding a variable number User Controls to. I have two problems:
The User Controls are added to a PlaceHolder on the form in the first PageLoad event (I only add them when "(!this.IsPostback)", but then when the form is posted back, the controls are gone. Is this normal? Since other controls on the form keep their state, I would expect these dynamically added ones to stay on the form as well. Do I have to add them for every postback?
I also have a button and an event handler for the button click event, but this event handler is never called when I click on the button. Is there something special I have to do to catch events on dynamically added controls?
Yes, you need to add them in every postback.
Yes... the control needs to be in the control hierarchy before asp.net dispatches the event (i.e. create the dynamic controls as early in the page lifecycle as possible).
1) You should add the controls on the Pre-init (Page life cycle)
2) You have to attach the event handler to the event of the created button.(events might occur much later in the page life cycle than the same events for controls created declaratively)
To achieve this, add your controls at page init instead of page load. (re-add at postback)
You'll need to know the id of the buttons added to bind them to the event.
I ran into a similar problem. I had a page that displayed a collection of custom web controls. My solution was to add an additional invisible web control so that when I clicked a button to add another control that I would just use the invisible one. Then on post back my load function would add another invisible control to the collection.
I figured out yesterday that you can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If IsPostBack Then
CreateMyControls()
End If
End Sub
I ran into the exact same problem and struggled through like 5-6 hours.
I'm posting this maybe someone like me could get help.
1) You should initialize your controls at Page.PreInit event. (In my case I had to add my controls to a place holder so I extended PreInit to load those controls before but you don't need to do that. It depends on your scenario.)
2) You should bind those exact methods to your controls after you initialize them in your Page.PreInit event.
Here is my sample code:
protected override void OnPreInit(EventArgs e)
{
// Loading controls...
this.PrepareChildControlsDuringPreInit();
// Getting ddl container from session and creating them...
if (GetDDLSession().Count != 0)
{
foreach (DropDownList ddl in GetDDLSession())
{
ddl.SelectedIndexChanged += SelectedIndexChanged;
phDropDowns.Controls.Add(ddl);
}
}
base.OnPreInit(e);
}
public static void PrepareChildControlsDuringPreInit(this Page page)
{
// Walk up the master page chain and tickle the getter on each one
MasterPage master = page.Master;
while (master != null) master = master.Master;
}

Resources