开发者

Implementation of IsPostBack in page load

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 th开发者_Go百科at 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.


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.


When there is no need to repeat the operation other than the first time.

use it with expensive operations (such as getting data from a database or populating ListItems) that must be performed only the first time the page or control is loaded. If the page is posted to the server and then reloaded, there is no need to repeat the operation. By testing the value of IsPostBack, you can skip the expensive operation,


It's for processing form data.

If you want to handle POSTed data, you only want to do so if the page actually posted data, not on first load. Hence, the IsPostBack flag.


What can happen if you cause a postback is you can change the state of your controls, without meaning to. For example in using a gridview, if you postback during edit mode, you will no longer have access to your edit-ed fields.

Often you need to preserve the information from disapearing on a page when you hit the server, this is the point of

if(!Page.IsPostBack)


Your event handlers should be wired up whenever the event can be fired (irrespective of PostBack status)

Also, when adding controls dynamically, be sure to observe the asp page lifecycle


protected void Page_Load(object sender, EventArgs e)            
{
    if (!IsPostBack) { 
        SqlConnection conn = new SqlConnection("Data Source=-----; Database=-----; Integrated Security=True");
        SqlDataAdapter da = new SqlDataAdapter();
        conn.Open();
        da.SelectCommand = new SqlCommand("Select Command",conn);
        conn.Close();
        DataTable dt = new DataTable();
        da.Fill(dt);

        ddlSearch.DataSource = dt;
        ddlSearch.DataTextField = "---";
        ddlSearch.DataValueField = "---";
        ddlSearch.DataBind();
    }
}


Also, you must use IsPostBack if you want to initialize controls, otherwise they will be reverted to the default on every load. This will confuse the user as when they try to use the form their entered values will get reset to your defaults.


First you need to understand what is postback,when you start your project in Visual Studio,
if you have a if statement which checks whether isPostBack is true or false in your Page_Load method, at this point, isPostBack is false, means it is not a postback, then what is postback,
now click a button (if you don't have a button,please add one and the button click method as well), at this point, you send a request back to the server, the server then response, this process is the so called postback, which is triggered by clicking the button,

one thing you really need to notice, the Page_Load method will be executed again, not only the Button_click method will be executed, so now, the isPostBack is true, means it is postback, yes, it really is a postback, because you clicked the button.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜