find details on event sender

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
 
P

Patrik Löwendahl

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top