问题描述
我的问题是,我有一个简单的网页形式,它包含两个文本框和一个button.there上page.so我想按钮的客户端禁用某些asp.net验证控件时,所有验证done.and后也按钮的禁用,我执行了一些服务器端$ C $这种c.All工作正常,但是,万一当我设置按钮的回发网址它就会失败。波纹管是编码的某些部分,会给你一些简单的想法。任何暗示将高度AP preciated .......
我想让复合控制此功能。
这里是按钮类
公共MyButton类:按钮
{ 保护覆盖无效在preRender(EventArgs的发送)
{
如果(this.CausesValidation)
{
如果(!String.IsNullOrEmpty(this.ValidationGroup))
{
this.Attributes.Add(的onclick,@的javascript: 如果(typeof运算(Page_ClientValidate)=='功能')
{
如果(Page_ClientValidate('+ this.ValidationGroup +')){+ this.ClientID +.disabled = TRUE;+ Page.GetPostBackEventReference(本)+ }其他{返回;}}其他{+ this.ClientID +.disabled = TRUE; + Page.GetPostBackEventReference(本)+});
} 其他
{
this.Attributes.Add(的onclick,@的javascript: 如果(typeof运算(Page_ClientValidate)=='功能')
{
如果(Page_ClientValidate()){+ this.ClientID +.disabled = TRUE;+ Page.GetPostBackEventReference(本)+
}其他{返回;}}其他{+ this.ClientID +.disabled = TRUE; + Page.GetPostBackEventReference(本)+});
}
}
其他
this.Attributes.Add(onclick事件,JavaScript的:+ this.ClientID +.disabled = TRUE;+ Page.GetPostBackEventReference(本)); base.On preRender(E);
}
这是做这个正确的和简单的方式:
创建(在Utlity命名空间的发言权)在应用程序中一个辅助方法:
公共共享子preventMultipleClicks(为ByRef按钮如System.Web.UI.WebControls.Button)
button.Attributes.Add(onclick事件,this.disabled = TRUE;&放大器; button.Page.ClientScript.GetPostBackEventReference(按钮的String.Empty)的ToString)
结束小组
现在,从每个网页,你可以简单地调用背后的code:
实用程序。preventMultipleClicks(按钮1)
其中button1的是要prevent多次点击的按钮。
这样做是只需设置上单击处理程序:this.disabled = TRUE
再追加按钮自己回来后的处理程序,所以我们得到:
的onclick =this.disabled =真; __ doPostBack('ID $ ID','');
这不会破坏网页的默认行为,并预期在所有浏览器上运行。
享受!
My Problem is ,I have a simple web form, which contains two textboxes and a button.there are some asp.net validator controls on page.so i want client side disabling of button when all validation is done.and also after disabling of button, i am executing some server side code.All of this is working fine but, in case when I set postback url of button it gets fail. bellow is some part of coding that will give you some brief idea. Any hint will be highly appreciated.......
I wanted to make this functionality in composite control.here is button class
public class MyButton : Button
{
protected override void OnPreRender(EventArgs e)
{
if (this.CausesValidation)
{
if (!String.IsNullOrEmpty(this.ValidationGroup))
{
this.Attributes.Add("onclick", @"javascript:
if (typeof(Page_ClientValidate) == 'function')
{
if (Page_ClientValidate('" + this.ValidationGroup + "')){" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) +
"}else{return;}} else{" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) + "}");
}
else
{
this.Attributes.Add("onclick", @"javascript:
if (typeof(Page_ClientValidate) == 'function')
{
if (Page_ClientValidate()){" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) +
"}else{return;}} else{" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) + "}");
}
}
else
this.Attributes.Add("onclick", "javascript:" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this));
base.OnPreRender(e);
}
This is the correct and simple way to do this:
Create a helper method in your application (say in a Utlity Namespace):
Public Shared Sub PreventMultipleClicks(ByRef button As System.Web.UI.WebControls.Button)
button.Attributes.Add("onclick", "this.disabled=true;" & button.Page.ClientScript.GetPostBackEventReference(button, String.Empty).ToString)
End Sub
Now from the code behind of each of your web pages you can simply call:
Utility.PreventMultipleClicks(button1)
where button1 is the the button you want to prevent multiple clicks.
What this does is simply sets the on click handler to: this.disabled=true
and then appends the buttons own post back handler, so we get:
onclick="this.disabled=true";__doPostBack('ID$ID','');"
This does not break the default behaviour of the page and works in all browsers as expected.
Enjoy!
这篇关于禁用多个点击的情况下,提交按钮。(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!