Using IsPostBack and Concept of PostBack

What is PostBack?

Asp.Net commands are server-side codes. When a request is made to an Asp.Net page, the request reaches the server, then the page is created on the server and the html output is sent to the client.

If the user wants to take action by clicking a button or performing a similar event, the page is sent back to the server.

Sending the page back to the server for processing is called postback.

In some cases, it may be necessary to know whether the page was called the first time or whether it was sent back to the server. To understand this, the IsPostBack property of the Page class is used.

Example: The most used example of the IsPostBack feature is as follows: Let's have a listBox control on our page. Let's add an element to this control in the Page_Load event.

protected void Page_Load(object sender, EventArgs e)

{

ListBox1.Items.Add("Trekking");

ListBox1.Items.Add("Swimming");

}

In a usage like above, the elements will be added again each time the page is sent to the server. So when the page is called for the first time, two elements are added to ListBox1. Then these two elements are added again when a button is clicked and the page is postbacked.

To prevent this;

protected void Page_Load(object sender, EventArgs e)

{

   if( !Page.IsPostBack )

   {

     ListBox1.Items.Add("Trekking");

     ListBox1.Items.Add("Motorsiklet");

   }

}

we can use as. The expression Page.IsPostBack here means "if the page has been sent back to the server". ! character means "if the page is not postback, that is, if the page is being called for the first time".

Let's do this in another example. When the page is first created, a random number is generated and written in label1. While this process takes place in new requests to the page, it should not be repeated when the page is postback, so the first number kept there.

if( !IsPostBack )
{
    Random nesne1 = new Random();
    int a = nesne1.Next(10000, 99999);
    Label1.Text = a.ToString();
}

As you can see in the example above, we can use the IsPostBack method without specifying the Page class.

what is postback, how to use ispostback, asp.net doesn't do it every time the page is loaded, only when the page is called first, IsPostBack example tutorial sample

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 783 times.

Online Users: 274



using-ispostback