[Visual Basic]
Sub Page_Load
If Not IsPostBack
LoadValues()
End If
End Sub
[C#]
void Page_Load() {
if (!IsPostBack) {
LoadValues(); }
}
http://msdn.microsoft.com/
Main Topics
Browse All TopicsWorking on my first-ever ASP.NET application, I think I've run into another conceptual gap between the worlds of desktop and web development. Either that, or I've gone nuts; I'd believe either. At any rate, it appears that buttons in ASP.NET do not do what I thought they did, and I'd like some elucidation.
The way my app is currently set up, the Page_Load event populates a RadioButtonList with values from a database. The user selects a value, then presses the Submit button. I coded the Click event of the Submit button to identify the selected value, compare it to something else, and do some other stuff based on the result. When that didn't work, I went to line-by-line debug, and found that before any of the Click event code executed, Page_Load executed a second time with IsPostback = True.
Well, in my app, Page_Load on postback clears all the old data and populates the controls with new values (which I have to figure is pretty standard; I can't see where else you'd do this). This means, of course, that by the time my button code is executed, the value it's looking for -- the user's selection -- is no longer there.
What am I missing here? If button code doesn't execute until after postback has cleared out input controls, how on Earth are you supposed to process user-entered information? Is this a property of the button that needs to be set? Is there a different event I need to code for? Is my compiler indulging in psychotropic substances?
Please advise, and thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
[Visual Basic]
Sub Page_Load
If Not IsPostBack
LoadValues()
End If
End Sub
[C#]
void Page_Load() {
if (!IsPostBack) {
LoadValues(); }
}
http://msdn.microsoft.com/
Ok, so let me get this straight. Right now, the structure looks like this:
Page_Load
{
if ( ! IsPostBack)
// Initialize controls
// Load first set of values
if ( IsPostBack)
// Clear old values
// Load next set of values
}
Button.Click
{
// Read input values that are on the page when the button is clicked
// Do stuff with input
}
....and you're saying it should look like this:
Page_Load
{
if ( ! IsPostBack)
// Initialize controls
// Load first set of values
if ( IsPostBack)
// Read input values that were in the last version of the page that are somehow still there (?)
// Do stuff with input
// Clear old values
// Load next set of values
}
Button.Click
{
// Sit there and look shiny
}
I can do that, it just strikes me as rather...bizzarre. What sort of code does one normally put in a button, then?
I'll accept the solution once I'm sure I've got my head screwed on straight about all this. Thanks for the advice.
No, you are thinking wrongly about this. The input values and the values that are on the page are stored in a ViewState automatically if they are asp.net controls (and not html controls). Normally we don't write too much code in the (IsPostBack) code, but in the (!IsPostBack) code since what matters is the first set of values.
Note that when you clear the old values, you cannot retreive them in the Button.Click event. So, review the part in the (IsPostBack) since you shouldn't include any one the ones that you mentioned.
LCMSDev,
Welcome to the world of the ASP.Net Page Life Cycle! I know it seems a bit confusing at first, but it's not really so bad once you get the hang of it (until you get into dynamically adding user-controls in your code behind, then prepare for late nights figuring out where all your input disappears to :) ). Although the subject can be a bit dry, I definitely recommend at least getting an overview on how it works. You can start at the MSDN here:
Looking at the way your code behind is set up now:
Page_Load
{
if ( ! IsPostBack)
// Initialize controls
// Load first set of values
if ( IsPostBack)
// Clear old values
// Load next set of values
}
Button.Click
{
// Read input values that are on the page when the button is clicked
// Do stuff with input
}
I'd say you're *pretty* much on the right track, but here is what I would change. Take all the code out of the IsPostBack section and place it in the button click event so it looks more like this:
Page_Load
{
if ( ! IsPostBack) {
// Initialize controls
// Load first set of values
}
}
Button.Click
{
// Read input values that are on the page when the button is clicked
// Do stuff with input
// Clear old values
// Load next set of values
}
As the previous posters mentioned, you don't really do too much with the code while using if(IsPostBack). There of course is always SOME situtation that exists that it might be appropriate, but for the most part you will place most code in the !IsPostBack and sometimes code that you want executed everytime outside of the if statement completely.
Hopefully this helps. Don't jump off the ledge just yet, I think you'll find that WebForms ends up being a bit easier to grasp than the WinForms. :)
Mike
Sorry,
I forgot to add the MSDN link before I actually submitted. Here it is:
http://msdn.microsoft.com/
Business Accounts
Answer for Membership
by: The_eaglePosted on 2009-08-05 at 09:39:32ID: 25025309
YOu are correct. The correct way is to put your code in the Page_Load inside an if Not IsPostBack. Since at each server event, the Page_Load is being called, and it posts back. So we put the Not IsPotBack condition in order to run codes the first time the page loads.