在A(yíng)SP.NET 2.0中,一個(gè)ASP.NET頁(yè)面的生命周期主要為:
其運行結果為:
<html>
<head> <title>Untitled Page</title>
<meta name="author" content="brooks" />
</head>
定義表單中的默認按鈕:
在A(yíng)SP.NET1.0中,我就為了設置表單中的默認按鈕而一籌莫展。幸好ASP.NET2.0把這個(gè)功能補上了,現在可以非常方便的設置表單中的默認按鈕了。
<%@ page language="C#" %>
<script runat="server">
void Button1_Click(object sender, System.EventArgs e)
{
this.LB_Message.Text = "You clicked button1";
}
</script>
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form runat="server" defaultbutton="Button1">
<asp:textbox id="Textbox1" runat="server"></asp:textbox>
<asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" />
<asp:label id="LB_Message" runat="server"></asp:label>
</form>
</body>
</html>
設置焦點(diǎn):
現在假設為T(mén)extBox1控件設置焦點(diǎn),在A(yíng)SP.NET 2.0中可以這樣實(shí)現:
this.Textbox1.Focus(); 或 this.SetFocus(this.Textbox1); 即可為T(mén)extBox1控件設置焦點(diǎn)。
如果打算也為表單設置個(gè)默認焦點(diǎn)控件,讓光標默認停留在TextBox1上:
<form runat="server" defaultfocus="TextBox1">
跨頁(yè)面數據發(fā)送:
如果你需要多個(gè)頁(yè)面發(fā)送數據到同一個(gè)表單程序進(jìn)行處理,或者數據在多個(gè)頁(yè)面之間傳輸處理的話(huà),你就可以使用ASP.NET 2.0這個(gè)新特性。例如,我打算把Default.aspx頁(yè)里TextBox1里的文本數據發(fā)送到Default2.aspx頁(yè)面進(jìn)行處理:
Default.aspx頁(yè):
<%@ Page Language="C#" %>
<script runat="server">
void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "Hi," + TextBox1.Text + ". This is Default.aspx";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
<asp:Button ID="Button1" Runat="server" Text="PostToAnotherPage" PostBackUrl="~/Default2.aspx" />
<asp:Button ID="Button2" Runat="server" Text="PostToSelf" OnClick="Button2_Click" />
<br />
<asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
Default2.aspx頁(yè):
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
TextBox textBox1 = (TextBox)PreviousPage.FindControl("TextBox1");
this.Label1.Text = "Hi," + textBox1.Text + ". This is Default2.aspx!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:label id="Label1" runat="server"></asp:label>
</form>
</body>
</html>
聯(lián)系客服