0

我尝试使用波纹管代码创建自定义图像按钮,但我仍然无法在单击图像时触发单击事件。请问有人可以帮我吗?

DefaultProperty("Text"),
ToolboxData("<{0}:CustomButton runat=\"server\"> </{0}:CustomButton>")
]
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]

public class CustomButton : Button
{
    private string imgURL;

    #region Constructor
    public CustomButton() { }
    #endregion

    #region Properties
    [Bindable(true)]
    [Category("Appearance")]
    [Description("ShowImage")]
    [DefaultValue(false)]
    public bool ShowImage { get; set; }


    [DefaultValue("")]
    [Browsable(true)]

    [UrlProperty]
    public string ImageUrl
    {
        get
        {
            return imgURL;
        }
        set
        {
            imgURL = value;
        }
    }
    #endregion

    #region Override
    protected override void Render(HtmlTextWriter writer)
    {


        writer.AddAttribute(HtmlTextWriterAttribute.Id, String.Format("tbl{0}", this.ID));
        writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
        writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
        writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "imgBtnWrapperStd");
        if (!this.Enabled)
            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);


        writer.AddAttribute(HtmlTextWriterAttribute.Class, "btnContainer");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        if (this.ShowImage)
        {

            writer.AddAttribute(HtmlTextWriterAttribute.Src, ResolveClientUrl(this.ImageUrl));
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absMiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
        }
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "imgBtnStd");
        base.Render(writer);
        writer.RenderEndTag();
        writer.RenderEndTag();



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

    }
    #endregion
}

}

单击图像时我还需要触发按钮单击事件

4

1 回答 1

0

如果您在后面的代码中动态创建按钮,则需要在 Page_Load 或 Page_Init 事件中初始化按钮。不包括在这些事件之一中将使 Click 事件不会触发。

protected void Page_Init(object sender, EventArgs e)
{
   CustomButton btn = new CustomButton();
   btn.Click += new EventHandler(yourButton_Click);

   Controls.Add(btn);
}
于 2013-04-30T16:37:11.787 回答