asp.net – 在表单提交时禁用按钮
发布时间:2020-12-16 00:24:13 所属栏目:asp.Net 来源:网络整理
导读:我有一个按钮,我想要禁用的表单提交,以防止用户多次提交。 我已经尝试天真地禁用javascript onclick按钮,但是如果客户端验证失败的按钮仍然禁用。 当表单成功提交时,不仅仅在用户点击时,如何禁用按钮? 这是一个ASP.NET表单,所以我想很好地与asp.net a
我有一个按钮,我想要禁用的表单提交,以防止用户多次提交。
我已经尝试天真地禁用javascript onclick按钮,但是如果客户端验证失败的按钮仍然禁用。 当表单成功提交时,不仅仅在用户点击时,如何禁用按钮? 这是一个ASP.NET表单,所以我想很好地与asp.net ajax页面生命周期挂钩,如果可能的话。 解决方法
给这个旋转:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Threading; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { // Identify button as a "disabled-when-clicked" button... WebHelpers.DisableButtonOnClick( buttonTest,"showPleaseWait" ); } protected void buttonTest_Click( object sender,EventArgs e ) { // Emulate a server-side process to demo the disabled button during // postback. Thread.Sleep( 5000 ); } } using System; using System.Web; using System.Web.UI.WebControls; using System.Text; public class WebHelpers { // // Disable button with no secondary JavaScript function call. // public static void DisableButtonOnClick( Button ButtonControl ) { DisableButtonOnClick( ButtonControl,string.Empty ); } // // Disable button with a JavaScript function call. // public static void DisableButtonOnClick( Button ButtonControl,string ClientFunction ) { StringBuilder sb = new StringBuilder( 128 ); // If the page has ASP.NET validators on it,this code ensures the // page validates before continuing. sb.Append( "if ( typeof( Page_ClientValidate ) == 'function' ) { " ); sb.Append( "if ( ! Page_ClientValidate() ) { return false; } } " ); // Disable this button. sb.Append( "this.disabled = true;" ); // If a secondary JavaScript function has been provided,and if it can be found,// call it. Note the name of the JavaScript function to call should be passed without // parens. if ( ! String.IsNullOrEmpty( ClientFunction ) ) { sb.AppendFormat( "if ( typeof( {0} ) == 'function' ) {{ {0}() }};",ClientFunction ); } // GetPostBackEventReference() obtains a reference to a client-side script function // that causes the server to post back to the page (ie this causes the server-side part // of the "click" to be performed). sb.Append( ButtonControl.Page.ClientScript.GetPostBackEventReference( ButtonControl ) + ";" ); // Add the JavaScript created a code to be executed when the button is clicked. ButtonControl.Attributes.Add( "onclick",sb.ToString() ); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- entity-framework – 从Entity Framework 6升级到
- 勾选复选框时,禁用一些ASP.Net验证控件
- 谈谈分布式事务之三: System.Transactions事务详
- asp.net – 发布时母版页无法加载类型错误
- asp.net-mvc-3 – 与SQL Server建立连接时发生与
- asp.net-mvc – 集合的验证摘要
- asp.net – 具有一个无效的SelectedValue,因为它
- asp.net-mvc – 具有插件和多租户支持的ASP.NET
- asp.net – 如何在Azure应用服务中管理(非SSL)证
- 具有EntityDataSource的一个ASP.NET GridView中的
热点阅读