Implementation of IsPostBack in page load

2019-01-14 16:19发布

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.

8条回答
来,给爷笑一个
2楼-- · 2019-01-14 16:28

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.

查看更多
Ridiculous、
3楼-- · 2019-01-14 16:28

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

查看更多
混吃等死
4楼-- · 2019-01-14 16:29

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.

查看更多
混吃等死
5楼-- · 2019-01-14 16:37

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)
查看更多
爷的心禁止访问
6楼-- · 2019-01-14 16:38

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,

查看更多
我命由我不由天
7楼-- · 2019-01-14 16:38
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();
    }
}
查看更多
登录 后发表回答