当前位置:编程学习 > C#/ASP.NET >>

关于IsCrossPagePostBack跨页回发,有一点想不通。

有两个页面Page1.aspx,Page2.aspx
Page1.aspx的Button2指向Page2.aspx,在Page2.aspx中判断Page1.aspx的跨页回发是否指定了Page2.aspx,在Page2.aspx中指定了的IsCrossPagePostBack,直接访问Page2.aspx正常,能跳转到Page1.aspx,但是如果通过Page1.aspx的Button2跳转到Page2.aspx就出问题了,不能执行if (IsCrossPagePostBack){}里面的代码。


page1.aspx:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    public TextBox pp_TextBox1
    {
        get
        {
            return TextBox1;
        }
    }

    public Calendar pp_Calendar1
    {
        get 
        {
            return Calendar1;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.Label1.Text = "Hello" + this.TextBox1.Text + "<br />" +
            "选择日期:" + this.Calendar1.SelectedDate.ToShortDateString();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        { 
            //Do Process
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交给自己" />
        <asp:Button ID="Button2" runat="server" Text="提交给egPostBackUrl2.aspx" PostBackUrl="egPostBackUrl2.aspx" OnClick="Button2_Click" /><br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>
</html>

Page2.aspx:

<%@ Page Language="C#" %>
<%@ PreviousPageType VirtualPath="Page1.aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (Page.IsCrossPagePostBack)
        {
            TextBox pp_Textbox1;
            Calendar pp_Calendar1;

            pp_Textbox1 = (TextBox)PreviousPage.FindControl("TextBox1");
            pp_Calendar1 = (Calendar)PreviousPage.FindControl("Calendar1");

            this.Label1.Text = "您好!" + pp_Textbox1.Text + "<br />" +
                "日期选择:" + pp_Calendar1.SelectedDate.ToShortDateString();
            this.Label2.Text = "您好!" + PreviousPage.pp_TextBox1.Text + "<br />" +
                "日期选择:" + PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
        }
        else
        {
            Response.Redirect("egPostBackUrl.aspx");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </div>
    </form>
</body>
</html> --------------------编程问答-------------------- 写错了几句

把page1中的PostBackUrl="egPostBackUrl2.aspx"改成PostBackUrl="Page2.aspx"
把page2中的Response.Redirect("egPostBackUrl.aspx");改成Response.Redirect("Page1.aspx"); --------------------编程问答-------------------- 我也有同样的问题,但是我的url都沒有写错:
default.aspx:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="CrossPagePosting._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:Button ID="Button1" runat="server" Text="Button" /><br />
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
      <br />
      <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="WebForm1.aspx">HyperLink</asp:HyperLink>
      <asp:Button ID="Button2" runat="server" Text="Button" PostBackUrl="WebForm1.aspx" /></div>
    </form>
</body>
</html>
default.aspx.vb
Partial Public Class _Default
  Inherits System.Web.UI.Page

  Public ReadOnly Property Info() As String
    Get
      Return Label1.Text
    End Get
  End Property

  Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.Text = TextBox1.Text
  End Sub
End Class
====
WebForm1.aspx:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="CrossPagePosting.WebForm1" %>
<%@ PreviousPageType VirtualPath="Default.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>
</html>
WebForm1.aspx.vb:
Public Partial Class WebForm1
    Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Me.IsCrossPagePostBack Then
      Label1.Text = PreviousPage.Info
    Else
      Response.Redirect("default.aspx")
    End If
  End Sub

End Class --------------------编程问答-------------------- 不能执行if   (IsCrossPagePostBack){}里面的代码  ?  
那跳转了? --------------------编程问答--------------------

//if(Page.IsCrossPagePostBack) 
if (PreviousPage.IsCrossPagePostBack == true)
--------------------编程问答-------------------- 好老的帖子啊

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack == true)
--------------------编程问答-------------------- 嗯,5楼的
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,