Listing 3-9: Page1.aspx
VB
<%
@ Page Language
=
”VB”
%>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text = “Hello “ & TextBox1.Text & “ < br /> ” & _
“Date Selected: “ & Calendar1.SelectedDate.ToShortDateString()
End Sub
</ script >
< html xmlns = ”http: // www.w3.org/1999/xhtml” >
< head runat = ”server” >
< title > First Page </ title >
</ head >
< body >
< form id = ”form1” runat = ”server” >
Enter your name: < br />
< asp:Textbox ID = ”TextBox1” Runat = ”server” >
</ asp:Textbox >
< p >
When do you want to fly ?< br />
< asp:Calendar ID = ”Calendar1” Runat = ”server” ></ asp:Calendar ></ p >
< br />
< asp:Button ID = ”Button1” Runat = ”server” Text = ”Submit page to itself”
OnClick = ”Button1_Click” />
< asp:Button ID = ”Button2” Runat = ”server” Text = ”Submit page to Page2.aspx”
PostBackUrl = ”Page2.aspx” />
< p >
< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label ></ p >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text = “Hello “ & TextBox1.Text & “ < br /> ” & _
“Date Selected: “ & Calendar1.SelectedDate.ToShortDateString()
End Sub
</ script >
< html xmlns = ”http: // www.w3.org/1999/xhtml” >
< head runat = ”server” >
< title > First Page </ title >
</ head >
< body >
< form id = ”form1” runat = ”server” >
Enter your name: < br />
< asp:Textbox ID = ”TextBox1” Runat = ”server” >
</ asp:Textbox >
< p >
When do you want to fly ?< br />
< asp:Calendar ID = ”Calendar1” Runat = ”server” ></ asp:Calendar ></ p >
< br />
< asp:Button ID = ”Button1” Runat = ”server” Text = ”Submit page to itself”
OnClick = ”Button1_Click” />
< asp:Button ID = ”Button2” Runat = ”server” Text = ”Submit page to Page2.aspx”
PostBackUrl = ”Page2.aspx” />
< p >
< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label ></ p >
</ form >
</ body >
</ html >
C#
<%
@ Page Language
=
”C#”
%>
< script runat = ”server” >
protected void Button1_Click ( object sender, System.EventArgs e)
{
Label1.Text = “Hello “ + TextBox1.Text + “<br />” +
“Date Selected: “ + Calendar1.SelectedDate.ToShortDateString();
}
</ script >
< script runat = ”server” >
protected void Button1_Click ( object sender, System.EventArgs e)
{
Label1.Text = “Hello “ + TextBox1.Text + “<br />” +
“Date Selected: “ + Calendar1.SelectedDate.ToShortDateString();
}
</ script >
在Listing 3-9: Page1.aspx中,有两个button。两个button提交form到不同的位置。
第一个button提交form到自身,是ASP.NET 1.0/1.1的默认行为。这个button包含一个OnClick事件。
第二个button使用PostBackUrl属性,这个属性是一个string值,它指出了要提交到文件的位置--Page2.aspx。
Listing 3-10: Page2.aspx
VB
<%
@ Page Language
=
”VB”
%>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim pp_Textbox1 As TextBox
Dim pp_Calendar1 As Calendar
pp_Textbox1 = CType(PreviousPage.FindControl(“Textbox1”), TextBox)
pp_Calendar1 = CType(PreviousPage.FindControl(“Calendar1”), Calendar)
Label1.Text = “Hello “ & pp_Textbox1.Text & “ < br /> ” & _
“Date Selected: “ & pp_Calendar1.SelectedDate.ToShortDateString()
End Sub
</ script >
< html xmlns = ”http: // www.w3.org/1999/xhtml” >
< head runat = ”server” >
< title > Second Page </ title >
</ head >
< body >
< form id = ”form1” runat = ”server” >
< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim pp_Textbox1 As TextBox
Dim pp_Calendar1 As Calendar
pp_Textbox1 = CType(PreviousPage.FindControl(“Textbox1”), TextBox)
pp_Calendar1 = CType(PreviousPage.FindControl(“Calendar1”), Calendar)
Label1.Text = “Hello “ & pp_Textbox1.Text & “ < br /> ” & _
“Date Selected: “ & pp_Calendar1.SelectedDate.ToShortDateString()
End Sub
</ script >
< html xmlns = ”http: // www.w3.org/1999/xhtml” >
< head runat = ”server” >
< title > Second Page </ title >
</ head >
< body >
< form id = ”form1” runat = ”server” >
< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label >
</ form >
</ body >
</ html >
C#
<%
@ Page Language
=
”C#”
%>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected void Page_Load( object sender, System.EventArgs e)
{
TextBox pp_Textbox1;
Calendar pp_Calendar1;
pp_Textbox1 = (TextBox)PreviousPage.FindControl(“Textbox1”);
pp_Calendar1 = (Calendar)PreviousPage.FindControl(“Calendar1”);
Label1.Text = “Hello “ + pp_Textbox1.Text + “<br />” + “Date Selected: “ +
pp_Calendar1.SelectedDate.ToShortDateString();
}
</ script >
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected void Page_Load( object sender, System.EventArgs e)
{
TextBox pp_Textbox1;
Calendar pp_Calendar1;
pp_Textbox1 = (TextBox)PreviousPage.FindControl(“Textbox1”);
pp_Calendar1 = (Calendar)PreviousPage.FindControl(“Calendar1”);
Label1.Text = “Hello “ + pp_Textbox1.Text + “<br />” + “Date Selected: “ +
pp_Calendar1.SelectedDate.ToShortDateString();
}
</ script >
有两种方法取得Page1.aspx中control的值:
第一种:如上Listing 3-10所示,使用PreviousPage的FindControl 方法,其中的字符串为Page1.aspx中control的id值。
第二种:如下Listing 3-11所示,为Page1.aspx中control建立属性。
Listing 3-11: Exposing the values of the control from a Property
VB
<%
@ Page Language
=
”VB”
%>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
Public ReadOnly Property pp_TextBox1() As TextBox
Get
Return TextBox1
End Get
End Property
Public ReadOnly Property pp_Calendar1() As Calendar
Get
Return Calendar1
End Get
End Property
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = “Hello “ & TextBox1.Text & “ < br /> ” & _
“Date Selected: “ & Calendar1.SelectedDate.ToShortDateString()
End Sub
</ script >
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
Public ReadOnly Property pp_TextBox1() As TextBox
Get
Return TextBox1
End Get
End Property
Public ReadOnly Property pp_Calendar1() As Calendar
Get
Return Calendar1
End Get
End Property
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = “Hello “ & TextBox1.Text & “ < br /> ” & _
“Date Selected: “ & Calendar1.SelectedDate.ToShortDateString()
End Sub
</ script >
C#
<%
@ Page Language
=
”C#”
%>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
public TextBox pp_TextBox1
{
get
{
return TextBox1;
}
}
public Calendar pp_Calendar1
{
get
{
return Calendar1;
}
}
protected void Button1_Click ( object sender, System.EventArgs e)
{
Label1.Text = “Hello “ + TextBox1.Text + “<br />” +
“Date Selected: “ + Calendar1.SelectedDate.ToShortDateString();
}
</ script >
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
public TextBox pp_TextBox1
{
get
{
return TextBox1;
}
}
public Calendar pp_Calendar1
{
get
{
return Calendar1;
}
}
protected void Button1_Click ( object sender, System.EventArgs e)
{
Label1.Text = “Hello “ + TextBox1.Text + “<br />” +
“Date Selected: “ + Calendar1.SelectedDate.ToShortDateString();
}
</ script >
Listing 3-12: Consuming the exposed properties from the first page
VB
<
%@ Page Language
=
”VB” %
>
< %@ PreviousPageType VirtualPath = ”Page1.aspx” % >
< !DOCTYPE html PUBLIC “ -// W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org / TR / xhtml11 / DTD / xhtml11.dtd” >
< script runat = ”server” >
Protected Sub Page_Load() Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = “Hello “ & PreviousPage.pp_Textbox1.Text & “<br />” & _
“Date Selected: “ & _
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString()
End Sub
</ script >
< %@ PreviousPageType VirtualPath = ”Page1.aspx” % >
< !DOCTYPE html PUBLIC “ -// W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org / TR / xhtml11 / DTD / xhtml11.dtd” >
< script runat = ”server” >
Protected Sub Page_Load() Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = “Hello “ & PreviousPage.pp_Textbox1.Text & “<br />” & _
“Date Selected: “ & _
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString()
End Sub
</ script >
c#
<%
@ Page Language
=
”C#”
%>
<% @ PreviousPageType VirtualPath = ”Page1.aspx” %>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected void Page_Load( object sender, System.EventArgs e)
{
Label1.Text = “Hello “ + PreviousPage.pp_TextBox1.Text + “<br />” +
“Date Selected: “ +
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
</ script >
<% @ PreviousPageType VirtualPath = ”Page1.aspx” %>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected void Page_Load( object sender, System.EventArgs e)
{
Label1.Text = “Hello “ + PreviousPage.pp_TextBox1.Text + “<br />” +
“Date Selected: “ +
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
</ script >
为了使用Page1.aspx的属性,上面代码必须加入<%@ PreviousPageType VirtualPath=”Page1.aspx” %>,之后可以通过IntelliSense智能感看到PreviousPage属性。
注意:你仍能使用Page1.aspx中button的OnClick事件,这时先提交到Page1.aspx,然后再跨页面提交到Page2.aspx。
最后,怎么能知道请求来自于Page1.aspx,还是来自于Page2.aspx呢?
使用IsCrossPagePostBack属性
Listing 3-13: Using the IsCrossPagePostBack property
VB
<
%@ Page Language
=
”VB” %
>
< %@ PreviousPageType VirtualPath = ”Page1.aspx” % >
< !DOCTYPE html PUBLIC “ -// W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org / TR / xhtml11 / DTD / xhtml11.dtd” >
< script runat = ”server” >
Protected Sub Page_Load() Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Page.IsCrossPagePostBack Then
Label1.Text = “Hello “ & PreviousPage.pp_Textbox1.Text & “<br />” & _
“Date Selected: “ & _
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString()
Else
Response.Redirect(“Page1.aspx”)
End If
End Sub
</ script >
< %@ PreviousPageType VirtualPath = ”Page1.aspx” % >
< !DOCTYPE html PUBLIC “ -// W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org / TR / xhtml11 / DTD / xhtml11.dtd” >
< script runat = ”server” >
Protected Sub Page_Load() Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Page.IsCrossPagePostBack Then
Label1.Text = “Hello “ & PreviousPage.pp_Textbox1.Text & “<br />” & _
“Date Selected: “ & _
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString()
Else
Response.Redirect(“Page1.aspx”)
End If
End Sub
</ script >
c#
<%
@ Page Language
=
”C#”
%>
<% @ PreviousPageType VirtualPath = ”Page1.aspx” %>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected void Page_Load( object sender, System.EventArgs e)
{
if (Page.IsCrossPagePostBack) {
Label1.Text = “Hello “ + PreviousPage.pp_Textbox1.Text + “<br />” +
“Date Selected: “ +
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
else
{
Response.Redirect(“Page1.aspx”);
}
}
</ script >
<% @ PreviousPageType VirtualPath = ”Page1.aspx” %>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected void Page_Load( object sender, System.EventArgs e)
{
if (Page.IsCrossPagePostBack) {
Label1.Text = “Hello “ + PreviousPage.pp_Textbox1.Text + “<br />” +
“Date Selected: “ +
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
else
{
Response.Redirect(“Page1.aspx”);
}
}
</ script >