ASP.NET中多按钮回车提交表单

方案一,利用脚本实现:
//调用jq
$(function() {
  //搜索按钮
  $("#txtSearch").keypress(function(e) {
    var key = window.event ? e.keyCode : e.which;
    if (key.toString() == "13") {
    $("#btnSearch")[0].click(); return false;
  }
});

//登录按钮LoginPwd
$("#loginPwd").keypress(function(e) {
  var key = window.event ? e.keyCode : e.which;
  if (key.toString() == "13") {
  $("#btnlogin")[0].click(); return false;
  }
});
//密码忘记
$("#txtRecPwd").keypress(function(e) {
  var key = window.event ? e.keyCode : e.which;
  if (key.toString() == "13") {
  $("#btnRecPwd")[0].click(); return false;
  }
});
});

方案二,用ASP.NET里自带的defaultButton属性:

 

ASP.NET 2.0中为此提供了很好的解决方法。只需要将"defaultbutton"属性指定到想要引发事件的按钮控件的 ID上就可以了。

在表单级别和面板级别(<asp:panel> 标记)均可以指定"defaultbutton"。当表单和面板中同时指定了defaultbutton,则如果在面板中触发了"Enter key",则执行面板中的

下面的实例代码中有一个表单和4个面板,报单和面板中都有按钮。请各位注意:在文本框中回车后会触发哪些按钮的事件

<formid="form1" runat="server" defaultbutton="btn1">
  <div>
    <asp:TextBoxID="txt" runat="server"></asp:TextBox>
    <asp:ButtonID="Button5" runat="server" Text="Cancel" OnClick="Button5_Click"/>
    <asp:ButtonID="btn1" runat="server" Text="Submit" OnClick="btn1_Click"/>
    <asp:PanelID="pnl1" runat="server" defaultbutton="Button1">
    <asp:TextBoxID="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBoxID="TextBox2" runat="server"></asp:TextBox>
    <asp:ButtonID="Button1" runat="server" Text="Button1" OnClick="Button1_Click"/>
    </asp:Panel>
    <asp:PanelID="Panel1" runat="server" defaultbutton="Button2">
    <asp:TextBoxID="TextBox3" runat="server"></asp:TextBox>
    <asp:TextBoxID="TextBox4" runat="server"></asp:TextBox>
    <asp:ButtonID="Button2" runat="server"Text="Button2"OnClick="Button2_Click"/>
    </asp:Panel>
    <asp:PanelID="Panel2" runat="server" defaultbutton="Button3">
    <asp:TextBoxID="TextBox5" runat="server"></asp:TextBox>
    <asp:TextBoxID="TextBox6" runat="server"></asp:TextBox>
    <asp:ButtonID="Button3" runat="server" Text="Button3" OnClick="Button3_Click"/>
    </asp:Panel>
    <asp:PanelID="Panel3" runat="server" defaultbutton="Button4">
    <asp:TextBoxID="TextBox7" runat="server"></asp:TextBox>
    <asp:TextBoxID="TextBox8" runat="server"></asp:TextBox>
    <asp:ButtonID="Button4" runat="server" Text="Button4" OnClick="Button4_Click"/>
    </asp:Panel>
  </div>
</form>



posted @ 2012-03-24 10:01  WebApi  阅读(422)  评论(0编辑  收藏  举报
CopyRight © 博客园 WebAPI