ASP.NET自定义按钮控制 - 如何替代的OnClientClick但是preserve现有的行为? [英] ASP.NET Custom Button Control - How to Override OnClientClick But Preserve Existing Behaviour?

查看:189
本文介绍了ASP.NET自定义按钮控制 - 如何替代的OnClientClick但是preserve现有的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个ASP.NET 4自定义控件称为其目的是覆盖客户端点击(的OnClientClick)的默认行为SafeClickButton

So i have a ASP.NET 4 Custom Control called "SafeClickButton" which is designed to override the default behaviour of the client-side click (OnClientClick).

基本上我试图禁用点击按钮,然后做任何现有的功能(验证,回传等)。

Essentially i'm trying to disable the button on click, then do any existing functionality (validation, postback, etc).

它看起来是正确显示HTML(的onclick =this.disabled = TRUE; __ doPostback ...),并且正确禁用,但问题是与页面验证如果页面上所有验证失败。 ,它的回发,然后显示(无需回发,它应该在客户端完成)的验证错误。

It looks to be correctly rendering the HTML (onclick="this.disabled=true;__doPostback...), and it is disabling correctly, but the problem is with the page validation. If any validation on the page has failed, its posting back and THEN showing the validation errors (where it should be done on client side without requiring a postback).

这里的code自定义控件。

Here's the code for the custom control.

public class SafeClickButton : Button
    {
        public override string OnClientClick
        {
            get
            {
                return string.Format("this.disabled=true;{0}", Page.ClientScript.GetPostBackEventReference(this, string.Empty));
            }
            set
            {
                base.OnClientClick = value;
            }
        }

        protected override PostBackOptions GetPostBackOptions()
        {
            PostBackOptions options = new PostBackOptions(this, string.Empty) {ClientSubmit = true};
            if (Page != null)
            {
                if (CausesValidation && (Page.GetValidators(ValidationGroup).Count > 0))
                {
                    options.PerformValidation = true;
                    options.ValidationGroup = ValidationGroup;
                }
                if (!string.IsNullOrEmpty(PostBackUrl))
                {
                    options.ActionUrl = HttpUtility.UrlPathEncode(ResolveClientUrl(PostBackUrl));
                }
            }
            return options;  
        }
    }

我究竟做错了什么?

What am i doing wrong?

修改

好吧,我发现这个问题的一部分:

Okay so i found part of the problem:

return string.Format("this.disabled=true;{0}", Page.ClientScript.GetPostBackEventReference(this, string.Empty));

将不适用的postbackoptions的dervied行为。

Will not apply the dervied behaviour of the postbackoptions.

所以我改成了这样:

return string.Format("this.disabled=true;{0}", Page.ClientScript.GetPostBackEventReference(GetPostBackOptions()));

现在的验证得到正确的客户端解雇,但没有重新启用按钮,FML =)

Now the validation is getting fired properly on the client side, but the button isn't re-enabled, FML =)

我想我需要现在连扎痛,并说:如果验证失败,请重新启用按钮。

I think i need to be even smarted now, and say "If validation fails, re-enable button".

任何想法?

推荐答案

您应该能够只是添加 Page_ClientValidate 方法内联。我从来没有试过这种因此它可能无法正常工作:

You should be able to just add the Page_ClientValidate method inline. I have never tried this so it might not work:

return string.Format("if (Page_ClientValidate()) { this.disabled=true; {0} } else return false;",
   Page.ClientScript.GetPostBackEventReference(this, string.Empty));

您可能要惹它,或添加一些检查,以支持GroupValidation但我认为这将让你在正确的道路上。

You might have to mess with it or add some checks to support GroupValidation but I think this will get you on the right path.

编辑:我已经更新了答案,感动您禁用到,如果这样当 Page_ClientValidate 失败,它只是被禁用

I have updated the answer and moved you disable into the if so it only gets disabled when Page_ClientValidate fails.

看看这个链接,因为它是做你正在寻找我觉得什么,说明了什么我跟这意味着 Page_ClientValidate

Check out this link as it is doing what you are looking for I think and illustrates what I was meaning with the Page_ClientValidate:

http://msmvps.com/blogs/anguslogan/存档/ 2004/12/22 / 27223.aspx

这篇关于ASP.NET自定义按钮控制 - 如何替代的OnClientClick但是preserve现有的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆