find details on event sender

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

When I raise an event in my ASP.NET (such as OnServerValidate), how do I
get details on the Sender in the called procedure e.g.

public void Check_Log_Details(object sender,
System.Web.UI.WebControls.ServerValidateEventArgs value)
{

}

Thanks,

Mike
 
Cast the parameter Sender to an appropiate class and get the information
that way.

This is a simple example using a a bit of reflection to get the type
information in string format. You could us the AS operator and if / els if
as well, maybe that is a better solution.

ex 1)

public void Check_Log_Details(object sender,

System.Web.UI.WebControls.ServerValidateEventArgs value)
{
switch(sender.GetType().ToString())
{
case "System.Web.UI.WebControls.Button":
Button btnSender = (Button)sender;
// Do whatever
break;
case "System.Web.UI.WebControls.TextBox":
TextBox txtSender = (TextBox)sender;
// Do whaterver
break;
default:
// No match
}

}

ex 2)

public void Check_Log_Details(object sender,

System.Web.UI.WebControls.ServerValidateEventArgs value)
{
if((sender as Button)!=null)
{
// It's a button,
Button btnSender = (Button)sender;
}
else if ((sender as TextBox)!=null)
{
// It's a textbox
TextBox txtSender = (TextBox)sender;
}
}

I haven't runned this code, might be that you wont get the control which
triggerd the validation in this specific scenario.
 
Back
Top