Implementation of IsPostBack in page load

元气小坏坏 提交于 2019-11-27 22:00:06

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

Yasir Antaal
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.

Zhenxiao Hao

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!