If Not Page.IsPostBack in ASP.NET

Many of you have seen this mysterious if/then statement in a lot of the online and book code samples, and you probably have, at one time or another, wondered just what exactly this was all about. This tutorial plans to answer all your questions. There is one basic question that gets asked on ASP.Net Forums and ASPFriends.com ListServes, over and over, in a several different ways. It all boils down to one answer concerning the IsPostBack Property of the page.
“Why doesn’t my DropDownList keep it’s Selection?”
“Why is the selectedindex for my (ASP.Net control) always turning up a -1?”

Fortunately, today, you’ve come to the right place. You questions will be answered.

Scenario:
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.

Examples:
If Not Page.IsPostBack then
Dim strConn as string = “server=(local);uid=UID;pwd=PWD;database=Northwind”
Dim MySQL as string = “Select LastName from Employees”
Dim MyConn as New SQLConnection(strConn)
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
DropDownList.DataSource = objDR
DropDownList.DataBind()
End If
By doing it this way, you will always be able to maintain the selection from your DropDownList (or other ASP.Net Control) through the postback events of your pages.

Related Posts:

  • No Related Posts
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!