Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of IsPostBack in page load

Tags:

The more I use ASP.NET, the more if (!IsPostBack) {} seems pointless...

First example:

For example, I just Googled an issue, they said use this as part of the solution:

if (!Page.IsPostBack)
{
   Page.LoadComplete += new EventHandler(Page_LoadComplete);
}

Which does exactly as coded, LoadComplete will only fire on the first load. After clicking a button, or anything that triggers a postback, the LoadComplete event is left unhooked, thus skipping the event handler. Therefore, their "fix" only works upon the first load = worthless. I promptly commented out the if (!Page.IsPostBack) {} and now the event always triggers as desired.

Second example:

I am attempting to hook events to a dynamically created button (which by the way, I can't get to work [GRR!]). I see examples showing this:

myEditToggleButton = new Button();
myEditToggleButton.ID = "editToggleButton"; 
//^GOTTA HAVE THIS FOR EVENTS TO WORK! (supposedly, I haven't seen it work...)
if (!IsPostBack)
{
   myEditToggleButton.Click += new EventHandler(myEditToggleButton_Click);
}
Controls.Add(myEditToggleButton);

Like the first example, my understanding is that the event wouldn't be hooked after the first page load, thus the button is "inert" after one click (because clicking triggered a postback).

Question:

When should you use if (!IsPostBack) {}? I am guessing it has to do with mark-up created controls only.

like image 468
Yerg Avatar asked Sep 01 '10 18:09

Yerg


People also ask

When should I use page IsPostBack?

IsPostBack. Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback. The IsPostBack property can be used to determine if the page is submitted to itself. When a form is submitted back to the same page that contains it, it's called a post back.

How do I use IsPostBack?

IsPostBack event is generated by the web controls to alert the server to take respected action of the event generated. When the button is clicked then click event is generated which further cause ispostback event & it alerts the server to take respected action during postback event.

What is IsPostBack property explain it with a suitable example?

IsPostBack is a property of the Asp.Net page that tells whether or not the page is on its initial load or if a user has perform a button on your web page that has caused the page to post back to itself.

What is the meaning of IsPostBack in ASP.NET c#?

IsPostBack is a Boolean property of a page when is set (=true) when a page is first loaded. Thus, the first time that the page loads the IsPostBack flag is false and for subsequent PostBacks, it is true.


1 Answers

In short, you use it everytime you need to execute something ONLY on first load.

The classic usage of Page.IsPostBack is data binding / control initialization.

if(!Page.IsPostBack)
{
   //Control Initialization
   //Databinding
}

Things that are persisted on ViewState and ControlState don't need to be recreated on every postback so you check for this condition in order to avoid executing unnecessary code.

Another classic usage is getting and processing Querystring parameters. You don't need to do that on postback.

like image 198
Claudio Redi Avatar answered Oct 02 '22 21:10

Claudio Redi