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 class BasePage : System.Web.UI.Page
{
public BasePage()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CancelFormControlEnterKey(this.Page.Form.Controls);
}
/// <summary>
/// 在这里我们给Form中的服务器控件添加客户端onkeydown脚步事件,防止服务器控件按下enter键直接回发
/// </summary>
/// <param name="controls"></param>
public static void CancelFormControlEnterKey(ControlCollection controls)
{
foreach (Control item in controls)
{
//服务器TextBox
if (item.GetType() == typeof(System.Web.UI.WebControls.TextBox))
{
WebControl webControl = item as WebControl;
webControl.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}} ");
}
//html控件
else if (item.GetType() == typeof(System.Web.UI.HtmlControls.HtmlInputText))
{
HtmlInputControl htmlControl = item as HtmlInputControl;
htmlControl.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}} ");
}
//用户控件
else if (item is System.Web.UI.UserControl)
{
CancelFormControlEnterKey(item.Controls); //递归调用
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Test" %>
<%@ Register src="TestUserControl.ascx" tagname="TestUserControl" tagprefix="uc1" %>
<!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" >
<input type=text id="txtTest" runat="server" /> <input id="txtTest1" type="text" name="txtTest1" onkeydown="if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}}" />
<asp:textbox ID="Textbox1" runat="server"></asp:textbox>
<uc1:TestUserControl ID="TestUserControl1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("123");
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="MyWeb.TestUserControl" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<input id="Text1" type="text" runat="server"/>
<br />
<input id="txtInput" type="text" name="txtInput" onkeydown="if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}}" />
<script type="text/javascript">
function forbidInputKeyDown(ev) {
if (typeof (ev) != "undefined") {
if (ev.keyCode || ev.which) {
if (ev.keyCode == 13 || ev.which == 13) { return false; }
}
}
}
</script>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有