2008-03-06, 11:51 AM | #1 |
注册日期: 2007-07-17
住址: 火星
帖子: 5,570
现金:9873金币
资产:21334金币
|
Aspnet服务器控件使用客户端脚本进行验证
ASP.NET虽然提供强大的验证控件,但这也给服务器加大了负担,我们可以利用server controns 的客户端脚本功能,把一些基本的验证放在客户端,以减轻服务器的负担! 下面是删除动作前执行询问操作者, 包含了显示用户确认弹出式对话框的函数。请注意确认对话框包含了“确认”和“取消”两个按钮控件,这与警告对话框不同,整个JavaScript代码是动态构建的。同样输入信息必须填写的,不然显示用户提示函数,并不执行服务器端动作!好了,下面是代码: gcba.aspx <%@ Page language="c#" Codebehind="gcba.aspx.cs" AutoEventWireup="false" Inherits="MessageManage.qualityinspect.gcba" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>jgys</title> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="gcba" method="post" runat="server"> <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 126px; POSITION: absolute; TOP: 99px" runat="server" Text=" 测 试"></asp:Button> <asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 128px; POSITION: absolute; TOP: 147px" runat="server"></asp:TextBox> <asp:TextBox id="TextBox2" style="Z-INDEX: 103; LEFT: 128px; POSITION: absolute; TOP: 202px" runat="server"></asp:TextBox> <asp:Button id="Button2" style="Z-INDEX: 104; LEFT: 265px; POSITION: absolute; TOP: 99px" runat="server" Text="数据完整性"></asp:Button> </form> </body> </HTML> gcba.aspx.cs using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace MessageManage.qualityinspect { /// /// jgys 的摘要说明。 /// public class gcba : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.TextBox TextBox2; protected System.Web.UI.WebControls.Button Button2; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 string bb="fsd"; System.Text.StringBuilder Msg = new System.Text.StringBuilder(); Msg.Append(" "); string clientConfrimDeleteJS = Msg.ToString(); if(!IsClientScriptBlockRegistered("confrimDelete")) { RegisterClientScriptBlock("confrimDelete",clientConfrimDeleteJS); } Msg.Remove(0,Msg.Length); Msg.Append(" "); string confrimTextboxClientJS = Msg.ToString(); if(!IsClientScriptBlockRegistered("confrimTextbox")) { RegisterClientScriptBlock("confrimTextbox",confrimTextboxClientJS);//注册客户端脚本 } Button1.Attributes.Add("onclick","return window.confrimDelete()");//添加控件的客户端脚本 Button2.Attributes.Add("onclick","return window.confrimTextbox()"); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Button2.Click += new System.EventHandler(this.Button2_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { Response.Write("HavedPost"); } private void Button2_Click(object sender, System.EventArgs e) { Response.Write("this textboxs value all fillde "); } } } |
|