Loading ...

What is IsPostBack in ASP.NET? | CodeAsp.Net

What is IsPostBack in ASP.NET?

 /5
0 (0votes)

Every Beginner have confused about IsPostBack property. Below you will find a very simple example of IsPostBack property. IsPostBack property returns Boolean value. It is used to determine if data is being sent back to the Web server or if the Web page is simply being requested.

Example :-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblIspostback" runat="server"></asp:Label>

        <asp:Button ID="btnTest" runat="server" Text="Click" />
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class CheckIsPostback : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblIspostback.Text = "This is not postback";
        }
        else
        {
            lblIspostback.Text = "This is postback";
        }
    }

}


There is one label and one button. First time when page is load label text will be ‘The page is not postback’ and when you click on the button label text will be ‘The page is postback’ because of postback. 

There are many benefits of postback. Like you want to bind a repeater on page load, so first you can check page is loaded first time or not with the help of IsPostBack property. If you don’t check, repeater would bind every postback.

Example :-

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           rptTest.DataSource = testdata();
           rptTest.DataBind();
       }
       
   }


Well if you want to your code would run only first time when page is loaded. So within the code block we can write the code which shouldn’t execute on postback of the page. When an action occurs, the page containing all the controls within the <FORM... > tag performs an HTTP POST, while having itself as the target URL. This is called Postback.


Do let me know your feedback, comments.



Comments (no comments yet)

Top Posts