2016-09-02 105 views
-1

我有一個自定義按鈕的問題,我創建的目的是在標籤之間使用文本和html。除了OnClientClick事件以外,一切正常。asp.net自定義按鈕OnClientClick不工作

這是自己項目中的Button類。

[ParseChildren(false)] 
[PersistChildren(true)] 
public class BButton : Button 
{ 
    protected override string TagName 
    { 
     get { return "button"; } 
    } 
    protected override HtmlTextWriterTag TagKey 
    { 
     get { return HtmlTextWriterTag.Button; } 
    } 
    public new string Text 
    { 
     get { return ViewState["NewText"] as string; } 
     set { ViewState["NewText"] = HttpUtility.HtmlDecode(value); } 
    } 
    protected override void OnPreRender(System.EventArgs e) 
    { 
     base.OnPreRender(e); 

     LiteralControl lc = new LiteralControl(this.Text); 
     Controls.Add(lc); 
     base.Text = UniqueID; 
    } 
    [Browsable(true)] 
    [EditorBrowsable(EditorBrowsableState.Always)] 
    [DisplayName("GlyphIcon")] 
    public string GlyphIcon 
    { 
     get { return this.ViewState["GlyphIcon"] as string; } 
     set { this.ViewState["GlyphIcon"] = HttpUtility.HtmlDecode(value); } 
    } 
    [Browsable(true)] 
    [EditorBrowsable(EditorBrowsableState.Always)] 
    [DisplayName("FontAwsome")] 
    public string FontAwsome 
    { 
     get { return this.ViewState["FontAwsome"] as string; } 
     set { this.ViewState["FontAwsome"] = HttpUtility.HtmlDecode(value); } 
    } 
    protected override void RenderContents(HtmlTextWriter writer) 
    { 
     if (!string.IsNullOrEmpty(this.GlyphIcon)) 
     { 
      writer.AddAttribute(HtmlTextWriterAttribute.Class, "glyphicon " + this.GlyphIcon); 
      writer.AddStyleAttribute(HtmlTextWriterStyle.MarginRight, "5px"); 
      writer.RenderBeginTag(HtmlTextWriterTag.Span); 
      writer.RenderEndTag(); // </span> 
     } 
     if (!string.IsNullOrEmpty(this.FontAwsome)) 
     { 
      writer.AddAttribute(HtmlTextWriterAttribute.Class, "fa fa " + this.FontAwsome); 
      writer.AddStyleAttribute(HtmlTextWriterStyle.MarginRight, "5px"); 
      writer.RenderBeginTag(HtmlTextWriterTag.Span); 
      writer.RenderEndTag(); // </span> 
     } 
     RenderChildren(writer); 
    } 
} 

而這是asp頁面調用的工作原理:

<CPCC:BButton ID="btnDelete" CssClass="btn btn-danger" runat="server" CausesValidation="False" OnClick="btnDelete_Click" OnClientClick="return confirm('Are you sure you want to Delete this item?');"><i class="fa fa-trash-o">&nbsp;</i>&nbsp;<asp:Localize runat="server" meta:resourcekey="btnDeleteText" /> 
</CPCC:StyleButton> 

此調用將無法工作:

<CPCC:BButton id="btnAdd" CssClass="btn btn-success" runat="server" OnClick="btnAdd_Click" ValidationGroup="Create" OnClientClick="ShowProgressDialog('Creating Account...');"> <i class="fa fa-plus">&nbsp;</i>&nbsp;<asp:Localize runat="server" meta:resourcekey="btnCreateText"/> </CPCC:StyleButton> 

一切正常,除了OnClientClick="return confirm('Creating Account...');"在需要驗證按鈕,這正常工作與ASP:按鈕。我在這裏錯過了什麼嗎?

+0

可你嘗試使用JavaScript - http://stackoverflow.com/questions/16293456/onclientclick-does-not-works-on-asp-net-linkbutton – MethodMan

回答

0

BButton中,您已經覆蓋了ASP:Button控件的RenderContents方法,所以基本方法(通常會呈現Javascript)會被跳過。

您需要將代碼添加到RenderContents方法中,該方法讀取OnClientClick屬性並輸出適當的標記。或者,如果你非常聰明,也許你可以通過撥打base.RenderContents()找到一種方法來完成這項工作。

或者,也許你只需要子控件(文字),以響應點擊,在這種情況下,你可以嘗試

protected override void OnPreRender(System.EventArgs e) 
{ 
    base.OnPreRender(e); 

    LiteralControl lc = new LiteralControl(this.Text); 
    lc.Attributes.Add("onclick",this.OnClientClick); 
    Controls.Add(lc); 
    base.Text = UniqueID; 
} 
+0

進一步跟蹤我發現它在取消按鈕上起作用,其中CausesValidation =「False」,但在需要驗證時不起作用。我正在編輯我的帖子以反映更改。 –