// js var id = event.srcElement.id;// cs ... Button1_Click(object sender, ... { string id = (sender as Control).ID }
服务端 public static Control FindThePostBackControl(Control container) { // Note: this is an adapted eggheadcafe.com code. // Control control = null; string ctrlname = HttpContext.Current.Request.Params["__EVENTTARGET"]; if ( ctrlname != null && ctrlname != String.Empty ) { string[] tokens = ctrlname.Split( new char[1] { ':' } ); if ( tokens != null && tokens.GetLength( 0 ) > 0 ) { ctrlname = tokens[( tokens.GetLength( 0 ) - 1 )]; } control = container.FindControl( ctrlname ); } else { // If __EVENTTARGET is null, control is a button type and need to // iterate over the form collection to find it // string ctrlStr = String.Empty; Control c = null; foreach ( string ctl in HttpContext.Current.Request.Form ) { // Handle ImageButton controls if ( ctl.EndsWith( ".x" ) || ctl.EndsWith( ".y" ) ) { ctrlStr = ctl.Substring( 0 , ( ctl.Length - 2 ) ); c = container.FindControl( ctrlStr ); } else { c = container.FindControl( ctl ); } if ( c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton ) { control = c; break; } } } return control; }
js:
event.srcElement
服务器:
((Control)sender).ID
var id = event.srcElement.id;// cs
... Button1_Click(object sender, ...
{
string id = (sender as Control).ID
}
{
// Note: this is an adapted eggheadcafe.com code.
//
Control control = null; string ctrlname = HttpContext.Current.Request.Params["__EVENTTARGET"];
if ( ctrlname != null && ctrlname != String.Empty )
{
string[] tokens = ctrlname.Split( new char[1] { ':' } ); if ( tokens != null && tokens.GetLength( 0 ) > 0 )
{
ctrlname = tokens[( tokens.GetLength( 0 ) - 1 )];
} control = container.FindControl( ctrlname );
}
else
{
// If __EVENTTARGET is null, control is a button type and need to
// iterate over the form collection to find it
//
string ctrlStr = String.Empty;
Control c = null;
foreach ( string ctl in HttpContext.Current.Request.Form )
{ // Handle ImageButton controls
if ( ctl.EndsWith( ".x" ) || ctl.EndsWith( ".y" ) )
{
ctrlStr = ctl.Substring( 0 , ( ctl.Length - 2 ) );
c = container.FindControl( ctrlStr );
}
else
{
c = container.FindControl( ctl );
} if ( c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton )
{
control = c;
break;
}
}
} return control;
}
学习js:
event.srcElement
服务器:
((Control)sender).ID