日期:2014-05-18  浏览次数:20331 次

ASP.Net 实现按回车触发事件
就是我的页面上有一个textBox,我要实现在其中输入一些数字后按回车触发执行操作,关键是如何实现按回车触发,求大牛指点~~~~~

------解决方案--------------------
function keyDown(evt){
if(isEvent(evt).which==13){//回车
 ajax//方法你要执行的代码
}
}
------解决方案--------------------

HTML code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_20120301_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>

    <script src="../jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
      //回车事件
    $(window).keydown(function(event){
      if(event.keyCode == 13) {
           alert("TextBox1的值为:" + $("[id$=TextBox1]").val());                    
      }
    });       
    </script>
    
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>

------解决方案--------------------
<asp:TextBox onkeydown= "if(event.keyCode==13) alert( 'xx ') "
------解决方案--------------------
$(document).keydown(function(e){
 if(e.keyCode==13)
{
document.getElementById("<%=btnclick.ClientID%>").click();
}

})
------解决方案--------------------
C# code

this.TextBox1.Attributes.Add("onkeydown", "if(event.keyCode==13) {document.all." + this.button1.ClientID + ".focus();document.all." + this.button1.ClientID + ".click();}");//当按下回车键时,让指定的按钮获取指定的文本框的事件

------解决方案--------------------
可以运用ajax,脚本调用后台方法,数据库查询返回结果字符串,在前台把字符串在页面显示,如:
前台脚本:
 $(window).keydown(function(event){
if(event.keyCode == 13) {
//调用后台方法
WebForm1.m_search_Click(price1, price2, type, function (response) {
if (response.value != undefined && response.value != "") {
$('#aaa').html(response.value);
}
});
}
});

WebForm1:后台文件名字
m_search_Click:方法名
后台:protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(WebTest.WebForm1)); //WebTest为命名空间
}
[Ajax.AjaxMethod]
public string m_search_Click(decimal price1,decimal price2,string type)
{
StringBuilder sb = new StringBuilder();
//调用方法,实现查询
sb.Append("<option>Choose a Status...</option>");

return sb.ToString();
}
webconfig:
<handlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</handlers>
------解决方案--------------------
$("#<%=TextBox1.ClientID%>").keydown(function(e){
if(e.keyCode==13)
{
document.getElementById("<%=btnclick.ClientID%>").click();
}

})