object sender question

  • Thread starter Thread starter Steven Blair
  • Start date Start date
S

Steven Blair

Hi,



dgTransaction_SelectedIndexChanged(object sender, System.EventArgs e)

That sender is actually the object that raised the event.

So, in this case you could do:

((DataGrid)sender).Items.Controls[0].ClientID; //Do something
meaningful here!

What is the difference (if any) between using the sender object cast to
the correct object type over just using the variable name directly:

dgTransaction.Items.Controls[0].ClientID;

Any help on this would be appreciated.

Regards,

Steven
 
Hi,

IMO, it is better to use control name directly (instead of casting the
sender)
The visible difference is the cast operation.

Also, If you dont cast, you will get compile time error

HTH
Kalpesh
 
Steven Blair said:
Hi,
dgTransaction_SelectedIndexChanged(object sender, System.EventArgs e)

That sender is actually the object that raised the event.

So, in this case you could do:

((DataGrid)sender).Items.Controls[0].ClientID; //Do something
meaningful here!

What is the difference (if any) between using the sender object cast to
the correct object type over just using the variable name directly:

<snip>
You can use one Eventhandler for several objects.
Than you need the object parameter, to know wich object fired the event.

Christof
 
How would I go about doing that?

Are you saying I could have 2 DataGrids, but one OnClick method, then
using the sender, I could determine which button fired the event:

this.dgTran1.SelectIndexChanged += new
System.EventHandler(this.My_SelectedIndexChanged);

this.dgTran2.SelectIndexChanged += new
System.EventHandler(this.My_SelectedIndexChanged);

And my event code:

private void My_SelectedIndexChanged(object sender, System.EventArgs e)
{
//Somehow work out which DataGrid fired the event from?
}
 
"Steven Blair" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| Are you saying I could have 2 DataGrids, but one OnClick method, then
| using the sender, I could determine which button fired the event:

Well, it always works in Delphi :-)

However, unless you have a lot of code in common, then I would recommend
that you use two separate handlers. even if there is code in common it is
better practice to have two handlers and call a common routine passing the
sender to that.

Joanna
 
Steven Blair said:
How would I go about doing that?

By casting the sender to a DataGrid exactly as you did in your original
question.

SP
Are you saying I could have 2 DataGrids, but one OnClick method, then
using the sender, I could determine which button fired the event:

this.dgTran1.SelectIndexChanged += new
System.EventHandler(this.My_SelectedIndexChanged);

this.dgTran2.SelectIndexChanged += new
System.EventHandler(this.My_SelectedIndexChanged);

And my event code:

private void My_SelectedIndexChanged(object sender, System.EventArgs e)
{
//Somehow work out which DataGrid fired the event from?

the sender obviously!!!
 
Steven said:
How would I go about doing that?

That what the sender argument is for so you can determine which object
raised the event.

You could do something like this (Watch for typos):

if (sender is DataGrid1)
//Do something here
 
"Chris Dunaway" <[email protected]> a écrit dans le message de (e-mail address removed)...

| You could do something like this (Watch for typos):
|
| if (sender is DataGrid1)
| //Do something here

The "is" syntax would compare sender's type to the class DataGrid1, not to a
component held in a field called DataGrid1.

You should use :

if (sender == DataGrid1)
...

Joanna
 
Back
Top