`
shirlly
  • 浏览: 1623455 次
  • 性别: Icon_minigender_2
  • 来自: 福州
社区版块
存档分类
最新评论

使用 Enter 键提交表单

    博客分类:
  • .NET
阅读更多
一、使用 JS 代码
我们在表单里面的 TextBox (asp.net控件),然后在里面添加 attributes 响应对 JS 函数处理。具体我也不知道怎么说,看看代码就了解了。
//Add the javascript so we know where we want the enter key press to go
if (!IsPostBack)
{
     txtboxFirstName.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
     txtboxLastName.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
     txtboxAddress1.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
     txtboxAddress2.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
     txtboxCity.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
     txtboxState.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
     txtboxZipcode.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)");
}

然后在default.aspx的页面里面写入下面的js代码:
<SCRIPT type=text/javascript>
    function doClick(buttonName,e)
    {
//the purpose of this function is to allow the enter key to 
//point to the correct button to click.
        var key;

         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox
    
        if (key == 13)
        {
            //Get the button the user wants to have clicked
            var btn = document.getElementById(buttonName);
            if (btn != null)
            { //If we find the button click it
                btn.click();
                event.keyCode = 0
            }
        }
   }</SCRIPT>

二、使用 Panel 来实现
panel 的实现方法实现起来很简单,不用写任何一行代码。把我们的 TextBox 放到 panel 控件里面,然后设置 panel 控件的 defaultbutton 的属性为我们要响应的那个button的id。

<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch2" Width="100%" >

只要把表单的部分,包括提交按钮包在pannel中即可

转自:http://www.cnblogs.com/jessezhao/archive/2007/02/21/653250.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics