ASP.NET Tutorial/Page Lifecycle/Controls
Содержание
Add a control to a page dynamically
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DynamicButton" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
<asp:Panel id="Panel1" runat="server" Width="364px" Height="142px"> <asp:Label id=Label1 runat="server">(No value.)</asp:Label> <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder> <asp:Button id="cmdReset" runat="server" Width="112px" Text="Reset Text" OnClick="cmdReset_Click"></asp:Button>
</asp:Panel>
</form>
</body> </html>
File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class DynamicButton : System.Web.UI.Page {
private void Page_Load(object sender, EventArgs e) { Button newButton = new Button(); newButton.Text = "Dynamic Button"; newButton.ID = "newButton"; newButton.Click += new System.EventHandler(this.Button_Click); PlaceHolder1.Controls.Add(newButton); } protected void Button_Click(object sender, System.EventArgs e) { Label1.Text = "You clicked the dynamic button."; } protected void cmdReset_Click(object sender, System.EventArgs e) { Label1.Text = "(No value.)"; } protected void cmdRemove_Click(object sender, System.EventArgs e) { Button foundButton = (Button)Page.FindControl("newButton"); if (foundButton != null) { foundButton.Parent.Controls.Remove(foundButton); } }
}</source>
Controls.Count
<source lang="csharp">
<%@ Page Language="vb" %> <html>
<head> <title>Controls collection example</title> <script runat="server"> Sub Page_Load() Message.Text = "There are currently " & Controls.Count & _ " controls on the page.
" Dim Message2 As New Label Controls.AddAt(Controls.Count - 1, Message2) Message2.Text = "There are now " & Controls.Count & _ " controls on the page." End Sub </script> </head>
<body>
<asp:label id="Message" runat="server"/>
</body> </html></source>
Grab Browser Info
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Grab Browser Info</title>
</head> <body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="I"m a Button" ie:Text="IE Button" mozilla:Text="Firefox Button" /> <asp:CheckBox ID="CheckBox1" runat="server" Text="I"m a CheckBox" ie:Text="IE CheckBox" mozilla:Text="Firefox CheckBox" />
</form>
</body> </html></source>
Use the input focus capability of ASP.NET 2.0 and superior
<source lang="csharp">
<%@ Page language="C#" %> <script runat="server">
void Page_Load(object sender, EventArgs e) { if (!IsPostBack) this.SetFocus("Pswd"); }
</script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
<title>Focus to the control</title>
</head> <body>
<form id="Form1" runat="server">
Please, login
User
<asp:textbox runat="server" id="UserName" text="" />
Password
<asp:textbox runat="server" id="Pswd" text="" TextMode="Password" />
<asp:button runat="server" id="Login" text="Log in" />
</form>
</body> </html></source>