Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 25-Jan-11 23:38pm
moon20111.1K
Comments
shakil0304003 at 26-Jan-11 5:48am
   
You can get it by searching google. Use google 1st.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can pass the button id to the function.
 
void MyFunc(string buttonID)
{
    //insert function code here
}
 
private void btnSubmit_Click(object sender, EventArgs e)
{
    Button b = (Button)sender;
    MyFunc(b.Name);
}
  Permalink  
Comments
ahsan sarfraz at 26-Jan-11 5:49am
   
thats exactly what i was writing. but you typed quickly b4 myy post :) . good one
Ryan Zahra at 26-Jan-11 5:51am
   
Quite an easy question to reply to :)
Your answer is good too..everyone has his/her approaches.
ahsan sarfraz at 26-Jan-11 5:53am
   
yes it was ez one. :)
monamoona at 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 at 26-Jan-11 6:28am
   
Can I see your code please?
monamoona at 26-Jan-11 6:30am
   
To excuted function is in another button click, so the sender it saw is the button that executing function in.
what can i do?
Ryan Zahra at 26-Jan-11 6:34am
   
Each button click event must call a function where the processing should be done. Your code should look something like this:
 
void MyFunc(string buttonID)
{
//insert function code here
}
 
private void btnOne_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
MyFunc(b.Name);
}
 
private void btnTwo_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
MyFunc(b.Name);
}
 
private void btnThree_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
MyFunc(b.Name);
}
monamoona at 26-Jan-11 6:32am
   
protected void xls_Click1(object sender, EventArgs e)
{
ImageButton b = (ImageButton)sender;
MyFun(b.ID);
}
 

public void MyFun(string buttonID)
{
if (buttonID == "btn_AllSubGroup")
{
ImageButton btnObj = new ImageButton();
string FileExt = btnObj.ID.ToString();
string ReportName = "CrystalReport2.rpt";
string FileName = "D:\\informations2";
ExportToFile(ReportName, FileName, FileExt);

}
 
else if (buttonID == "btn_SaveAll")
{
ImageButton btnObj = new ImageButton();
string FileExt = btnObj.ID.ToString();
string ReportName = "CrystalReport.rpt";
string FileName = "D:\\informations";
ExportToFile(ReportName, FileName, FileExt);

}

}
 
that is my code
Ryan Zahra at 26-Jan-11 6:38am
   
First of all, are you using a button or an image button?
Also, what are you trying to do here? "string FileExt = btnObj.ID.ToString();"
monamoona at 26-Jan-11 7:12am
   
the function that i told you about before is afunction to export data to xls file using crystal report and (FileExt) indicates that the file is .pdf or .xls file
Ryan Zahra at 26-Jan-11 7:49am
   
In the following code, you are creating a new instance of ImageButton. The ID of the button definitely doesn't hold your file extension.
"ImageButton btnObj = new ImageButton();
string FileExt = btnObj.ID.ToString();"
 
Can you please specify where you are storing the file extention?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
Sajid Ahmed Shahsroha at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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);
}
  Permalink  
v2
Comments
Mark Nischalke at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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:
private void button_Click(object sender, EventArgs e)
{
    if (sender.Equals(button1))
    {
        //TODO
    }
    else if (sender.Equals(button2))
    {
        //TODO
    }
    else
    {
        //TODO
    }
}
  Permalink  
Comments
Sajid Ahmed Shahsroha at 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
Mark Nischalke at 20-Mar-11 8:42am
   
Solution 7, yours, is not correct either
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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);
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 15 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100