If Not Page.IsPostBack
Uncovering the Mystery
揭开他的神秘面纱
________________________________________
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.
很多朋友在网上和参考书中见过这个有点神秘的if/then的判断,也许有那么一次,有人会想知道它究竟讲些什么,本教程会为我们解开疑云。
在asp.net论坛和asp之友的网站上,有一个问题很常见,他以各种形式出现,归结起来就是页面的IsPostBack属性值
"Why doesn't my DropDownList keep it's Selection?"
"Why is the selectedindex for my (ASP.Net control) always turning up a -1?"
“为什么页面的下拉菜单不能保留选项值?”
“为什么页面的控件值总是指向-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.
摘要:
我们在web页面上(当然是在form中)放了一个DropDownList或者ListBox(或者是任何具有多重值的ASP.NET控件).接下来,或者手动添加值,或者绑定数据库表,反正你得到了可供用户选择的选项。基于上述选择,终端用户可以得到更多的数据。多数时候,我们在初始化页面时(即Page_Load事件)就完成了选项值的填充。这样,一旦页面下载完成时,列表项即可供选择。
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:
或者你会说:“这当然”,或者你会说“这简单”,或者你会说“有什么难的”甚至你也许会说:“不要对我一派胡言!”创作这篇教程正是归结于这种神秘感。接下来,你的DropDownlist将会填上你同事的姓氏。我们写下来单击按钮事件的主程序,连接数据库得到基于姓氏的所有相关数据,SQL语句如下:
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