When an asynchronous postback happened inside update panel, another postback happens also for MasterPage not only update panel embedded page .
I want to prevent this MasterPage postback .
is this possible ?
think like i have a MasterPage
and another page which is test.aspx which is content page of MasterPage
i have update panel at test.aspx
when asynchronous postback happens at this test.aspx update panel it also loads MasterPage Page_Load
i want to prevent this (it should not also load MasterPage Page_Load)
Thank you
ASP.NET's UpdatePanel (as well as any normal asp.net page)'s postback does postback the master page as well.
Normally, to properly deal with postbacks, programmers check for it and program accordingly :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dataBindAndSetUpControls();
}
}
So, my answer here is that I don't know if it is possible (and if it is, it certainly isn't easy), and that your request is not a normal thing to do, and programmers handle PostBacks via the blurb above.
Related
I had been having a great deal of issues trying to put a server control button and event handler onto a master page. Every time I clicked the button from one of the content pages, the postback would take me not back to the content page, but to the site.master URL, so naturally that would crash my application. I scoured the internet for a solution, but I couldn't get any to work for me. I finally figured out a very easy fix and so I thought I'd share it in case anyone else runs into similar troubles.
On the Master Page's Page_Init() method, I assigned the PostBackUrl attribute of the server control to the current page Uri using HttpContext.Current.Request.Url.AbsoluteUri;
(UserLogin is my button control name)
protected void Page_Init(object sender, EventArgs e)
{
UserLogin.PostBackUrl = HttpContext.Current.Request.Url.AbsoluteUri;
}
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.
In an effort to speed up my site, I am trying to disable the viewstate as I don't think I am using it everywhere. I have a master page setup with user controls loaded (using LoadControl) in default.aspx. My typical page setup would be:
Main.master -> Default.aspx -> ControlWrapper.ascx -> MyControl.ascx
I have put EnableViewState="false" in my Default.aspx page. Now when I try and read a value from a DropDownList in MyControl.ascx it comes back blank when the form is posted. First all, why is this? I thought I should still be able to read the value from the drop down list?
I then tried enabling the ViewState on that control and it didn't work.
I also tried enabling the viewstate on the Page_Init event of MyControl.ascx using Page.EnableViewState = True; but that didn't help either.
I guess I am misunderstanding the viewstate somewhat, can someone point me in the right direction please?
p.s I don't know if this information is relevant but I am adding the contents of the DropDownList dynamically in the Page_Load event. (Thinking about it, could this be the issues - Will test this now).
Thanks.
With viewstate turned off, the values you are loading in Page_Load are no longer in the list when you post back (until you reload them obviously). If you want to work without viewstate, you will need to set the selected item from the value in Request.Form.
protected void Page_Load(object sender, System.EventArgs e)
{
ddlItems.Items.Add(new ListItem("test1", "test1"));
ddlItems.Items.Add(new ListItem("test2", "test2"));
ddlItems.Items.Add(new ListItem("test3", "test3"));
if (Page.IsPostBack)
ddlItems.SelectedValue = Request.Form["ddlItems"];
}
When you've set ViewState to false the dropdown needs to get populated before page load - which means you probably should do it at page init. Something like this:
protected void Page_Init(object sender, System.EventArgs e)
{
ddlItems.Items.Add(new ListItem("test1", "test1"));
ddlItems.Items.Add(new ListItem("test2", "test2"));
ddlItems.Items.Add(new ListItem("test3", "test3"));
}
Then you should be able to read the value at load:
protected void Page_Load(object sender, System.EventArgs e)
{
someTextBox = ddlItems.SelectedValue;
}
A bit of background:
On this page: Microsofts page cycle
At the image with the page cycle there is the methods "ProcessPostData" and "LoadPostData" firing in between Init and Load. The post data for the drop down contains the selected value - but not the possible values, so when it loads the post data it is essential that the possible values are already there (or it won't be able to set the selected value). Also before the post data has been loaded the selected value has not been set.
If viewstate is enabled it saves and retrieves the possible values in between postbacks.
I will assume you're using .NET 4. View State is the method that the ASP.NET page framework uses to preserve page and control values between round trips.
The reason it didn't work for you when View State was turned off is because that control was rendered again when you performed a PostBack to the server, meaning you lost your selected value.
The reason it didn't work for you when View State was off for the page, but on for the control is because in order for that to work, the following conditions must be met:
The EnableViewState property for the page is set to true.
The EnableViewState property for the control is set to true.
The ViewStateMode property for the control is set to Enabled or inherits the Enabled setting.
ASP .NET View State Overview
When you did EnableViewState = false; on a page then you should not expect DropdownList.SelectedValue after postback.
It will be good if you Enable/Disable ViewState on particular controls rather than disabling whole view state by specifying it on page directive.
I have AutoEventWireup="true" and in my code behind
protected void Page_Init(object sender, EventArgs e)
{
}
When I'm debugging, the Page_Init method is getting fired twice!
Whats going on?
Let's make sure we cover the basics here:
Do you have any controls on your page that have server events? If so, remember that every postback re-creates the entire page. So, to handle an event means running all of the code required put the page together, including your Init and Load events.
Always two there are, no more no less. A request and a response.
You probably have some sort of redirect or ajax postback that is firing.
Do you have any code anywhere that looks something like this?
this.Init += Page_Init;
If so you are accidentally wiring the event twice. Either delete the manual event wiring or set AutoEventWireup to false.
I have several instances of user controls on a page. They are also nested in each other. Since these controls are created dynamically, I am having trouble maintaining their state. I decided to save the state manually to a persistent medium (possibly Session).
On the click event on a button on the host page, I want to write functionality to save the state of the controls BEFORE the postback happens (Once there is a postback, the controls are recreated and I lose the state).
If I put this logic in the button event handler, since this is executed AFTER the postback, I dont have the information anymore.
Which method should I override?
Try creating your button in the Page Init instead of on load.
Not really answering you question, but I'm curious as to why the layout of your buttons is getting reset. Are you doing something via javascript?
How are you creating your buttons. in the page_load? are you checking when its a postback?
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// load all buttons
LoadButtons();
}
}
It sounds like you are re-creating the controls on postback and so they lose their state. You should make use of Page.IsPostBack to prevent this. Also check that the page allows Viewstate.
Actually you want to be loading your dynamic controls in the LoadViewState method override. Don't try to maintain state yourself; it is a waste of time. You just have to put your code in the right place.
See My previous comments on this