我已经尝试使用波纹管代码创建自定义图像按钮,但是当我点击图像时仍然无法点击事件。请任何人帮助我吗?
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
}
}
点击图片时我需要点击按钮点击事件
答案 0 :(得分: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);
}