Introduction: In this article i will explain how to call common event handler for multiple button. In this article I will execute the operations like Sum, subtraction, multiplication and division on two numbers by creating a common event handler method in asp.net using both C# and VB languages.
The main benefit of using
this concept is that we don't need to write the code on each button, we can
create a single common function that handle and respond to all the buttons to perform their specific task.
Implementation: Let's create a simple web application to perform the operations like Sum, subtraction, multiplication and division on two numbers.
Implementation: Let's create a simple web application to perform the operations like Sum, subtraction, multiplication and division on two numbers.
Asp.Net C# Section:
Design Section:
<fieldset style="width: 400px;">
<legend><strong>Use of common handler in Asp.net C#</strong></legend>
<table
width="100%">
<tr>
<td>Enter First Number: </td>
<td><asp:TextBox ID="txtFirstNum"
runat="server"
Width="60px"></asp:TextBox>
</td>
<td> </td>
<td
rowspan="4"
align="center"
valign="top"><strong>Result</strong><br /><br />
<asp:TextBox ID="txtResult"
runat="server"
Width="60px"></asp:TextBox>
</td>
</tr>
<tr>
<td>Enter Second Number: </td>
<td><asp:TextBox ID="txtSecondNum"
runat="server"
Width="60px"></asp:TextBox></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td></td>
<td> </td>
</tr>
<tr>
<td
colspan="2"
align="center">
<asp:Button ID="btnSum" runat="server" onclick="Calculate" Text="Sum" />
<asp:Button ID="btnSub" runat="server" onclick="Calculate" Text="Subtract" />
<asp:Button ID="btnMul" runat="server" onclick="Calculate" Text="Multiply"/>
<asp:Button ID="btnDiv" runat="server" onclick="Calculate" Text="Divide"/>
</td>
<td>
</td>
</tr>
</table>
</fieldset>
In design section, all the button are calling
the same event handler on click of button.
Now
create the common event handler function in the Code behind file that will
respond to all the buttons:
protected void Calculate(object sender, EventArgs e)
{
if (sender
== btnSum)
{
txtResult.Text = Convert.ToString(Convert.ToInt32(txtFirstNum.Text)
+Convert.ToInt32(txtSecondNum.Text));
}
else if (sender == btnSub)
{
txtResult.Text = Convert.ToString(Convert.ToInt32(txtFirstNum.Text)
-Convert.ToInt32(txtSecondNum.Text));
}
else if (sender == btnMul)
{
txtResult.Text = Convert.ToString(Convert.ToInt32(txtFirstNum.Text)
*Convert.ToInt32(txtSecondNum.Text));
}
else
{
txtResult.Text = Convert.ToString(Convert.ToInt32(txtFirstNum.Text)
/Convert.ToInt32(txtSecondNum.Text));
}
}
Asp.Net VB Section
<fieldset style="width: 400px;">
<legend><strong>Use of common handler in Asp.net C#</strong></legend>
<table
width="100%">
<tr>
<td>Enter First Number: </td>
<td><asp:TextBox ID="txtFirstNum"
runat="server"
Width="60px"></asp:TextBox>
</td>
<td> </td>
<td
rowspan="4"
align="center"
valign="top"><strong>Result</strong><br /><br />
<asp:TextBox ID="txtResult"
runat="server"
Width="60px"></asp:TextBox>
</td>
</tr>
<tr>
<td>Enter Second Number: </td>
<td><asp:TextBox ID="txtSecondNum"
runat="server"
Width="60px"></asp:TextBox></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td></td>
<td> </td>
</tr>
<tr>
<td
colspan="2"
align="center">
<asp:Button ID="btnSum" runat="server" Text="Sum" />
<asp:Button ID="btnSub" runat="server" Text="Subtract" />
<asp:Button ID="btnMul" runat="server" Text="Multiply"/>
<asp:Button ID="btnDiv" runat="server" Text="Divide"/>
</td>
<td>
</td>
</tr>
</table>
</fieldset>
In the Code behind file write
the code as:
Protected Sub btnSum_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSum.Click,
btnSub.Click, btnMul.Click, btnDiv.Click
If sender Is btnSum Then
txtResult.Text = Convert.ToInt32(txtFirstNum.Text) + Convert.ToInt32(txtSecondNum.Text)
ElseIf sender Is btnSub Then
txtResult.Text = Convert.ToInt32(txtFirstNum.Text) - Convert.ToInt32(txtSecondNum.Text)
ElseIf sender Is btnMul Then
txtResult.Text = Convert.ToInt32(txtFirstNum.Text) * Convert.ToInt32(txtSecondNum.Text)
Else
txtResult.Text = Convert.ToInt32(txtFirstNum.Text) / Convert.ToInt32(txtSecondNum.Text)
End If
End Sub
The same event handler is handling the events of multiple buttons.
The same event handler is handling the events of multiple buttons.
0 comments:
Post a Comment