所以我有一個名爲「SafeClickButton」的ASP.NET 4自定義控件,它旨在覆蓋客戶端單擊(OnClientClick)的默認行爲。ASP.NET自定義按鈕控件 - 如何重寫OnClientClick但保留現有行爲?
本質上,我試圖禁用點擊按鈕,然後做任何現有的功能(驗證,回發等)。
它看起來是正確呈現HTML(onclick =「this.disabled = true; __ doPostback ...),並且它禁用正確,但問題是與頁面驗證。失敗了,它的回發,然後顯示(無需回發,它應該在客戶端完成)的驗證錯誤。
這裏的自定義控制代碼。
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;
}
}
我在做什麼錯?
編輯
好了,所以我找到了問題的一部分:
return string.Format("this.disabled=true;{0}", Page.ClientScript.GetPostBackEventReference(this, string.Empty));
將不適用的postbackoptions的dervied行爲。
所以我改成了這樣:
return string.Format("this.disabled=true;{0}", Page.ClientScript.GetPostBackEventReference(GetPostBackOptions()));
現在驗證正確地得到在客戶端解僱,但沒有重新啓用按鈕,FML =)
我想我現在需要更加聰明,並說「如果驗證失敗,請重新啓用按鈕」。
任何想法?
沒有ü讀我的更新?驗證正在工作 - 我只需要重新啓用驗證失敗。我有一個自定義GetPostBackOptions方法,所以我不需要使用GetPostBackEventReference。 – RPM1984 2010-08-20 02:06:29
@ RPM1984這就是爲什麼我說要在內部添加「Page_ClientValidate」選項的原因。如果失敗(返回false),那麼請禁用並返回false。我不知道如何從客戶端驗證的結果得到任何其他方式的結果,除非'Page.ClientScript.GetPostBackEventReference'返回一個錯誤,然後你可以觸發它。這些只是在黑暗中刺中:) – Kelsey 2010-08-20 02:23:34