我有一個自定義按鈕的問題,我創建的目的是在標籤之間使用文本和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"> </i> <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"> </i> <asp:Localize runat="server" meta:resourcekey="btnCreateText"/> </CPCC:StyleButton>
一切正常,除了OnClientClick="return confirm('Creating Account...');"
在需要驗證按鈕,這正常工作與ASP:按鈕。我在這裏錯過了什麼嗎?
可你嘗試使用JavaScript - http://stackoverflow.com/questions/16293456/onclientclick-does-not-works-on-asp-net-linkbutton – MethodMan