Sunday, 31 March 2013

create single event for multiple Button or LinkButton


For Asp.net Training  Click Here

Written By:-Isha Malhotra
Email:-malhotra.isha3388@gmail.com


How to create single event for multiple Button or LinkButton

In my last article I discussed how we create event for control which we generate at runtime. In this article I am discussing how to create a single event for multiple controls and how we access all control through single even.

Just remember the example for searching where a list is showing with alphabet and on which alphabet we click the list generated according to it.

I am taking the same example here.



Figure 1


If you see the figure then you came to know about the example which I am taking here. I want on which link I will click it will show me data according to it.
If you think that we have to create event for each single link then that’s totally wrong. If you remember that when we create any event it takes two arguments one is Object type and another is EventArgs. I will solve this example with the help of parameter type Object. This parameter holds the object of current control through which we fire this event.

For Example:-

Check the following code (you can also do this task by generating control at runtime as I did in previous example. But here I am not taking any runtime control)

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td>
                <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton_Click">A</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton_Click">B</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton_Click">C</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton4" runat="server" onclick="LinkButton_Click">D</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton5" runat="server" onclick="LinkButton_Click">E</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton6" runat="server" onclick="LinkButton_Click">F</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton7" runat="server" onclick="LinkButton_Click">G</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton8" runat="server" onclick="LinkButton_Click">H</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton9" runat="server" onclick="LinkButton_Click">I</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton10" runat="server" onclick="LinkButton_Click">J</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton11" runat="server" onclick="LinkButton_Click">k</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton12" runat="server" onclick="LinkButton_Click">L</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton13" runat="server" onclick="LinkButton_Click">M</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton14" runat="server" onclick="LinkButton_Click">N</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton15" runat="server" onclick="LinkButton_Click">O</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton16" runat="server" onclick="LinkButton_Click">P</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton17" runat="server" onclick="LinkButton_Click">Q</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton18" runat="server" onclick="LinkButton_Click">R</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton19" runat="server" onclick="LinkButton_Click">S</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton20" runat="server" onclick="LinkButton_Click">T</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton21" runat="server" onclick="LinkButton_Click">U</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton22" runat="server" onclick="LinkButton_Click">V</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton23" runat="server" onclick="LinkButton_Click">W</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton24" runat="server" onclick="LinkButton_Click">X</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton25" runat="server" onclick="LinkButton_Click">Y</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton26" runat="server" onclick="LinkButton_Click">Z</asp:LinkButton>
            </td>
        </tr>
    </table>
    </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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinkButton_Click(object sender, EventArgs e)
    {
        string strchar = ((LinkButton)(sender)).Text;
        Response.Write("You have Searched for " + strchar);
    }
}

As we know that object type sender holds the object of current control through which we fire event. So I convert this sender into linkbutton and after that I picked its text. Now you can use this text in any query or according to this text you can also perform any task. Apart from this now you can access any property of that linkbutton like id etc.

Output:-



Figure 2
Hope you enjoyed the article.

2 comments: