Click here to Skip to main content
15,903,849 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In my page three linkbuttons are there each button based report will be generated and i want to how to find out which linkbutton is clicked in normal button event



please share any idea to me
Posted
Comments
Thanks7872 16-Oct-13 5:14am    
Define different click events for all the link buttons. Write code for that link button in related click event. Whats the problem?

1 solution

There are two ways.

1. Define separate click events for all the link buttons as 'Rohan Leuva' suggested
2. Provide Command Argument with link button as mentioned below :

ASPX :
XML
<asp:LinkButton runat="server" ID="link1" OnClick="Lnk_Click" CommandArgument="1" Text="test Link 1"></asp:LinkButton>
        <asp:LinkButton runat="server" ID="link2" OnClick="Lnk_Click" CommandArgument="2" Text="test Link 2"></asp:LinkButton>
        <asp:LinkButton runat="server" ID="link3" OnClick="Lnk_Click" CommandArgument="3" Text="test Link 3"></asp:LinkButton>


Code Behind:
C#
protected void Lnk_Click(object sender, EventArgs e)
      {
          var button = (LinkButton)sender;
          var commandArgument = button.CommandArgument;

          if (commandArgument == "1")
          {
              // Link Button 1 Clicked
          }
          else if (commandArgument == "2")
          {
              // Link Button 2 Clicked
          }
          else if (commandArgument == "3")
          {
              // Link Button 3 Clicked
          }
      }
 
Share this answer
 
Comments
krish2013 16-Oct-13 5:47am    
hiii,
here var button = (LinkButton)sender; igot an error like "Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.LinkButton".i can't find any solution please provide any solution to me.
CodeBlack 16-Oct-13 5:50am    
Are you using Asp:LinkButton or Asp:Button ?

Use
var button = (LinkButton)sender;
if you are using linkbutton

Use
var button = (Button)sender;
if you are using button.
krish2013 16-Oct-13 5:57am    
Hii,
Actually button is varible.but i am using asp:linkbutton itried previous your's posted one but i got same error on that
krish2013 16-Oct-13 6:03am    
<asp:LinkButton ID="linkmenuitem1" runat="server" Text="Growth,Relative Performance and Lossratios"
ForeColor="White" OnClick="linkmenuitem1_Click" CommandArgument="1">   

<asp:LinkButton ID="LinkButton1" runat="server" Text="MarketShare and change Relative Performance"
ForeColor="Black" OnClick="LinkButton1_Click" CommandArgument="2">           

<asp:LinkButton ID="LinkButton2" runat="server" Text="Relative Performance loss Ratio"
ForeColor="Black" OnClick="LinkButton2_Click" CommandArgument="3">

<asp:Button ID="btngetdata" runat="server" Text="GetData" OnClick="btngetdata_Click" />

this is my .aspx code
i want to access the link button commmand argument in button click event
CodeBlack 16-Oct-13 6:10am    
set OnClick event of every LinkButton to OnClick="btngetdata_Click" and on click event of btngetdata_Click write below code :

var commandArgument = string.Empty;

if (sender.GetType().Name == "LinkButton")
{
var button = (LinkButton)sender;
commandArgument = button.CommandArgument;
}
else
{
var button = (Button)sender;
commandArgument = button.CommandArgument;
}

if (commandArgument == "1")
{
// Link Button 1 Clicked
}
else if (commandArgument == "2")
{
// Link Button 2 Clicked
}
else if (commandArgument == "3")
{
// Link Button 3 Clicked
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900