日期:2014-05-18  浏览次数:20464 次

请问如何取得鼠标点击的那个控件的ID..........谢谢
如题,我想取鼠标点击的那个控件的ID....知道的请告诉我.谢谢,最好有代码




------解决方案--------------------
客户端控件还是服务器端控?
js:
event.srcElement
服务器:
((Control)sender).ID
------解决方案--------------------
哇,发财了
------解决方案--------------------
// 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;
}