Tuesday, 23 September 2014

ASP.net IsPostBack method.

It is not IsPostBack method. It is the property in the System.Web.UI namespace. It should be Page.IsPostBack.

IsPostback property is used to Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.

This IsPostBack property returns the boolean value either True or False.

The following example shows how to test the value of the IsPostBack property when the page is loaded in order to determine whether the page is being rendered for the first time or is responding to a postback. If the page is being rendered for the first time, the code calls the  BindDropDown() method.

private void Page_Load()
{
    if (!IsPostBack)
    {
        // Validate initially to force asterisks
        // to appear before the first roundtrip.
        BindDropDown();
    }
}

After render the web page, if you do any post back then it will be called. In the web page, we used to check whether its loaded first time or posted back. Because we do not want to bind the concrete controls to rebind again. It will increase the performance of the web application.
 public void Page_Load(Object sender, EventArgs e)
        {
            //Print the time when the page loaded initially
            Response.Write("The Page Loaded at: " + DateTime.Now.ToLongTimeString());
            //Attach the selected index change event for both the dropdown control and the list box control
            //so as to get the value and set it in the corresponding label
            dropDownList1.SelectedIndexChanged += new EventHandler(dropDownList1_SelectedIndexChanged);
            listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
            if (!IsPostBack)
            {
                var posts = FaceBookPosts.Posts();
                this.listBox1.DataSource = posts;
                this.listBox1.DataTextField = "Post";
                this.listBox1.DataValueField = "UserName";

                this.dropDownList1.DataSource = posts;
                this.dropDownList1.DataTextField = "Post";
                this.dropDownList1.DataValueField = "UserName";

                this.DataBind();
            }
        }

PostBack is the name given to the process of submitting an ASP.NET page to the server for processing . PostBack is done if certain credentials of the page are to be checked against a database (such as verification of username and password). This is something that a client machine is not able to accomplish and thus these details have to be 'posted back' to the server.

Usage of IsPostBack in ASP.Net-

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. An important point to be noted here is that each time a PostBack occurs, the entire page including the Page_Load is 'posted back' and executed.
Thus a very common programming practice is that whenever some part of the code is to be executed just the first time a page loads, it is checked against the IsPostBack flag.

Pls refer this url for examples,
http://dotnet.tekyt.info/?p=30

No comments:

Post a Comment