Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Controls Server Event Handling using ASP.NET 2.0 and C#
Controls Server Event Handling using ASP.NET 2.0 and C#


ASP.NET Controls Tutorial

This tutorial will show Controls how to Handle Server Event using ASP.NET 2.0 and C#

An important feature of ASP.NET is that it allows you to program Web pages using an event-based model that is similar to that in client applications.If you already have an event handler, you can bind several control events to it. These multiple events can be from the same control or one event from several different controls, as long as the events all have the same method signature. 

This tutorial only using the default namespace.The System namespace contains the EventHandler.

We use the Buttonx_Click event to connect multiple events to a single event handler. In the other word, Button1,Button2,Button3(multiple events will be connected to single event handler) will call same event handler. But this event handler will determine which Web Server Control Raised an Event. And then, we use the Button6_Click event to call eventd of  both Button4 and Button5.

protected void Buttonx_Click(object sender, EventArgs e)
{
Button b;
b = (Button)sender;
switch (b.ID)
{
case "Button1":
Label1.Text = "You clicked the first button";
break;
case "Button2":
Label1.Text = "You clicked the second button";
break;
case "Button3":
Label1.Text = "You clicked the third button";
break;
}
}

protected void Button6_Click(object sender, EventArgs e)
{

}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

The front end .aspx page looks something like this:

<table width="500" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" class="basix" height="50">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Button ID="Button1" runat="server" OnClick="Buttonx_Click" Text="Button1" />
<br />
<asp:Button ID="Button2" runat="server" OnClick="Buttonx_Click" Text="Button2" /><br />
<asp:Button ID="Button3" runat="server" OnClick="Buttonx_Click" Text="Button3" />
<br />
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Button4" />
<br />
<asp:Button ID="Button5" runat="server" Enabled="False" OnClick="Button5_Click" Text="Button5" /><br />
<asp:Button ID="Button6" runat="server" OnClick="Button6_Click" Text="Button6" /><br />
</td>
</tr>
</table>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

The flow for the code behind page is as follows.

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 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button6.Click += new System.EventHandler(Button4_Click);
Button6.Click += new System.EventHandler(Button5_Click);
}

protected void Buttonx_Click(object sender, EventArgs e)
{
Button b;
b = (Button)sender;
switch (b.ID)
{
case "Button1":
Label1.Text = "You clicked the first button";
break;
case "Button2":
Label1.Text = "You clicked the second button";
break;
case "Button3":
Label1.Text = "You clicked the third button";
break;
}
}

protected void Button4_Click(object sender, EventArgs e)
{
Label1.Text = "You clicked the 4th button";
}

protected void Button5_Click(object sender, EventArgs e)
{
Label1.Text = Label1.Text+"You clicked the 5th button";
}

protected void Button6_Click(object sender, EventArgs e)
{

}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks