初学问题,关于IsPostBack。
<%@ Page Language="VB" %>
<script language="vb" runat="server">
Sub Page_Load(Source As Object, E as EventArgs)
If Not IsPostBack Then
MyButton.Text = "OK"
MyDropDownList.Items.Add("http://www.microsoft.com")
MyDropDownList.Items.Add("http://www.wrox.com")
MyDropDownList.Items.Add("http://msdn.microsoft.com")
End If
End Sub
Public Sub Click (ByVal sender As Object, ByVal e As System.EventArgs)
Response.Redirect(MyDropDownList.SelectedItem.Text)
End Sub
</script>
<html>
<body>
<form id="WebForm1" method="post" runat="server">
<asp:DropDownList id=MyDropDownList runat="server"/>
<asp:button id=MyButton runat="server" OnClick="Click" Text=""/>
</form>
</body>
</html>
上面的
If Not IsPostBack Then
...
End If
据作者介绍,是为了防止在下拉列表中增加重复的项,原话如下:
我们添加此检查功能,因为每次单击OK时要重新加载页,以执行Redirect方法。事实上,这表示每次按下按钮时,就会在下拉列表中出现另外三个复制的项,就像它们被重新加载一样。
可是我去掉If Not IsPostBack Then判断,效果完全一样,为什么?IsPostBack到底是什么意思?在什么情况下会为真?