日期:2014-05-19  浏览次数:20427 次

页面有几十个按钮,有没有办法直接获取是哪个按钮按下的?
如题   ,难道非得在每个按钮的click事件里面写   ?谢谢各位

------解决方案--------------------
每一个按钮事件绑定同一方法上
------解决方案--------------------
<%@ Page Language= "C# " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">
protected override void OnLoad(EventArgs e)
{
//列举form1所有的按钮
//如果是多层嵌套的,可能还需要递归
foreach (Control ctrl in form1.Controls) {
if (Request.Form[ctrl.UniqueID] != null) {
Response.Write(ctrl.ID + "Click ");
break;
}
}
base.OnLoad(e);
}

</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:Button ID= "Button1 " runat= "server " Text= "Button " />
<asp:Button ID= "Button2 " runat= "server " Text= "Button " />
<asp:Button ID= "Button3 " runat= "server " Text= "Button " />
<asp:Button ID= "Button4 " runat= "server " Text= "Button " /> </div>
</form>
</body>
</html>

------解决方案--------------------
在客户端好象是
event.srcElement
在服务器端的事件处理方法一般都有sender参数,它就代表被点击的按钮
------解决方案--------------------
给相应的控件上加对应的事件
TextBox1.Attributes.Add( "onkeydown ", "if(event.keyCode==13){document.all.Button1.focus();document.all.Button1.click();} ");
这样就是当TEXTBOX1中回车,触发Button1的click事件