我遇到了一个问题,我创建了一个自定义按钮,用于在标签之间使用文本和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:按钮。我在这里错过了什么吗?
答案 0 :(得分: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;
}