ASP.NET Tutorial/ASP.net Controls/Events

Материал из .Net Framework эксперт
Версия от 12:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

EventHandler

<%@ 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">
    <div>
      <h1>ASP.NET Server Controls</h1>
      <h2>The date and time is <% =DateTime.Now.ToString() %>.</h2>
          <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>
    </div>
    </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;
    }
 }


Get event dispacher from event argument

<%@ 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">
    <div>
      <h3>List of events:</h3>      
    <asp:ListBox id="lstEvents" 
                 runat="server" 
                 Height="107px" 
                 Width="355px">
    </asp:ListBox><br/>
    <br/><br/>
    <h3>Controls being monitored for change events:</h3>
    <asp:TextBox id="txt" 
                 runat="server" 
                 AutoPostBack="True" 
                 OnTextChanged="CtrlChanged"></asp:TextBox><br/>
    <br/>
    <asp:CheckBox id="chk" 
                  runat="server" 
                  AutoPostBack="True" 
                  OnCheckedChanged="CtrlChanged">
    </asp:CheckBox><br/>
    <br/>
    <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>
    </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 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;
  }
}


The Button_Command event (C#)

<%@ 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">
    <div>
        <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" />    
    </div>
    </form>
</body>
</html>


The Button_Command event (VB.net)

<%@ 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">
    <div>
        <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"/>
    </div>
    </form>
</body>
</html>


Two types of events for the button (C#)

<%@ 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>


Two types of events for the button (VB)

<%@ 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>