在ASP.NET 2.0中,一個ASP.NET頁面的生命周期主要為:
其運(yùn)行結(jié)果為:
<html>
<head> <title>Untitled Page</title>
<meta name="author" content="brooks" />
</head>
定義表單中的默認(rèn)按鈕:
在ASP.NET1.0中,我就為了設(shè)置表單中的默認(rèn)按鈕而一籌莫展。幸好ASP.NET2.0把這個功能補(bǔ)上了,現(xiàn)在可以非常方便的設(shè)置表單中的默認(rèn)按鈕了。
<%@ 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>
設(shè)置焦點(diǎn):
現(xiàn)在假設(shè)為TextBox1控件設(shè)置焦點(diǎn),在ASP.NET 2.0中可以這樣實(shí)現(xiàn):
this.Textbox1.Focus(); 或 this.SetFocus(this.Textbox1); 即可為TextBox1控件設(shè)置焦點(diǎn)。
如果打算也為表單設(shè)置個默認(rèn)焦點(diǎn)控件,讓光標(biāo)默認(rèn)停留在TextBox1上:
<form runat="server" defaultfocus="TextBox1">
跨頁面數(shù)據(jù)發(fā)送:
如果你需要多個頁面發(fā)送數(shù)據(jù)到同一個表單程序進(jìn)行處理,或者數(shù)據(jù)在多個頁面之間傳輸處理的話,你就可以使用ASP.NET 2.0這個新特性。例如,我打算把Default.aspx頁里TextBox1里的文本數(shù)據(jù)發(fā)送到Default2.aspx頁面進(jìn)行處理:
Default.aspx頁:
<%@ 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頁:
<%@ 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)系客服