обработчик событий

 
 

двойной клик на элементе

Сделайте двойной клик на элементе.
Visual Studio создает обработчик события.
Скрыть

Показать

Копировать
  Default.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
   <br />
   <br />
   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  Default.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication1 {
 public partial class Default : System.Web.UI.Page {
  protected void Page_Load(object sender, EventArgs e) {
 
  }
 
  protected void Button1_Click(object sender, EventArgs e) {
   Label1.Text = "Hello World!";
  }
 }
}
 
 

из окна свойств

Сделайте одиночный клик на элементе. Нажмите F4. Перейдите в окно свойств. Кликните на иконку событий.
Скрыть

Показать

Копировать
  Default.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
   <br />
   <br />
   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  Default.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication1 {
 public partial class Default : System.Web.UI.Page {
  protected void Page_Load(object sender, EventArgs e) {
 
  }
 
  protected void Button1_Click(object sender, EventArgs e) {
   Label1.Text = "Hello World!";
  }
 }
}
 
 

на странице разметки

Добавьте обработчик событий на страницу вручную.
Скрыть

Показать

Копировать
  Default.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
 
<!DOCTYPE html>
<script runat="server">
 //добавляем обработчик событий
 protected void Button1_Click(object sender, EventArgs e) {
  Label1.Text = "Hello World!";
 }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <!--добавьте атрибут OnClick-->
   <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
   <br />
   <br />
   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  </div>
 </form>
</body>
</html>
 
 

при отключенной автоматической привязке событий

Для отключения автоматической привязки событий, нужно для атрибута AutoEventWireup задать значение false
Скрыть

Показать

Копировать
  Default.aspx  
<!--нужно для атрибута AutoEventWireup задать значение false-->
<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
   <br />
   <br />
   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  Default.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication1 {
 public partial class Default : System.Web.UI.Page {
  protected void Page_Load(object sender, EventArgs e) {
   Button1.Click += new EventHandler(Button1_Click);
  }
  protected void Button1_Click(object sender, EventArgs e) {
   Label1.Text = "Hello World!";
  }
 }
}