IsPostBack in ASP.NET

What is the logic behind IsPostBack in Asp.Net, means what and how do the system do when the ispostback properties of page has been occur

Questions by sharif

Showing Answers 1 - 7 of 7 Answers

B.K.Das

  • Feb 11th, 2006
 

when u click on any button on the browser then a postback event generates and sends a alert mesg. to the web server and the server executes the code behind the button clicked.

  Was this answer useful?  Yes

paramvir singh

  • Mar 24th, 2006
 

hi! currently i am pursuming for .net certification and i want to clear the logic behind ispostback property of page plz reply as early as possible.

with regards.

paramvir singh

Engg. in CSE.

  Was this answer useful?  Yes

mahesh Reddy

  • Sep 15th, 2006
 

An ASPX page is loaded upon each request.when we click the command button, it submits the form back to the server and requests the same page.This phenomenon is known as PostBack.The system will load the page again, and hence, the Page_Load event will take place on every request.

  Was this answer useful?  Yes

satya

  • Oct 11th, 2006
 

You put a DropDownList or a ListBox (or just about any ASP.Net control which has multiple items assigned to it) on your web page (inside a form, naturally). Then, at some point, you either populate the list items manually, or bind it to a database table. However, you do it, you get a list of items from which, at some point, the end user can make a choice. Based on that choice, the end user gets more data in return. Most of the basic item population of these controls is done during the initial loading of the page (Page_Load event). That way, the list items are available for choosing once the page is finished loading.

Let's say, then, you also put a button on the page. That way, the end user can choose an item in the list, click the button and get the extended data, based on the selection made. The button's click event would then take the item which was selected and use it in a click event that could then, possibly connect to a database and use the selected item's data to filter a query against a database.

"Of course", you say - "Simple", you say - "No Problem!", you say - even "Why are you bothering me with this drivel?" This is where it all boils down to the mystery for which this tutorial was created. Here it is - your DropDownList was populated with the last names of people at your company. You create a sub procedure for the onclick event of the button to connect to the database and give the end user back all the available data in the table, based on that last name. The SQL statement goes something like this:

SQL = "Select * from Employees where lastname = '" & DropDownList.SelectedItem.Text & "'"
However, when you do this, you either get an error, or you get absolutely no results back from the query. You're sure the query is correct - but you can't figure out why this isn't returning any records.

That's because you didn't surround the data population of your control with the basic statement this tutorial is all about:

If not Page.IsPostback then

End If

What's happening is that your page is re-loading the data into your server control - it's not responding in any way, to the click event from the button click. First of all, you would never need the server control to re-populate EVERY time the page loaded. That would be terribly inefficient and as you have seen here - it makes making a choice from the control pretty much unuseable.

As I said at the beginning, there are a couple of ways to populate the data items of a server control (DropDownList, ListBox, RadioButtonList, CheckBoxList, etc) - manually and binding the results from a database query to it. It doesn't matter how you populate the data. What DOES matter is that you include the population of the control within the boundaries of this If/Then Statement.

Two things will happen then. The first thing that will happen, is that, on each subsequent loading of the page, the control doesn't go through the redundant action of re-loading the control. It loads it only once, at the initial page load. Once the page is loaded, the click event from the button (or however you post back) is considered to be a postback. Yes, the page gets reloaded, but, by surrounding your data population with this statement, the page knows that the initial data loading was already done, so it doesn't happen any more, and it gets maintained throughout any subsequent post back. The second, and most rewarding thing that happens is that the selection which was made by the end user is maintained throughout the post back. This is the pay-off - by doing it with the If/Then Page.IsPostBack statement, your selection carries through to your query and the query returns the results you wanted in the first place.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions