MagicAjax执行javascript,实现回车事件提交表单
最近在一个项目,使用到了magicajax,其中要实现一个功能是:一个textbox,一个button,当用户按回车enter时提交表单.本来是个很简单的问题,一个javascript代码就搞定了.但是由于是textbox,和button都是在AjaxPanel中,所以一般的javascript写法是会被magicajax拦截,就出现了一般的javascript代码不会执行还发生错误.下面,分享一下我个人的经验:
实现功能:在magicajax的AjaxPanel中,实现用户按回车键提交表单(本例中,用户按回车键盘,Label显示TextBox输入的值,当然也可以提交表单了)
解决方法:
.CS代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.txt.Attributes.Add("onkeydown", "if( event.keyCode==13 ){__doPostBack('sendbtn','');}");
}
protected void sendbtn_Click(object sender, EventArgs e)
{
this.lb.Text = this.txt.Text;
}
}
HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="MagicAjax" Namespace="MagicAjax.UI.Controls" TagPrefix="ajax" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<ajax:AjaxPanel ID="AjaxPanel1" runat="server">
<table cellpadding="0" cellspacing="0" border="0">
<tr><td>
<asp:TextBox runat='server' ID="txt" AutoPostBack="True" ></asp:TextBox><asp:Button ID="sendbtn" runat="server" Text="Send" OnClick="sendbtn_Click" /><asp:Label ID="lb" runat="server"></asp:Label>
</td></tr>
</table></ajax:AjaxPanel>
</form>
</body>
</html>