Click here to Skip to main content
14,388,768 members
Rate this:
Please Sign up or sign in to vote.
See more:
hiii all,
i have two buttons that onclick of them adropdown list appears with data, i want in the select index change of that drop, check for which button loads that droplist.
i tried that code:
if (sender ==" Buttonn1")
{
//do something
}
else if (sender=="Button2")
{
//do another thing
}


but it didn't have value for me because i checked for it in the select index change of the drop list.

so, any one can give me another idea, please?

thanks
Posted
Updated 1-Apr-11 19:22pm
v4
Rate this:
Please Sign up or sign in to vote.

Solution 1

The sender parameter is an object, you need to cast it appropriately.
In the case of a DropDownList.SelectedIndexChanged event, the sender parameter is the DropDownList that the used selected a value from. This is not in any way related to which button he clicked to load the dropdown in the first place!

If you need to know which button was clicked, then in the ButtonClick event, cast the sender to a Button, and insert it as the Tag property of the dropdown:
Button b = sender as Button;
if (b != null)
   {
   ...fill myDropDownList
   myDropDownList.Tag = b;
   }
Then check the tag in the SelectedIndexChanged Event:
Button b = myDropDownList.Tag as Button;
if (b == myButton1)
   {
   ...
   }



"can i understand what is Tag ??"

The Tag property is a an object variable which is available in all controls, for you to add your own information as required. For example, you might set the Tag of a textbox to the Database row ID that the textbox content came from: then you can update the correct row without having to look elsewhere.

Because it is an object variable, and everything is derived from object you can put anything you like in there: a Button, a string, a Form, even a class you defined. All you have to do is cast it appropriately and use it when you want.
   
v2
Comments
Sergey Alexandrovich Kryukov 30-Mar-11 14:50pm
   
Good, my 5.
Please see my Answer: extra value added :-)
--SA
Espen Harlinn 30-Mar-11 15:03pm
   
Good reply - my 5!
moon2011 31-Mar-11 3:31am
   
can i understand what is Tag ??
OriginalGriff 31-Mar-11 3:59am
   
Answer updated
Rate this:
Please Sign up or sign in to vote.

Solution 2

sender is not a string, it is the Control which sent the event. So in your case it is a Button.

void button_Clicked(object sender, EventArgs e)
{
    //converts sender into a Button object
    Button button = sender as button;
}


If you want to perform 2 different actions for the buttons, then it makes no sense to use 1 handler for both and add an if test inside the handler...

Instead use 2 different handlers:
void button1_Clicked(object sender, EventArgs e)
{
    //do the action you want for button1
}
void button2_Clicked(object sender, EventArgs e)
{
    //do the action you want for button2
}
   
Comments
Sergey Alexandrovich Kryukov 30-Mar-11 14:50pm
   
Correct, my 5, but as always I recommend anonymous form, this type it helps to simplify the type cast. Please see my Answer.
--SA
Espen Harlinn 30-Mar-11 15:04pm
   
Good advice, my 5
Rate this:
Please Sign up or sign in to vote.

Solution 3

In addition to correct Answers by Olivier and Griff:

Using just a regular cast without slower "as" is safe enough especially if you use anonymous delegates.

Consider:

MyButton.Click += delegate(object sender, System.EventArgs eventArgs) {
    Button myButton = (Button)sender; //you're sure, due to the line above
};


When using C# v.3 or later, you can use even more convenient lambda form:

MyButton.Click += (sender, eventArgs) => {
    Button myButton = (Button)sender; //you're sure, due to the line above
};

In this case, you don't even need to declare parameter types, thanks to type inference. In this case, the types are inferred from the event type.

I recommend always use this anonymous forms and never use the obsolete and hard-to-support syntax generated by the Designer.

—SA
   
v2
Comments
Espen Harlinn 30-Mar-11 15:04pm
   
It does have a certain elegance, my 5!
Sergey Alexandrovich Kryukov 30-Mar-11 15:14pm
   
Hi Espen, how it's going?
Thank you for the vote.
--SA
Espen Harlinn 30-Mar-11 15:27pm
   
Busy, my friend, quite busy ...
Olivier Levrey 31-Mar-11 4:17am
   
Anonymous methods and lambda expressions are convenient in many ways. However, I don't massively use them because of 2 reasons:

1- the function body doesn't appear in the class methods list. So when dealing with large files, I need more time to find the function body (if you use a named method, wich name sarts with the control name, it is very easy and quick to find its body.

2- I use them only if the body itself is very short (let's say not more than 5-6 lines).
An lambda expression is only elegant when it fits in 1-2 lines. More lines only leads to heavy coding (my opinion).

So when do I use them? Well I would say I use anonymous methods/lambda expressions basically only when dealing with LINQ queries and Invoke/BeginInvoke methods. All my event handlers remain named methods. But once again, it is just a matter of tastes.

Have my 5 for this answer since it is very correct and has the benefit to demonstrate the use of these handy and powerful features ;)
Sergey Alexandrovich Kryukov 31-Mar-11 13:01pm
   
Thank you for the vote, Olivier.
I'm not completely agree on items (1) and (2). In fact, I insist on using the anonymous specifically for events, even though not in 100% cases (but close).

I feel you're missing one point about my approach. In practice, the bodies of even handle still exist as separate (named) methods in order to be re-used. The role of the anonymous method is different. The example above is too simple. Its code should provide additional method of abstraction between "semantic" event arguments and more abstract event arguments of the event. This is a thunk. For example, you do typecast from sender to concrete control here, a cast from control tag, etc. and transform a call into "semantic", application-specific call the the named "handler" which is free from mandatory set of arguments (sender, eventArgs). If you tried my method in full I think you would understand its benefits.
1) this argument is not serious, frankly. If clicking on control (OK, property box) helps you find handlers faster, it's already too slow; :-) the code could be organized better, etc.
2) short body is applicable, long body -- see my explanation about the thunk.

Hope you will understand it.

Thank you very much.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 4

thanks alot for all your replies,
i have found another soultion amd will put it to be useful for all of us:
<br />
<br />
<pre lang="midl">Button b = (Button)sender;<br />
string id = b.ID;<br />
Session["id"] = id;<br />
<br />
<br />
</pre>


thanks
   

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




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