I created a custom web control to act like an image button. I would like to be able to set this as the target DefaultButtonof an ASP.NET Panel control parameter . I implemented the IButton interface and there are no errors when loading the page using the control. However, when I press the enter button in the text box inside this panel, the Click event of the control does not raise. When I replace my control with a standard ASP.NET button, everything works fine.
I have a test page with a panel containing a text box, an instance of my custom button and a standard asp.net button. The buttons are connected to an event handler that changes the text field to the caller ID.
When the DefaultButton panel is installed on the ASP.NET button, pressing the enter button in the next field works correctly - the page returns, and the text field is filled with the name of the standard button. When the DefaultButton panel is set to my button, pressing input in the text field causes the page to return, but the Click event does not fire. Pressing the button manually works correctly.
Does anyone know what I need to add to my user control in order to handle the event coming from the control panel? I look with Reflector in the Button source code, but cannot determine what this behavior allows.
I placed the control source below and the corresponding test page source.
Source of control:
public class NewButton : WebControl, IPostBackEventHandler, IButtonControl
{
public NewButton() : base(HtmlTextWriterTag.A) { }
public event EventHandler Click;
public event CommandEventHandler Command;
public string Text
{
get { return ViewState["Text"] as string; }
set { ViewState["Text"] = value; }
}
public string ImageUrl
{
get { return ViewState["ImageUrl"] as string; }
set { ViewState["ImageUrl"] = value; }
}
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
Click(this, e);
}
protected virtual void OnCommand(CommandEventArgs e)
{
if (Command != null)
Command(this, e);
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Class, "Button");
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "Click"));
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.WriteFullBeginTag("span");
writer.WriteFullBeginTag("span");
if (!String.IsNullOrEmpty(ImageUrl))
{
writer.WriteBeginTag("img");
writer.WriteAttribute("src", Page.ResolveClientUrl(ImageUrl));
writer.Write(HtmlTextWriter.SelfClosingTagEnd);
writer.Write(" ");
}
writer.WriteEncodedText(Text);
writer.WriteEndTag("span");
writer.WriteEndTag("span");
}
public void RaisePostBackEvent(string eventArgument)
{
if (this.CausesValidation)
{
Page.Validate(this.ValidationGroup);
}
OnClick(EventArgs.Empty);
OnCommand(new CommandEventArgs(this.CommandName, this.CommandArgument));
}
public bool CausesValidation
{
get { return ViewState["CausesValidation"] as bool? ?? true; }
set { ViewState["CausesValidation"] = value; }
}
public string CommandArgument
{
get { return ViewState["CommandArgument"] as string; }
set { ViewState["CommandArgument"] = value; }
}
public string CommandName
{
get { return ViewState["CommandName"] as string; }
set { ViewState["CommandName"] = value; }
}
public string PostBackUrl
{
get { return ViewState["PostBackUrl"] as string; }
set { ViewState["PostBackUrl"] = value; }
}
public string ValidationGroup
{
get { return ViewState["ValidationGroup"] as string; }
set { ViewState["ValidationGroup"] = value; }
}
}
Code to call:
<asp:Panel ID="Panel1" runat="server" CssClass="StandardForm" DefaultButton="NewButton1">
<asp:TextBox runat="server" ID="text1" Columns="40" />
<MyControls:NewButton ID="NewButton1" runat="server" Text="Test" OnClick="OnClick" />
<asp:Button runat="server" ID="OldButton1" OnClick="OnClick" Text="Test" />
</asp:Panel>
Code for:
protected void OnClick(object sender, EventArgs args)
{
text1.Text = "Click event received from " + ((WebControl) sender).ID;
}
The control is as follows:
<div id="ctl00_MainContent_Panel1" class="StandardForm" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'ctl00_MainContent_NewButton1')">
<input name="ctl00$MainContent$text1" type="text" size="40" id="ctl00_MainContent_text1" />
<a id="ctl00_MainContent_NewButton1" class="Button" onclick="__doPostBack('ctl00$MainContent$NewButton1','')"><span><span>Test</span></span></a>
<input type="button" name="ctl00$MainContent$OldButton1" value="Test" onclick="javascript:__doPostBack('ctl00$MainContent$OldButton1','')" id="ctl00_MainContent_OldButton1" />
</div>