Difference Between PreInit And Init In The WebPage LifeCycle
Mar 1, 2010the exact difference between PreInit and Init in the ASP.NET Page LifeCycle ?
View 10 Repliesthe exact difference between PreInit and Init in the ASP.NET Page LifeCycle ?
View 10 RepliesI see from around in the 1.4.4 source that Autofac's ASP.NET integration (via Autofac.Integration.Web) peforms injection of properties on the Page as part of the HttpContext.PreRequestHandlerExecute event handling, but that the page's child controls don't get their properties injected until Page.PreLoad.
What this means, though is that the injected properties of child controls are unavailable for use in the OnInit event handler.
For example, this works fine:
HelloWorld.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HelloWorld.aspx.cs" Inherits="HelloWorld" %>
<html>
<body>
<asp:Label runat="server" id="lblMsg" OnInit="HandleInit"/>.........
But changing the HelloWorld Page to a UserControl (.acsx) and putting the UserControl in another page doesn't work because _msgProvider isn't injected early enough.
Is there a way to make Autofac inject properties of child controls earlier?
When exactly is the view state accessible from the .Viewstate property of a control? From my observations, the closest event is on the Page.PreLoad event (at Page.InitComplete, Viewstate is still unavailable).
However, for controls that implement the IPostBackEventHandler interface the LoadValue() method is called and the .Viewstate collection is available (this occurs after Page.InitComplete and before Page.PreLoad).
Does anyone know of any additional events that can be used to know when Viewstate is available? Or any tricks (not excluding reflection on private/protected/internal members) that can be used to know if the Viewstate has loaded or not?
According to the msdn documentation, as you can see on the diagram, a server control's "Data binding events" occurs after all "control changed events". So in the case of a DetailsView control, the ItemInserted event would happen before the database operation?
View 1 Replieswhen does the constructor on a page in asp.net page lifecycle get called?
View 1 RepliesHow to get control in ASP.NET PreInit event? Pointers are null and FindControl method returns null.
I am using master and content pages. Markup of the content page looks like this:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentBody" runat="server">
<asp:Table ID="Table1" runat="server" Width="100%">
.....
</asp:Table>
</asp:Content>
And code like this:
private void Page_PreInit(object sender, EventArgs e)
{
Control table = this.FindControl("Table1");
//table is null here
}
So table still is null after this.FindControl("Table1"). NamingContainer of the page is null too. What am I doing wrong?
UPDATE I have to use this event to create controls. As said in the ASP.NET Page Life Cycle Overview this event should be used for dynamic control creation. I need to create a list of links in my table. May be there is another way to do it?
how can we add Page's PreInit event programatically in code behind with c# language ? More precisely i would like to say if AutoEventWireup is set to false then how to handle Page's PreInit event ?
View 4 RepliesI have a master page and i am changing the masterpage file property to some other master page dynamically in the page PreInit event and that changing url is taken from a viewstate. but the view state is not getting in the pre init event.
View 2 RepliesHow to get postback data in page preinit stage? Can I do that?
View 2 Repliesi am writing a login page in asp.net c# not using login control.i can create account and log in without problems as a user.the pages that require login has a separate master page.and i want to check if the user is logged in in masterpage page_preinit function but the problem is that child page event functions are called before masterpage's so i could experience a problem like session expire in child page before i get to check it in master page.is there anyway around this?
ok found the event im looking for. second one on the list
http://msdn.microsoft.com/en-us/library/dct97kc3.aspx
I have a content page with a grid view control in it. In the page preinit event of this page, i am trying to add a column to the grid view. But the value of the grid view shows up as null. Is there something i am missing or how do we add properties to the grid view in the page pre init of the content page.
View 3 Replies I'm referencing this article:[URL]Specifically, this line:
"The Init event for each child control occur before the corresponding event is raised for its container"
I've never noticed this behavior before, but can definitely see that this is correct. I don't understand WHY this is necessary though. Can someone shed some light on this?
During a recent interview the following question was asked.
- A Master page which contains
- An ASPX web form page which contains
- A Web User Control inside the page which contains
- A button to fire some code in a button_click event
The Init Event will fire (Inner Most to Outer Most)
aspx.page Begin Init
-> Inside user control Page_Init
-> Inside master page Page_Init
-> Inside lifecycle page Page_Init
aspx.page End Init
and Load Event will fire
aspx.page Begin Load
-> Inside lifecycle page Page_Load
-> Inside master page Page_Load
-> Inside user control Page_Load
aspx.page End Load
Why does ASP.NET framework support different execution order in Load() and Init().This was the question asked in interview.I have no idea about what the interviewer expecting from me.
In S#Arch based web application I need to read some data during the application initialization. From the first point, the best place - HttpApplication.Application_Start() or HttpApplication.Init()But, Application_Start isn't applicable as there is no WebSessionStorage yet. Init() seems isn't fit as well, as there is no NHibernateSession.
View 1 RepliesI am creating a dynamic <asp:Table> based on a user's selection from a DropDownList. The number of columns and rows in the table depends on the user's choice from the DropDown.
The final two columns in each row needs to contain an Edit and Delete button. I am able to create these buttons programmatically. However, I am unable to get the click event to fire.
After researching this, it appears to be the case that my dynamic Web controls need to be created in the page Init method.
However, I am unable to do this as I do not know how many table rows, columns and buttons to create, until after the user has made a selection from a DropdownList.
I would be be very grateful for some tips on the correct way to create and enable the events for my dynamic buttons, in this scenario.
Here is a brief outline of how the program currently works (this is just a brief outline I quickly sketched, small details may be wrong!):
[Code]....
[Code]....
In building custom controls, I've seen two patterns for using the viewstate. One is to use properties to disguise the viewstate access as persistent data.
public bool AllowStuff
{
get
{
return (ViewState[constKeyAllowStuff] != null) ?
(bool)ViewState[constKeyAllowStuff] : false;
}
set { ViewState[constKeyAllowStuff] = value; }
}
The other is to use private member fields and to override the Load/SaveViewState methods on the control and handle it all explicitly:
protected override object SaveViewState()
{
object[] myViewState = new object[2];
myViewState[0] = base.SaveViewState();
myViewState[1] = _allowStuff;
return myViewState;
}
protected override void LoadViewState(object savedState)
{
object[] stateArray = (object[])savedState;
base.LoadViewState(stateArray[0]);
_allowStuff = (bool)stateArray[1];
}
(I cut out a lot of safety checking for clarity, so just ignore that.) Is there are particular advantage to one method over the other? I can't see how they'd differ much performance wise. Version 1 is lazy, so I guess you save a bit if you don't need that particular value during a pass. Version 1 is also more abstract, hides the details better. Version 2 is clearer about when the data is actually valid and ok to read or modify (between the load and save) because it more clearly works within the ASP.NET lifecycle. Version 2 does tend to require more boilerplate code though (a property, a backing private field, and viewstate handling in two places) as opposed to Version 1 which combines all that into one place.
Is it possible to debug the init event from a http module? If I set breakpoints, they don't get triggered.
View 2 RepliesI have a custom control with a UpdatePanel inside. Depending of user action or page programing I call Update method of my UpdatePanel to refresh html portion to user.
For example, I created a property called Title. If the Title change i call Update of my UpdatePanel.
[Code]....
But if the Title property is configured on aspx file, my UpdatePanel is always refreshed because my Title property is setted on Init stage of page life cycle.
I need to know if this property is setted on init stage or if ViewState is restored to avoid call Update, or any tips to avoid refresh my control everytime.
Web application initialization is as follows:
As we know when IIS receives the first request for a particular Asp.net application resource, IIS creates an instance of a HttpApplication (defined in global.asax codebehind).When this new instance is created it's initialization happens that also checks all configured HTTP modules.All modules are then instantiated and put in the application's Modules collection (of type HttpModuleCollection)modules are looped through and their Init() method is called (when they register for request events)
As far as I understand it the above scenario happens when a web application is started/initialized (hence application start event).What happens with modules?
Are they (re)instatiated on each request or reused from the Modules property on each consecutive request while the web application is alive? As I understand IIS and Asp.net they are reused through the whole life of a web application.
If they are reused, can we assume that their Init() method is actually a pseudo event handler for application start event? The thing is we can't attach to application level events within http modules. But if they are being reused we could use Init() as application start event and do whatever we'd put in global.asax instead.
Question,Can we assume that module's Init() method is called only on application start event? Could we use this assumption to i.e. register routes for applications whose global.asax codebehind we can't change? web.config is usually accessible and we can change it the way we want.Would this actually work?
Additional info,We can check HttpApplication code and check its InitModulesCommon() method. This one actually calls Init() of each registered HTTP module. What is more interesting is that this method is only used by InitIntegratedModules() and InitModules() methods. Which are both used only in HttpApplication.InitInternal() method. This is the basis of my assumptions, but I would like to know whether someone has abused IHttpModule.Init() for application start event.
im using a init function after partial postback again the init function is not calling.
<asp:ScriptManager runat="server" ID="MainSM" EnablePartialRendering="true">
<Scripts>
<asp:ScriptReference Path="~/JS/menu.js" />
</Scripts>
</asp:ScriptManager>
<script type="text/javascript">
menu.init("class");
</script>
this script works fine but after partial postback again this init function is not working. So i searched in google i found some articles regarding this [URL]
<script type="text/javascript">
function pageLoad() {
// Initialization code here, meant to run once.
</script>
this works fine when it was postback,but without postback this init function is not working
I'm using a file manager-type WebControl that does lots of postbacks. It's placed inside a Page that is relatively complex. I would like to prevent the WebControl from causing the whole Page to go through the lifecycle.
Is there any way to isolate the WebControl from the rest of the Page? The only way I can think of is sticking the WebControl in a separate Page and creating an iframe in the original Page. Unfortunately that also means my WebControl properties/settings are no longer in the original Page. If I want two instances of the WebControl with different settings, then I have to create a Page for each setting and reference the correct one in my iframes. Not quite as "drag & drop" as I would like.
Here's the situation - Most of this ASP.NET Web Forms application (which uses a single master page for all pages) with Forms Authentication, has a standard session timeout, but there are some "modes" where we store an encoded cookie that links the user to their account.
I would like to manually check early on in the page lifecycle for the cookie, and if certain conditions are met, manually re-establish the user's authentication ticket/session.
Where's the best place to do this? Master page Page_Init? Global.asax BeginRequest?
BreakPoint not working in Init, InitComplate, PreLoad events in ASP.NET page with C# in VS2008. But it is working for Page_Load event.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void InitializeComponent()
{
this.PreLoad += new System.EventHandler(this._Default_PreLoad);
this.InitComplete += new System.EventHandler(this._Default_InitComplete);
this.Init += new System.EventHandler(this._Default_Init);
this.PreRender += new System.EventHandler(this._Default_PreRender);
this.PreInit += new System.EventHandler(this._Default_PreInit);
this.SaveStateComplete += new System.EventHandler(this._Default_SaveStateComplete);
}
protected void _Default_InitComplete(object sender, EventArgs e)
{
........
}
protected void _Default_Init(object sender, EventArgs e)
{
.........
}
protected void _Default_PreLoad(object sender, EventArgs e)
{
..........
}
}
I read this article first to try to solve this issue, but I am already loading the controls in the Init phase of the page, yet viewstate is not persisting. Any ideas as to why? (Of note is that this is for a custom module in DNN and I do have some AJAX update panels on the page, though this section is not within an update panel, for what it's worth.)
As an overview of what I've got:
1. LoadControl.ascx - based on reading query string parameters, determines whether to load the master or the detail .ascx.
2. Master.ascx
3. Detail.ascx
In my master control I dynamically load either a master or detail sub-control in the page_Init. The detail page has a treeview control and then uses a multi-view control to display the panel associated with the depth of the node selected on the tree. On the panel being displayed I have a cascading ddl within a detailsview control which initiates a postback to select the child ddl based on the parent ddl selection. However, when the page refreshes after the postback, both ddls have resorted to their default selection.
What is interesting however, and I just realized this as I am typing, is that the Treeview is populated when the page comes back from the postback, so that is only possible if viewstate is being persisted right? (See the page_load code and you'll see that Call PopulateRootLevel(intTreeUserID) which populates the treeview, is not called on postbacks....hmmm.
Any ideas as to why the treeview maintains viewstate but not my ddls???? But though the tree is populated, I do have to explicitly select the node again and set the panel that the detailsview control is on to visible again too....
Here is the dynamically loaded control code:
[Code]....
And in the Detail.ascx page load I've got:
[Code]....
[Code]....
When the user selects an "account" we dynamically create a linkbutton with the selected dates. However, the linkbutton onclick handler doesn't fire when the button is clicked.
Dim linkBtn As New LinkButton
linkBtn.Text = "blah"
AddHandler linkBtn.Click, AddressOf linkButtonHandler
linkBtn.ID = panelDatesRencontre.Controls.OfType(Of LinkButton).Count
panelDatesRencontre.Controls.Add(linkBtn)
ScriptManager1.RegisterAsyncPostBackControl(linkBtn)
Dim Trigger1 As New AsyncPostBackTrigger
Trigger1.ControlID = linkBtn.ID
Trigger1.EventName = "Click"
UpdatePanel2.Triggers.Add(Trigger1)
Answer: i make that in page_load, it seems to work