ASP.NET Events Handling
ASP.NET Events Handling
M.SARULATHAAssistant ProfessorDept. of CA,JJC.
ASP.NET
provides important feature event handling to Web Forms. It let us to implement
event-based model for our application. As a simple example, we can add a button
to an ASP.NET Web Forms page and then write an event handler for the button's
click event. ASP.NET Web Forms allows events on both client and server sides.
In
ASP.NET Web Forms pages, however, events associated with server controls
originate on the client but are handled on the Web server by the ASP.NET.
ASP.NET
Web Forms follow a standard .NET Framework pattern for event-handler methods.
All events pass two arguments: an object representing the object that raised
the event, and an event object containing any event-specific information.
Example: Creating Event Handler
Here, we are creating an event
handler for click event. In this example, when user click on the button, an
event fires and handler code executes at server side.
// EventHandling.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EnventHandling.aspx.cs"
Inherits="asp.netexample.EnventHandling" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 108px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">First value</td>
<td>
<asp:TextBox ID="firstvalue" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Second value</td>
<td>
<asp:TextBox ID="secondvalue" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Sum</td>
<td>
<asp:TextBox ID="total" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<br/>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"Text="Sum"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code Behind
// EventHandling.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace asp.netexample
{
public partial class EnventHandling : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(firstvalue.Text);
int b = Convert.ToInt32(secondvalue.Text);
total.Text = (a + b).ToString();
}
}
}
Output:

Comments
Post a Comment