荷梅月剑 编程之路

这个世界没有偶然,有的只是必然
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

页面中按回车执行指定个按钮(原创)

Posted on 2007-08-14 14:26  荷梅月剑  阅读(406)  评论(0)    收藏  举报


关闭页面中默认的回车
一直没有找到好的方法,只能用JS来关闭了

第一步,关闭默认回车事件
1.关闭默认回事事件 指定的多行文本框例外

<script type="text/javascript">

function document.onkeydown()

{

    if(event.keyCode == 13&&event.srcElement! = document.getElementById("<%=txtLeave.ClientID %>"))

    {

        event.returnValue = false;

    }

}

</script>


2.关闭默认回事事件 所有多行文本框例外

<script type="text/javascript">

function document.onkeydown()

{

    if(event.keyCode == 13&&event.srcElement.tagName.toUpperCase() != "TEXTAREA")

    {

        event.returnValue = false;

    }

}

</script>


第二步,给TextBox加事件
打个比方,一个页面有两个按钮,两个TextBox,当TextBox1获得焦点时按回车执行Button1,当TextBox2获得焦点时按回车执行Button2
后台加代码
TextBox1.Attributes["onkeydown"= " if(event.keyCode==13) document.getElementById('" + Button1.ClientID + "').click();";
TextBox2.Attributes[
"onkeydown"= " if(event.keyCode==13) document.getElementById('" + Button2.ClientID + "').click();";