Next Level ASP.NET / Understanding PostBack
Subtitles of the Movie
Now in this video I want to talk about understanding PostBack. Now PostBack is something that if you'll stop and think about it is very understandable but trust me, this thing will take you by surprise and absolutely steal an afternoon from you one day while you're programming. But let's talk about PostBack. We've got a Web browser and a client out there and on the Web server we have a file. This is www.mark.com/pricelist. And so this Pricelist.aspx page, this user wants to see, or they want to get. Well, let's think about what happens here. If we send this page to them, then they get the page and then they click on it. They input text, they make some changes, they click Send and then this page then gets send back. Well, what's happened now is the page comes back, ASP.NET does a feature called PostBack and what that means is, when the page comes back it posts that information to the same page. What we have to know on the Web server is, is this page, has it gone out for the first time or has it been out there and now made a round trip and come back with new information that I need to know about. So, PostBack simply means this has been out to the Browser and they've done something to it and they've clicked it and they've sent it back to us. So here's what happens. How can we know if this page is being opened for the first time or if it's been posted back? Well, as it turns out, the user requests the page, aspx grabs it and starts to work with it and there is a IsPostBack property on the page object in ASP.NET and we can check this. And so, the first time that the user accesses this page, PostBack is false, because this page hasn't been out to the end user yet, hasn't been out to the Browser and it has not been here before on the server, as far as coming from the end user. When we run our ASP.NET functionality we add our data from ASP.NET into the page, whatever was happening, maybe we connected to a database server or something and at this point we now return the page back to the Browser, so the end user sees the page on their screen. Now, they fill out a form, they choose some DropDownLists, they change something on the page and they click Send, or sometimes the DropDownList will send it back automatically. It returns to the Server. It gets posted back. Now, PostBack equals true and then when the page goes back the user sees the result of whatever happened. Now, let me show you in code how you can take advantage of this, of PostBack. What I'm going to do is, let's go out to Visual Web Developer and let's remember that on a previous exercise I went into the PageLoad Event and I said let's make the Label1 Text, whenever this thing opens up the first time, let's make that Mark. Well, I want to show you something here. The first time it loads up we're going to make it Mark. The second time it loads up on a PostBack I want it to say Postback Occurred. So here's what I'm going to do. On my code I'm going to use this PostBack feature. I'm going to say If Page.IsPostBack, I'll just type an equal sign, If Page PostBack is True, then - I'm just going to do a simple IF statement. If Page PostBack is True then let's set Label1 - wait, what are we doing on our Event up here. We are setting Label1.Text to You clicked. So what I'm going to do is, If Page PostBack is True I'm going to set Button2 Text equal to Postback Occurred. It's a little different than what I said I was going to do. Let me go point that out to you. The first time this page comes back from the Server we requested Button2 will be Button2. Once we click on Button1 and we send it to the page and we carry out something on this page the Label will change to, I think, You clicked, something like that. What did I have on there? No the Label Text will change to You clicked. And if it's a PostBack, if PostBack is True, then the Button2 Text will be Postback Occurred. The first time up the Button2 Text is going to be the default, which is Button2. Hope that makes sense. Let's see it in action. Execute this page and on my PageLoad I'm setting the Label Text to Mark, so I will hit OK and the page is going to open up and you will see the PageLoad Event set the Label equal to Mark and Button2 is Button2, because this has not been posted back to the Server. Now when I click Button1 it's going to send this page back to the Server. It's going to change the Label Text to You clicked, just like I told it to in my code - let me see if I can get back over here, yes - notice on the Button1 Click I'm going to change the Label1 Text to You clicked and then I am going to change the Button2, if it's a PostBack, the Button2 is going to change to Postback Occurred. Now when the page loads on the ASP.NET Web server, it's going to check and if PostBack is True then it's going to set the text to this. So first time up we requested the page from the Web server, Button2 is Button2 and the Label is Mark. When I click the button, send it back, when it comes back to me, notice it says You clicked and I can see here that Postback Occurred. ASP.NET figured out, hey, wait a minute, I've already sent this page back, now it's coming back, so PostBack is set to True. This is a PostBack page. Always remember that. Now in the next video we'll talk about what exactly causes PostBack to occur and how you can control it.
Tutorial Information
| Course: | Microsoft ASP.NET 3.5 |
| Author: | Mark Long |
| SKU: | 34102 |
| ISBN: | 1-93633412-7 |
| Release Date: | 2010-03-24 |
| Duration: | 6 hrs / 69 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 