推荐使用,可踌页面传值或对象.关于其他页面传值方式,
开始正题:page1.aspx前台代码
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
first Page
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
输入你的名字:
<
br
/>
<
asp:TextBox ID
=
"
TextBox1
"
runat
=
"
server
"
>

</
asp:TextBox
>
<
p
>
选择一个日期
<
br
/>
<
asp:Calendar ID
=
"
Calendar1
"
runat
=
"
server
"
BackColor
=
"
White
"
BorderColor
=
"
Black
"
BorderStyle
=
"
Solid
"
CellSpacing
=
"
1
"
Font
-
Names
=
"
Verdana
"
Font
-
Size
=
"
9pt
"
ForeColor
=
"
Black
"
Height
=
"
250px
"
NextPrevFormat
=
"
ShortMonth
"
Width
=
"
330px
"
>
<
SelectedDayStyle BackColor
=
"
#333399
"
ForeColor
=
"
White
"
/>
<
TodayDayStyle BackColor
=
"
#999999
"
ForeColor
=
"
White
"
/>
<
DayStyle BackColor
=
"
#CCCCCC
"
/>
<
OtherMonthDayStyle ForeColor
=
"
#999999
"
/>
<
NextPrevStyle Font
-
Bold
=
"
True
"
Font
-
Size
=
"
8pt
"
ForeColor
=
"
White
"
/>
<
DayHeaderStyle Font
-
Bold
=
"
True
"
Font
-
Size
=
"
8pt
"
ForeColor
=
"
#333333
"
Height
=
"
8pt
"
/>
<
TitleStyle BackColor
=
"
#333399
"
BorderStyle
=
"
Solid
"
Font
-
Bold
=
"
True
"
Font
-
Size
=
"
12pt
"
ForeColor
=
"
White
"
Height
=
"
12pt
"
/>
</
asp:Calendar
>
</
p
>
<
br
/>
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
Text
=
"
提交到当前页
"
OnClick
=
"
Button1_Click
"
/>
<
asp:Button ID
=
"
Button2
"
runat
=
"
server
"
Text
=
"
提交到page2.aspx
"
PostBackUrl
=
"
Page2.aspx
"
/>
<
p
>
<
asp:Label ID
=
"
Label1
"
runat
=
"
server
"
></
asp:Label
></
p
>
</
div
>
</
form
>
</
body
>
</
html
>
Protected
Sub Button1_Click()
Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Hello " & TextBox1.Text & "<br />" & _
"日期选择了: " & Calendar1.SelectedDate.ToShortDateString()
End Sub
Protected
Sub Page_Load()
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'当上页属性值不为空时
If Not Page.PreviousPage Is Nothing Then
Dim p_textbox1 As TextBox = CType(PreviousPage.FindControl("textbox1"), TextBox)
Dim p_Calendar1 As Calendar = CType(PreviousPage.FindControl("calendar1"), Calendar)
Label1.Text = "Hello " & p_textbox1.Text & "<br />" & _
"日期选择了: " & p_Calendar1.SelectedDate.ToShortDateString()
End If
End Sub
说明:Page1.aspx上的Button1_Click事件处理页面上服务器控件包含的值
.第二个按钮Button2完全不同。与第一个按钮不同,这个按钮不包含OnClick事件,它使用的是PostBackUrl属性。这个属性带一个字符串值,指向页面要传送到的文件位置。在本例中是Page2.aspx。这说明,现在Page2.aspx接收回送的内容和包含在Page1.aspx控件中的所有值。
要获得从前一个页面传送过来的控件值,只需创建该控件类型的一个实例,并用PreviousPage属性中的FindControl方法填充该实例。赋予FindControl方法的String值是Id值,它用于前一个页面上的服务器控件。赋予了值之后,就可以处理该服务器控件及其值了,就好像它最初位于当前页面上一样。从例子中可以看出,可以从控件中提取Text和SelectedDate属性.
来自于MSDN或实例文档
本文介绍ASP.NET中页面间的传值方法,包括通过Button控件的PostBackUrl属性提交表单到另一个页面,并演示如何在目标页面获取源页面中TextBox和Calendar控件的值。
1万+

被折叠的 条评论
为什么被折叠?



