If Not Page.IsPostBack 如果没有Page.IsPostBack

Veronika 2010-04-01 05:10:24
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
所发生的事情是,页面把服务器端控件的数据重新加载了一遍,他没有任何包括按钮点击事件反应。第一,我们不需要每次加载页面的时候都重新载入服务器端控件的值,那将低效至极,而且我们也看到了,这样的话我们没法子做出选择。
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.
就像我一开始说过的,有很多种方式为我们的服务器端控件赋值(DropDownList, ListBox, RadioButtonList, CheckBoxList, 等等),或为手动,或通过数据库绑定。但怎样为控件赋值并不重要,真正重要的是你是否把控件放入这个If/Then声明之内。
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.

接下来发生两件事情。首先将要发生的是,每一次页面载入,控件只需在初始化时加载一次,不需要多余的重复加载。一旦页面载入操作完成,由按钮触发的事件(或者其他的需要postback的事件)就会当做是一个postback。是的,页面被重新加载了,但是通过把数据包含在if/then即判断ispostback语句中,页面便知道了控件的数据加载已经完成了,所以加载事件不会再发生,这种状态将会一直贯穿任何的重复的回发事件。第二件事,也是最有意义的事情就是终端用户做出的任何选择都会一直维持在选择的状态。这一切都归功于我们应用了If/Then Page.IsPostBack声明,我们通过选项进行查询,而查询返回的结果我们想置于首位。
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

例子:
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.

通过这样做,贯穿整个页面的postback事件始终,你都可以一直继续保持DropDownList的选项。
...全文
374 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
daxia_9999 2012-08-11
  • 打赏
  • 举报
回复
讲的很好,还是中英文对照的,太棒了!赞一个!
zckynq 2010-07-31
  • 打赏
  • 举报
回复
<asp.net repeater怎么用啊? >
porschev 2010-04-02
  • 打赏
  • 举报
回复
unicode 2010-04-02
  • 打赏
  • 举报
回复
postback和viewstate,难兄难弟
Veronika 2010-04-02
  • 打赏
  • 举报
回复
只能散分了,还要要姐分分的小朋友么
Veronika 2010-04-02
  • 打赏
  • 举报
回复
我只是在博客中,发表了这篇文章,纳了闷了,怎么跑到论坛里面来了~~╭(╯^╰)╮
V-Far 2010-04-01
  • 打赏
  • 举报
回复
不错.不过最好还是中英文分开.一段一段的看着眼花
echo0808 2010-04-01
  • 打赏
  • 举报
回复
学习了,支持下^^^^^^^^^^^
随风落梦 2010-04-01
  • 打赏
  • 举报
回复
学习了,支持下^^^^^^^^^^^
十八道胡同 2010-04-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wiki14 的回复:]
支持。
[/Quote]
rt
wiki14 2010-04-01
  • 打赏
  • 举报
回复
支持。
msnadair 2010-04-01
  • 打赏
  • 举报
回复
这人说话咱有点像拍尖锋时刻时成龙的搭档
Peter200694013 2010-04-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 foren_whb 的回复:]

[/Quote]
够快...
Peter200694013 2010-04-01
  • 打赏
  • 举报
回复
sf?
丰云 2010-04-01
  • 打赏
  • 举报
回复

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧