Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net客户端上的自定义按钮单击“不工作”_C#_Asp.net - Fatal编程技术网

C# asp.net客户端上的自定义按钮单击“不工作”

C# asp.net客户端上的自定义按钮单击“不工作”,c#,asp.net,C#,Asp.net,为了在标记之间使用文本和html,我创建了一个自定义按钮,但它有一个问题。除OnClientClick事件外,所有功能都正常工作 下面是Button类在其自己的项目中的位置 [ParseChildren(false)] [PersistChildren(true)] public class BButton : Button { protected override string TagName { get { return "button"; } }

为了在标记之间使用文本和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);
    }
}
[ParseChildren(false)]
[儿童(对)]
公共类B按钮:按钮
{
受保护重写字符串标记名
{
获取{返回“按钮”;}
}
受保护的覆盖HtmlTextWriterTag标记键
{
获取{return htmltextwittertag.Button;}
}
公共新字符串文本
{
获取{return ViewState[“NewText”]作为字符串;}
设置{ViewState[“NewText”]=HttpUtility.HtmlDecode(值);}
}
受保护的覆盖无效OnPreRender(System.EventArgs e)
{
基于预渲染(e);
LiteralControl lc=新的LiteralControl(this.Text);
控制。添加(lc);
base.Text=UniqueID;
}
[可浏览(正确)]
[EditorBrowsable(EditorBrowsableState.Always)]
[显示名称(“字形图标”)]
公共字符串图示符图标
{
获取{将this.ViewState[“GlyphIcon”]作为字符串返回;}
设置{this.ViewState[“GlyphIcon”]=HttpUtility.HtmlDecode(值);}
}
[可浏览(正确)]
[EditorBrowsable(EditorBrowsableState.Always)]
[显示名称(“FontAwsome”)]
公共字符串
{
获取{将this.ViewState[“FontAwsome”]作为字符串返回;}
设置{this.ViewState[“FontAwsome”]=HttpUtility.HtmlDecode(值);}
}
受保护的覆盖无效呈现内容(HtmlTextWriter)
{
如果(!string.IsNullOrEmpty(this.GlyphIcon))
{
writer.AddAttribute(HtmlTextWriterAttribute.Class,“glyphicon”+this.glyphicon);
writer.AddStyleAttribute(HtmlTextWriterStyle.MarginRight,“5px”);
writer.RenderBeginTag(htmltextwittertag.Span);
writer.renderndtag();//
}
如果(!string.IsNullOrEmpty(this.FontAwsome))
{
writer.AddAttribute(HtmlTextWriterAttribute.Class,“fa fa”+this.FontAwsome);
writer.AddStyleAttribute(HtmlTextWriterStyle.MarginRight,“5px”);
writer.RenderBeginTag(htmltextwittertag.Span);
writer.renderndtag();//
}
伦德尔德伦(作家);
}
}
这是有效的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: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;
}

您可以尝试使用JavaScript吗?我发现它确实适用于CausesValidation=“False”的取消按钮,但在需要验证时不起作用。我现在正在编辑我的帖子以反映这一变化。