Professional ASP.NET 2.0之跨页提交-Cross Page Posting

先看代码
Listing 3-9: Page1.aspx
VB 
None.gif <% @ Page Language = ”VB”  %>
None.gif
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
None.gif
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
None.gif
< script runat = ”server” >
None.gifProtected Sub Button1_Click(ByVal sender As Object, _
None.gifByVal e As System.EventArgs)
None.gifLabel1.Text 
=  “Hello “  &  TextBox1.Text  &  “ < br  /> ”  &  _
None.gif“Date Selected: “ 
&  Calendar1.SelectedDate.ToShortDateString()
None.gifEnd Sub
None.gif
</ script >
None.gif
< html xmlns = ”http: // www.w3.org/1999/xhtml” >
None.gif
< head runat = ”server” >
None.gif
< title > First Page </ title >
None.gif
</ head >
None.gif
< body >
None.gif
< form id = ”form1” runat = ”server” >
None.gifEnter your name:
< br  />
None.gif
< asp:Textbox ID = ”TextBox1” Runat = ”server” >
None.gif
</ asp:Textbox >
None.gif
< p >
None.gifWhen 
do  you want to fly ?< br  />
None.gif
< asp:Calendar ID = ”Calendar1” Runat = ”server” ></ asp:Calendar ></ p >
None.gif
< br  />
None.gif
< asp:Button ID = ”Button1” Runat = ”server” Text = ”Submit page to itself”
None.gifOnClick
= ”Button1_Click”  />
None.gif
< asp:Button ID = ”Button2” Runat = ”server” Text = ”Submit page to Page2.aspx”
None.gifPostBackUrl
= ”Page2.aspx”  />
None.gif
< p >
None.gif
< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label ></ p >
None.gif
</ form >
None.gif
</ body >
None.gif
</ html >

C#
None.gif <% @ Page Language = ”C#”  %>
None.gif
< script runat = ”server” >
None.gif
protected   void  Button1_Click ( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifLabel1.Text 
= “Hello “ + TextBox1.Text + “<br />” +
InBlock.gif“Date Selected: “ 
+ Calendar1.SelectedDate.ToShortDateString();
ExpandedBlockEnd.gif}

None.gif
</ 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
None.gif <% @ Page Language = ”VB”  %>
None.gif
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
None.gif
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
None.gif
< script runat = ”server” >
None.gifProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
None.gifDim pp_Textbox1 As TextBox
None.gifDim pp_Calendar1 As Calendar
None.gifpp_Textbox1 
=  CType(PreviousPage.FindControl(“Textbox1”), TextBox)
None.gifpp_Calendar1 
=  CType(PreviousPage.FindControl(“Calendar1”), Calendar)
None.gifLabel1.Text 
=  “Hello “  &  pp_Textbox1.Text  &  “ < br  /> ”  &  _
None.gif“Date Selected: “ 
&  pp_Calendar1.SelectedDate.ToShortDateString()
None.gifEnd Sub
None.gif
</ script >
None.gif
< html xmlns = ”http: // www.w3.org/1999/xhtml” >
None.gif
< head runat = ”server” >
None.gif
< title > Second Page </ title >
None.gif
</ head >
None.gif
< body >
None.gif
< form id = ”form1” runat = ”server” >
None.gif
< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label >
None.gif
</ form >
None.gif
</ body >
None.gif
</ html >

C#

None.gif <% @ Page Language = ”C#”  %>
None.gif
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
None.gif
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
None.gif
< script runat = ”server” >
None.gif
protected   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifTextBox pp_Textbox1;
InBlock.gifCalendar pp_Calendar1;
InBlock.gifpp_Textbox1 
= (TextBox)PreviousPage.FindControl(“Textbox1”);
InBlock.gifpp_Calendar1 
= (Calendar)PreviousPage.FindControl(“Calendar1”);
InBlock.gifLabel1.Text 
= “Hello “ + pp_Textbox1.Text + “<br />” + “Date Selected: “ +
InBlock.gifpp_Calendar1.SelectedDate.ToShortDateString();
ExpandedBlockEnd.gif}

None.gif
</ 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

None.gif <% @ Page Language = ”VB”  %>
None.gif
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
None.gif
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
None.gif
< script runat = ”server” >
None.gifPublic ReadOnly Property pp_TextBox1() As TextBox
None.gifGet
None.gifReturn TextBox1
None.gifEnd Get
None.gifEnd Property
None.gifPublic ReadOnly Property pp_Calendar1() As Calendar
None.gifGet
None.gifReturn Calendar1
None.gifEnd Get
None.gifEnd Property
None.gifProtected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
None.gifLabel1.Text 
=  “Hello “  &  TextBox1.Text  &  “ < br  /> ”  &  _
None.gif“Date Selected: “ 
&  Calendar1.SelectedDate.ToShortDateString()
None.gifEnd Sub
None.gif
</ script >

C#

None.gif <% @ Page Language = ”C#”  %>
None.gif
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
None.gif
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
None.gif
< script runat = ”server” >
None.gif
public  TextBox pp_TextBox1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return TextBox1;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
public  Calendar pp_Calendar1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return Calendar1;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
protected   void  Button1_Click ( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifLabel1.Text 
= “Hello “ + TextBox1.Text + “<br />” +
InBlock.gif“Date Selected: “ 
+ Calendar1.SelectedDate.ToShortDateString();
ExpandedBlockEnd.gif}

None.gif
</ script >

Listing 3-12: Consuming the exposed properties from the first page
VB

 

None.gif < %@ Page Language = ”VB” % >
None.gif
< %@ PreviousPageType VirtualPath = ”Page1.aspx” % >
None.gif
< !DOCTYPE html  PUBLIC  “ -// W3C // DTD XHTML  1.1 // EN”
None.gif“http:
// www.w3.org / TR / xhtml11 / DTD / xhtml11.dtd” >
None.gif
< script runat = ”server” >
ExpandedBlockStart.gifContractedBlock.gif
Protected   Sub Page_Load() Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs)
InBlock.gifLabel1.Text 
= “Hello “ & PreviousPage.pp_Textbox1.Text & “<br />” & _
InBlock.gif
Date Selected: “ & _
InBlock.gifPreviousPage.pp_Calendar1.SelectedDate.ToShortDateString()
ExpandedBlockEnd.gif
End Sub

None.gif
</ script >

c#

None.gif <% @ Page Language = ”C#”  %>
None.gif
<% @ PreviousPageType VirtualPath = ”Page1.aspx”  %>
None.gif
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
None.gif
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
None.gif
< script runat = ”server” >
None.gif
protected   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifLabel1.Text 
= “Hello “ + PreviousPage.pp_TextBox1.Text + “<br />” +
InBlock.gif“Date Selected: “ 
+
InBlock.gifPreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
ExpandedBlockEnd.gif}

None.gif
</ 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

None.gif < %@ Page Language = ”VB” % >
None.gif
< %@ PreviousPageType VirtualPath = ”Page1.aspx” % >
None.gif
< !DOCTYPE html  PUBLIC  “ -// W3C // DTD XHTML  1.1 // EN”
None.gif“http:
// www.w3.org / TR / xhtml11 / DTD / xhtml11.dtd” >
None.gif
< script runat = ”server” >
ExpandedBlockStart.gifContractedBlock.gif
Protected   Sub Page_Load() Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs)
InBlock.gif
If Page.IsCrossPagePostBack Then
InBlock.gifLabel1.Text 
= “Hello “ & PreviousPage.pp_Textbox1.Text & “<br />” & _
InBlock.gif
Date Selected: “ & _
InBlock.gifPreviousPage.pp_Calendar1.SelectedDate.ToShortDateString()
InBlock.gif
Else
InBlock.gifResponse.Redirect(“Page1.aspx”)
InBlock.gif
End If
ExpandedBlockEnd.gif
End Sub

None.gif
</ script >

c#

None.gif <% @ Page Language = ”C#”  %>
None.gif
<% @ PreviousPageType VirtualPath = ”Page1.aspx”  %>
None.gif
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
None.gif
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
None.gif
< script runat = ”server” >
None.gif
protected   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (Page.IsCrossPagePostBack) dot.gif{
InBlock.gifLabel1.Text 
= “Hello “ + PreviousPage.pp_Textbox1.Text + “<br />” +
InBlock.gif“Date Selected: “ 
+
InBlock.gifPreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifResponse.Redirect(“Page1.aspx”);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
</ script >






 

转载于:https://www.cnblogs.com/zhhui/archive/2006/03/20/354245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值