ASP.NET Tutorial/Page Lifecycle/Controls — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:00, 26 мая 2010
Содержание
[убрать]Add a control to a page dynamically
<%@ 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">
<div>
<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><br/>
<hr/>
</asp:Panel>
</div>
</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);
}
}
}
Controls.Count
<%@ 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.<br/>"
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>
Grab Browser Info
<%@ 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">
<div>
<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" />
</div>
</form>
</body>
</html>
Use the input focus capability of ASP.NET 2.0 and superior
<%@ 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>
<div id="pageContent">
<form id="Form1" runat="server">
<h1>Please, login</h1>
<h3>User</h3>
<asp:textbox runat="server" id="UserName" text="" /><br />
<h3>Password</h3>
<asp:textbox runat="server" id="Pswd" text="" TextMode="Password" /><br />
<asp:button runat="server" id="Login" text="Log in" />
<hr />
</form>
</div>
</body>
</html>