初学问题,关于IsPostBack。

youngwolf 2004-10-07 11:01:57
<%@ 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到底是什么意思?在什么情况下会为真?
...全文
257 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
youngwolf 2004-10-13
  • 打赏
  • 举报
回复
up
aboluoyuren 2004-10-09
  • 打赏
  • 举报
回复
简单,我常遇到这样的问题。
If Not IsPostBack Then
...
End If
是页面的刷新的段程序!是去除页面缓存的程序段,通常用在和数据库关联的页面中,一般是在page-load事件中使用的!你的实验效果没有达到是因为没有重新装载(在你submit后点下backspace键,或者在页面上加上个返回的按钮,你看看效果!)
呵呵,不善表达,见笑了!~~~~~~~
maq2000 2004-10-09
  • 打赏
  • 举报
回复
我也想知道有什么东西是直接提供给我们的,而有哪些不是
youngwolf 2004-10-09
  • 打赏
  • 举报
回复
有人能回答一下倒数第三楼的问题吗?
feona 2004-10-08
  • 打赏
  • 举报
回复
如果是第一次载入页面的话,ispostback为false
astrofay 2004-10-08
  • 打赏
  • 举报
回复
判定是否为第一次载入页面
huanghui22 2004-10-08
  • 打赏
  • 举报
回复
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
'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


Private Sub MyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyButton.Click
'这句不要你就能看到效果了Response.Redirect(MyDropDownList.SelectedItem.Text)
End Sub
youngwolf 2004-10-08
  • 打赏
  • 举报
回复
顺便再问个问题,如果知道ispostback,那编写上面那样的程序就很容易,可如果我不知道有ispostback这个属性,应该去什么地方查询呢?其实编程方面我并不害怕,我是VC程序员,我只想知道哪些东西是提供给我直接用的,可是找不到去哪儿找我要的信息。
newstone25 2004-10-08
  • 打赏
  • 举报
回复
Sub Page_Load(Source As Object, E as EventArgs)
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 Sub

当load页面后,MyDropDownList添加了3个连接
当点击ok时,又添加了3个连接
DeltaCat 2004-10-07
  • 打赏
  • 举报
回复
<%@ Page Language="VB" %>
<script language="vb" runat="server">
Sub Page_Load(Source As Object, E as EventArgs)
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 Sub

Public Sub Click (ByVal sender As Object, ByVal e As System.EventArgs)
'Response.Redirect(MyDropDownList.SelectedItem.Text)
label1.text = "hello"
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=""/>
<asp:label id="label1" runat="server" text=""/>
</form>
</body>
</html>
DeltaCat 2004-10-07
  • 打赏
  • 举报
回复
IsPostBack 是判断 是否是第一次访问, 如果第一次访问, 就在DROPDOWNLIST里增加那几个链接, 否则就不增加,要不然,你点了某个SERVER端的BUTTON后, 如果不离开本页面, 就又增加了 同样的那几个链接
youngwolf 2004-10-07
  • 打赏
  • 举报
回复
可是我去掉If Not IsPostBack Then后,并没有出现重复的项,你能改一下程序,使我在没有If Not IsPostBack Then判断的条件下能看到重复的数据项吗?我只想确定一下是不是像作者说的这么一回事。
echo123321 2004-10-07
  • 打赏
  • 举报
回复
可以这么说看看是不是回执页面。也就是是不是第一次访问
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { LoginAcc.LoginCheck(); if (!Page.IsPostBack) { DataBind(); } } void DataBind() { //string str = "select NewsId,Title,AddTime,ViewCounter from ViewTable order by AddTime desc"; SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); conn.Open(); SqlCommand cmd = new SqlCommand("up_GetDetilNews", conn); cmd.CommandType = CommandType.StoredProcedure; DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds,"result"); GridView1.DataSource = ds.Tables["result"]; GridView1.DataBind(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { //GridView1.PageIndex = e.NewPageIndex; GridView theGrid = sender as GridView; // refer to the GridView int newPageIndex = 0; if (-2 == e.NewPageIndex) { // when click the "GO" Button TextBox txtNewPageIndex = null; //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow //updated at 2006年6月21日3:15:33 if (null != pagerRow) { txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; // refer to the TextBox with the NewPageIndex value } if (null != txtNewPageIndex) { newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex } } else { // when click the first, last, previous and next Button newPageIndex = e.NewPageIndex; } // check to prevent form the NewPageIndex out of the range newPageIndex = newPageIndex < 0 ? 0 : newPageIndex; newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex; // specify the NewPageIndex theGrid.PageIndex = newPageIndex; // rebind the control // in this case of retrieving the data using the xxxDataSoucr control, // just do nothing, because the asp.net engine binds the data automatically // 别忘了重新绑定 DataBind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string strId = (GridView1.DataKeys[e.RowIndex].Value).ToString(); string delStr = "delete from ViewTable where NewsId=" + strId; DbUtil.DbHelper.ExecSqlNonQuerry(delStr); DataBind(); MyUtil.CsHelper.ExecJS("删除成功!", this); } }

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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