我创建了一个自定义WebControl,它实现了一个可以显示在网页顶部的简单消息框。与YouTube在其网页上显示消息的方式类似。我的问题是将Click事件从我添加到框中的任何按钮传递到ASPX页面。
以下是WebControl的代码。我认为我已经正确处理了点击事件,但是当我将此代码添加到我的页面时,即使是Page_Load,也不会调用click事件。
以下是APSX页面中的代码:
<rt:ConfirmationBox ID="ConfirmationBox1" runat="server" BoxType="Info" BoxButtons="OkayCancel" OnOkayClicked="ConfirmationBoxOkayClicked"
Text="Click OK to close." />
以下是代码隐藏页面中的代码:
protected void ConfirmationBoxOkayClicked(object sender, EventArgs e)
{
ConfirmationBox1.BoxButtons = ConfirmationBoxButtons.None;
ConfirmationBox1.BoxType = ConfirmationBoxType.Success;
ConfirmationBox1.Text = "You clicked OK!";
}
这是WebControl代码:
public enum ConfirmationBoxType
{
Hidden,
Success,
Warn,
Error,
Info
}
public enum ConfirmationBoxButtons
{
None,
Okay,
Cancel,
OkayCancel
}
[DefaultProperty("Text")]
[ToolboxData("<{0}:ConfirmationBox ID=\"ConfirmationBox1\" runat=server></{0}:ConfirmationBox>")]
public class ConfirmationBox : WebControl
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ConfirmationBox));
private Button _okayButton;
private Button _cancelButton;
public event EventHandler OkayClicked;
public event EventHandler CancelClicked;
public virtual void OnOkayClicked(object sender, EventArgs eventArgs)
{
if (OkayClicked != null)
OkayClicked(sender, eventArgs);
}
public virtual void OnCancelClicked(object sender, EventArgs eventArgs)
{
if (CancelClicked != null)
CancelClicked(sender, eventArgs);
}
protected override void OnPreRender(EventArgs e)
{
_okayButton = new Button {ID = "ConfirmBoxOkayButton", CssClass = "button", Text = "OK"};
_okayButton.Click += new EventHandler(OnOkayClicked);
_cancelButton = new Button {ID = "ConfirmBoxCancelButton", CssClass = "button", Text = "Cancel"};
_cancelButton.Click += new EventHandler(OnCancelClicked);
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
var s = (String)ViewState["Text"];
return (s ?? String.Empty);
}
set
{
ViewState["Text"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("Hidden")]
public ConfirmationBoxType BoxType
{
get
{
return (ConfirmationBoxType)ViewState["BoxType"];
}
set
{
ViewState["BoxType"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("None")]
public ConfirmationBoxButtons BoxButtons
{
get
{
return (ConfirmationBoxButtons)ViewState["BoxButtons"];
}
set
{
ViewState["BoxButtons"] = value;
}
}
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Id, "alerts");
base.AddAttributesToRender(writer);
}
protected override void RenderContents(HtmlTextWriter writer)
{
if (Site != null && Site.DesignMode)
{
writer.Write("[" + ID + "]");
}
else
{
if (BoxType == ConfirmationBoxType.Hidden)
return;
var theme = HttpContext.Current.Profile["UserTheme"].ToString();
writer.AddAttribute(HtmlTextWriterAttribute.Id, "confirmBox");
writer.AddAttribute(HtmlTextWriterAttribute.Class, "rt-alert " + RenderBoxType());
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.AddAttribute(HtmlTextWriterAttribute.Src, string.Format("{0}/{1}/pixel.gif", ResolveUrl("~/App_Themes"), (string.IsNullOrEmpty(theme)) ? "Default" : theme));
writer.AddAttribute(HtmlTextWriterAttribute.Class, "icon");
writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Alert icon");
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
writer.AddAttribute(HtmlTextWriterAttribute.Class, "rt-alert-content");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write(Text);
writer.RenderEndTag();
writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
writer.AddAttribute(HtmlTextWriterAttribute.Class, "close");
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "$(\"#alerts\").hide()");
writer.RenderBeginTag(HtmlTextWriterTag.Button);
writer.Write("close");
writer.RenderEndTag();
if (BoxButtons != ConfirmationBoxButtons.None)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "rt-alert-buttons");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
if (BoxButtons == ConfirmationBoxButtons.Okay || BoxButtons == ConfirmationBoxButtons.OkayCancel)
_okayButton.RenderControl(writer);
if (BoxButtons == ConfirmationBoxButtons.Cancel || BoxButtons == ConfirmationBoxButtons.OkayCancel)
_cancelButton.RenderControl(writer);
writer.RenderEndTag();
}
writer.RenderEndTag();
}
}
private string RenderBoxType()
{
switch (BoxType)
{
case ConfirmationBoxType.Success:
return "rt-alert-success";
case ConfirmationBoxType.Warn:
return "rt-alert-warn";
case ConfirmationBoxType.Error:
return "rt-alert-error";
case ConfirmationBoxType.Info:
return "rt-alert-info";
}
return string.Empty;
}
}
答案 0 :(得分:1)
为自定义事件处理程序创建一个单独的方法,如下所示:
protected virtual void OnOkayClicked(EventArgs e)
{
if (OkayClicked!= null)
OkayClicked(this, e);
}
更改按钮点击事件的名称并使其受到保护,如下所示:
protected void OkayButton_Click(object sender, EventArgs e)
{
//call your event handler method
this.OnOkayClicked(EventArgs.Empty);
}
测试一下,看看它是否有所作为。