I am trying to modify a control on a page to reduce session dependence. I currently do not have access to the page itself, only the control which primarily consists of a DataGrid. I am trying to retrieve the DataGrid information on postback so I can manipulate the data and rebind the grid.
The issue is that the page is calling a databind on the control before I can retrieve the data. (actually it is calling the databind on the tab control where my control is located.) This call is happening on the OnLoad event of the page, before the OnLoad of the control is called. I saw that the is a PreLoad event that occurs after the viewstate is loaded but before the OnLoad is called. However I am having issues accessing this event from my control. Is there anyway I can access this event so I can retrieve the data before the page overwrites it?
Add the following code to your control instead of the OnLoad. (from here)
protected override void OnInit(System.EventArgs e)
{
// this assigns Page_PreLoad as the event handler
// for the PreLoad event of the Control's Page property
this.Page.PreLoad += Page_PreLoad;
base.OnInit(e);
}
private void Page_PreLoad(object sender, System.EventArgs e)
{
// do something here
}
This might help: MSDN - ASP.NET Page Life Cycle Overview
The image 1/3 of the way down sums it up.
Related
I got a website where you must be logged in to access it, however, if the user lost the Session "loginID", the user should be kicked out immediately by a if(Session["LoginID"] == null) via MasterPage. But since im new to HTML/asp.net, Im not sure where I should actually have it, in Page_Load or Page_PreRender.
Does it even matter which one I have it in?
Using asp.net/c#
Thanks.
You can use Page_PreInIt event for your problem to check if session is live or ended
protected void Page_PreInIt(object sender,EventsArgs e)
{
if(Session["LoginID"] == null)
{
// redirect to login if session is null
Response.Redirect("Login.aspx");
}
}
Page Load:
The Page object calls the OnLoad method on the Page object, and then
recursively does the same for each child control until the page and
all controls are loaded. The Load event of individual controls occurs
after the Load event of the page
Page PreRender
Raised after the Page object has created all controls that are
required in order to render the page, including child controls of
composite controls. The Page object raises the PreRender event on the
Page object, and then recursively does the same for each child
control. The PreRender event of individual controls occurs after the
PreRender event of the page
Read More
I've got aspx page which dynamically loads UserControl into a Panel object based on the input on a set of radio buttons. The UserControl successfully adds and displays properly to the Panel on postback and calls the Page_Load() event just fine in the UC but when I interact with the form in any way that would trigger an event, the event is not captured on the postback.
I've tried to add the event handling association in the Page_Load() which I know gets called as well as adding the association in the ASP.NET tag without any difference in result.
This is how I am adding the control (object names have been simplified):
private UserControl _control;
protected void RadioButtonGroup_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = (RadioButton)sender;
if (radioButton == RadioButton1Ctl)
{
_control = (UserControl1)LoadControl("~/Controls/UserControl1.ascx");
PanelCtl.Controls.Add(_control);
}
else if (radioButton == RadioButton2Ctl)
{
_control = (UserControl2)LoadControl("~/Controls/UserControl2.ascx");
PanelCtl.Controls.Add(_control);
}
}
As I said, the control gets successfully added by when I click any buttons or have any postback events which should be bound on the UC, the control gets removed from the page and events aren't fired.
Any help would be greatly appreciated.
This is happening because the controls are being added dynamically. I would suggest using the DynamicControlsPlaceHolder instead of a Panel. It will persist your controls for you when the page is posted back.
DynamicControlsPlaceHolder:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
The other alternative is to recreate the controls at every postback, before the ViewState is reloaded. I would suggest using OnInit.
The DynamicControlsPlaceHolder takes all of the hard work away, so that might be the best option for you.
Greetings,
1) I assume ObjectDataSource automatically binds to data source only on first request, but not on postbacks ( else ObjectDataSource.Selecting event would be fired on postbacks also, but it isn’t):
A) So only way to force ObjectDataSource to also bind on postbacks is by manually calling DataBind()?
2) Assuming DropDownList1 has DataSourceID set to ObjectDataSource1 , then first time page is loaded, ObjectDataSource1 will automatically call DropDownList1.DataBind() ( after Page.PreRender event) and insert retrieved data.
A) But what if we also manually call DropDownList1.DataBind() when page is first loaded:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) DropDownList1.DataBind();
}
Will ObjectDataSource1 somehow notice that DropDownList1.DataBind() has already be called and thus won’t automatically call DropDownList1.DataBind() ?
B) Normally ObjectDataSource1.Selecting event is fired after Page.Prerender event.But what if DropDownList1.DataBind() is called inside Page_Load()?
Will in that case ObjectDataSource1.Selecting event be fired prior to Page.PreRender?
thanx
Will in that case ObjectDataSource1.Selecting event be fired prior to Page.PreRender?
Yes it is called prior to Page.PreRender.
Reason: Each data bound control whose DataSourceID property is set calls its DataBind method in prerender event,
check page life cycle
http://msdn.microsoft.com/en-us/library/ms178472.aspx
http://dotnetshoutout.com/Data-Binding-Events-for-Data-Bound-Controls-in-ASPNet
Since the load event is called before prerender, and when call databind method then in your situation objectdatasource selected event fired before prerender
I have a piece of code in a User controls that normally should be put in the Page_Load (initializes other components such DropDowns etc.) but I need this to happen before the Page_Load of the page that hosts this control.
I tried to put this in Page_Init:
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
Methods.PopulateWhatList(cboWhatList0, cboWhatList1, fldWhat, Request["WhatId"], true);
Methods.PopulateWhereList(cboWhereList0, cboWhereList1, fldWhere, Request["WhereId"], true);
Methods.PopulateWhoList(cboWho, true, Request["WhoId"]);
Methods.PopulateWhenList(cboWhen, true, Request["WhenId"]);
Methods.PopulatePriceRangeList(cboPriceRange, true, Request["PriceRangeId"]);
}
}
...but have experienced some problems. So where is the best place to but this type of code?
The problem I'm having (and might be unrelated) is that my:
protected override void Render(HtmlTextWriter writer)
{
Methods.EnableValidationWhereList(cboWhereList1, this.Page);
Methods.EnableValidationWhatList(cboWhatList1, this.Page);
base.Render(writer);
}
Isn't called on certain postbacks? (When pressing a LinkButton?)
I'll try a wild guess to what you're trying to do and suggest a solution:
In your Page_Init you're populating the contents of various controls on the page. You're dependent on URL parameters, hence the if(!IsPostBack) clause.
After Page_Init, some of your controls are left in a disabled state, hence the need to enable them in your Render method.
When doing a postback on the LinkButton, you don't see your dropdowns populated on the next page rendering.
What you're experiencing is that disabled controls doesn't get persisted to the ViewState. Since the SaveViewState is called before Render, you're enabling the controls too late in the page lifecycle.
If you instead move your Methods.EnableValidation... calls to a Pre_Render method on your page, control state will get persisted to the ViewState.
After that fix, you should move your code in the Page_Init method to the Page_Load method, where it belongs. That way your controls data will have been loaded from the ViewState if you're on a postback.
The page lifecycle is such that the page_load of the page runs before the page_load of the controls.
If you need to initialise your data sources prior to page load then you can put that initialisation in OnInit provided you ensure the base.OnInit() is called first. Ideally, you should keep your databind calls to the page_load mechanism to ensure you don't have issues with viewstate.
Regarding your render method. What purpose do the 'enable' methods serve in the overall page lifecycle?
Basically, I have a drop down list and a dynamically added user control. The user control loads a grid view depending on the choice that was made in the drop down list. The drop down list is not part of the user control.
Now, the question is, how do i simulate (isControlPostback = false) every time the user changes the selection in the drop down list? It looks like ViewState remembers the control.
Inside my user control I have:
protected bool IsUserControlPostBack
{
get
{
return this.ViewState["IsUserControlPostBack"] != null;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsUserControlPostBack)
{
ViewState.Add("IsUserControlPostBack", true);
//load stuff in the grid view and bind it
}
}
When the user changes the selection on the drop down list, i have a javascript confirm box, and the page posts back. So OnSelectedIndexChanged event for drop down list doesn't get triggered. I would like to remove to do something like this every time the selected index changes:
ViewState.Remove("IsUserControlPostBack");
You can make changes to the control in prerender event. When this event is fired all other actions are made.
Or you can do public property in user control and when setting required to value react on appropriately.
The ViewState you access in your user control is not the same one you access on the page. If you need your page to communicate with your user control, I suggest you add a public method on your user control for this purpose.
If, for some reason, you prefer
something similar to your ViewState
approach, you can try Context.Items.
Note that Context.Items is not
preserved between requests.
Add the control to the page sometime before OnLoad. E.g. OnInit. Between OnInit and OnLoad, the viewstate is loaded and postback events are run.
For anyone who is interested to know the answer:
I ended up implementing a public property inside user control and load the control inside the server drop down list SelectedIndexChanged event rather than OnInit. This eliminated the need for explicit Viewstate use.