[转]设置textBox的响应按钮事件的js的两种方法
原文地址:http://www.cnblogs.com/dlwang2002/archive/2006/02/06/326165.html
1:设置整个页面都响应该事件
1:设置整个页面都响应该事件
public void SetEnterControl(object ctrl)
{
System.Web.UI.Control command = (System.Web.UI.Control)ctrl;
Page mPage = command.Page;
string mScript;
mScript = @"<script language=""javascript"">
function document.onkeydown()
{
var e = event.srcElement;
var k = event.keyCode;
if (k == 13 && e.type != ""textarea"")
{
document.getElementById('" + command.ClientID + @"').click();
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>";
if (!mPage.IsClientScriptBlockRegistered("SetEnterControl"))
mPage.RegisterClientScriptBlock("SetEnterControl", mScript);
}
{
System.Web.UI.Control command = (System.Web.UI.Control)ctrl;
Page mPage = command.Page;
string mScript;
mScript = @"<script language=""javascript"">
function document.onkeydown()
{
var e = event.srcElement;
var k = event.keyCode;
if (k == 13 && e.type != ""textarea"")
{
document.getElementById('" + command.ClientID + @"').click();
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>";
if (!mPage.IsClientScriptBlockRegistered("SetEnterControl"))
mPage.RegisterClientScriptBlock("SetEnterControl", mScript);
}
2:给每个TextBox都设置一个响应
public void SetEnterControl(TextBox source,Button btn)
{
//根据传递的参数来触发命令
string script=@"
<script language='javascript'>
function SetEnterControl(btnId)
{
var e=event.srcElement;
var k = event.keyCode;
if (k == 13 && e.type != 'textarea')
{
document.getElementById(btnId).click();
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>
";
//注册js
if (!this.Page.IsClientScriptBlockRegistered("SetEnterControl"))
this.Page.RegisterClientScriptBlock("SetEnterControl", script);
//给textbox添加属性,使用js函数
source.Attributes["onkeydown"]="SetEnterControl('"+btn.ClientID+"')";
}
{
//根据传递的参数来触发命令
string script=@"
<script language='javascript'>
function SetEnterControl(btnId)
{
var e=event.srcElement;
var k = event.keyCode;
if (k == 13 && e.type != 'textarea')
{
document.getElementById(btnId).click();
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>
";
//注册js
if (!this.Page.IsClientScriptBlockRegistered("SetEnterControl"))
this.Page.RegisterClientScriptBlock("SetEnterControl", script);
//给textbox添加属性,使用js函数
source.Attributes["onkeydown"]="SetEnterControl('"+btn.ClientID+"')";
}