ASP.NET Tutorial/ASP.net Controls/Events
Содержание
- 1 EventHandler
- 2 ASP.NET Server Controls
EventHandler
<source lang="csharp">
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %> <!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>ASP.NET Server Controls</title>
</head> <body>
<form id="form1" runat="server">
ASP.NET Server Controls
The date and time is <% =DateTime.Now.ToString() %>.
<asp:TextBox ID="txtBookName" runat="server" Width="250px">Enter a book name.</asp:TextBox> <asp:Button ID="btnBookName" runat="server" Text="Book Name" OnClick="btnBookName_Click" /> <asp:Label ID="lblBookName" runat="server"></asp:Label>
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; 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 Default_aspx : System.Web.UI.Page {
public Default_aspx() { Load += new EventHandler(Page_Load); } protected void btnBookName_Click(object sender, EventArgs e) { lblBookName.Text = txtBookName.Text; } }</source>
Get event dispacher from event argument
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="EventTracker" %> <!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">
List of events:
<asp:ListBox id="lstEvents" runat="server" Height="107px" Width="355px"> </asp:ListBox>
Controls being monitored for change events:
<asp:TextBox id="txt" runat="server" AutoPostBack="True" OnTextChanged="CtrlChanged"></asp:TextBox>
<asp:CheckBox id="chk" runat="server" AutoPostBack="True" OnCheckedChanged="CtrlChanged"> </asp:CheckBox>
<asp:RadioButton id="opt1" runat="server" GroupName="Sample" AutoPostBack="True" OnCheckedChanged="CtrlChanged"> </asp:RadioButton> <asp:RadioButton id="opt2" runat="server" GroupName="Sample" AutoPostBack="True" OnCheckedChanged="CtrlChanged"> </asp:RadioButton>
</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 EventTracker : System.Web.UI.Page {
protected void CtrlChanged(Object sender, EventArgs e) { string ctrlName = ((Control)sender).ID; lstEvents.Items.Add(ctrlName + " Changed"); lstEvents.SelectedIndex = lstEvents.Items.Count - 1; }
}</source>
The Button_Command event (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
protected void Button_Command(Object sender, System.Web.UI.WebControls.rumandEventArgs e) { switch (e.rumandName) { case ("DoSomething1"): Response.Write("Button 1 was selected"); break; case ("DoSomething2"): Response.Write("Button 2 was selected"); break; } }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Buttons</title>
</head> <body>
<form id="form1" runat="server">
<asp:Button ID="Button1" Runat="server" Text="Button 1" OnCommand="Button_Command" CommandName="DoSomething1" /> <asp:Button ID="Button2" Runat="server" Text="Button 2" OnCommand="Button_Command" CommandName="DoSomething2" />
</form>
</body> </html></source>
The Button_Command event (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
Protected Sub Button_Command(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.rumandEventArgs) Select Case e.rumandName Case "DoSomething1" Response.Write("Button 1 was selected") Case "DoSomething2" Response.Write("Button 2 was selected") End Select End Sub
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Buttons</title>
</head> <body>
<form id="form1" runat="server">
<asp:Button ID="Button1" Runat="server" Text="Button 1" OnCommand="Button_Command" CommandName="DoSomething1" /> <asp:Button ID="Button2" Runat="server" Text="Button 2" OnCommand="Button_Command" CommandName="DoSomething2"/>
</form>
</body> </html></source>
Two types of events for the button (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
protected void Button1_Click(object sender, EventArgs e) { Response.Write("Postback!"); }
</script> <script language="javascript" type="text/javascript">
function AlertHello() { alert("Hello ASP.NET"); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Button Server Control</title>
</head> <body>
<form id="form1" runat="server"> <asp:Button ID="Button1" Runat="server" Text="Button" OnClientClick="AlertHello()" OnClick="Button1_Click" /> </form>
</body> </html></source>
Two types of events for the button (VB)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Response.Write("Postback!") End Sub
</script> <script language="javascript" type="text/javascript">
function AlertHello() { alert("Hello ASP.NET"); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Button Server Control</title>
</head> <body>
<form id="form1" runat="server"> <asp:Button ID="Button1" Runat="server" Text="Button" OnClientClick="AlertHello()" OnClick="Button1_Click" /> </form>
</body> </html></source>