asp.net自定义按钮OnClientClick不起作用
本文关键字:OnClientClick 不起作用 按钮 自定义 net asp | 更新日期: 2023-09-27 18:05:20
我有一个自定义按钮的问题,我创建的目的是在标签之间使用文本和html。除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:button一起正常工作。我错过什么了吗?
在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;
}