Click here to Skip to main content
15,867,849 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hiii all
i have 3 buttons in the onclick of each one it will go to execute aspecific function.
that function will be the same except one line that will differ in disply the result.
can i ask in that function if the click came from which button?
any help, please?
thanks
Posted
Comments
shakil0304003 26-Jan-11 5:48am    
You can get it by searching google. Use google 1st.

You can pass the button id to the function.

C#
void MyFunc(string buttonID)
{
    //insert function code here
}

private void btnSubmit_Click(object sender, EventArgs e)
{
    Button b = (Button)sender;
    MyFunc(b.Name);
}
 
Share this answer
 
Comments
ahsan sarfraz 26-Jan-11 5:49am    
thats exactly what i was writing. but you typed quickly b4 myy post :) . good one
Ryan Zahra 26-Jan-11 5:51am    
Quite an easy question to reply to :)
Your answer is good too..everyone has his/her approaches.
ahsan sarfraz 26-Jan-11 5:53am    
yes it was ez one. :)
moon2011 26-Jan-11 6:21am    
Error 6 'System.Web.UI.WebControls.Button' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'System.Web.UI.WebControls.Button' could be found (are you missing a using directive or an assembly reference?)
that is the exception appears when i tried that
Ryan Zahra 26-Jan-11 6:28am    
Can I see your code please?
You can use CommandName property of Button to specify from which button the event has been raised..
use different command names for each button.
Hope this will work..
 
Share this answer
 
hey,
you can add an argument in the function. and send the name of the button as argument while calling the function.
e.g.
function Showdata(string buttonName)
{
if(buttonName=="btn1")
{
//do stuff
}
if(buttonName=="btn2")
{
//do stuff
}
}

if(buttonName=="btn3")
{
//do stuff
}


Best of luck
regards
Ahsan Sarfraz

Dont forget to mark as answered if info was useful.Thanks
 
Share this answer
 
Comments
Sajid Ahmed Shahsroha 18-Mar-11 0:27am    
In your solution,if you want to add up more button(s) for the same method(button_Click1),you have to do more coding..its not a right way,check out solution 7..regards Sajid
you can cast the sender to a button and check it's ID:

Button bt = sender as Button;

if(bt.ID == "<your id="" here="">")
{
 //your code here
}

</your>
 
Share this answer
 
The button that you pressed is the sender of the click-event. So you could simply check if the sender equals the button you want. A simple snippet follows:
C#
private void button_Click(object sender, EventArgs e)
{
    if (sender.Equals(button1))
    {
        //TODO
    }
    else if (sender.Equals(button2))
    {
        //TODO
    }
    else
    {
        //TODO
    }
}
 
Share this answer
 
Comments
Sajid Ahmed Shahsroha 18-Mar-11 0:26am    
In your solution,if you want to add up more button(s) for the same method(button_Click1),you have to do more coding..its not a right way,check out solution 7..regards Sajid
[no name] 20-Mar-11 8:42am    
Solution 7, yours, is not correct either
Sure you can. Just modify it's parameters and a Button or a string to use it as in:

private string MyFunction(yourParamsHere, string buttonName){
....
switch(buttonName)....
}


and on the click events that call the function:

MyFunction(yourParamsHere, ((Button)sender).ID);
 
Share this answer
 
Markup:

<asp:Button ID="btnOne" runat="server" Text="One" OnClick="btnOne_Click"/>
<asp:Button ID="btnTwo" runat="server" Text="Two" OnClick="btnOne_Click"/>
<asp:Button ID="btnThree" runat="server" Text="Three" OnClick="btnOne_Click"/>

Code:
protected void btnOne_Click(object sender,EventArgs e)
{
Button btn=(Button)sender;
Response.Write(btn.Text);
}
 
Share this answer
 
v2
Comments
[no name] 20-Mar-11 8:44am    
You have not answered the OPs question. How will this help to distinguish which button was clicked? If you think using the button text is correct, then think again.

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