Reputation: 1504
The title is not entirely accurate, I have a Container control with a property of type PanelControlCollection
which is basically a collection of PanelControl
:
public class PanelControl : System.Web.UI.WebControls.Panel
{
public string Title { get; set; }
}
public class PanelControlCollection : Collection<PanelControl> {}
My custom User Control:
public partial class FormSlideShow : BaseUserControl
{
[PersistenceMode(PersistenceMode.InnerProperty)]
public PanelControlCollection Panels Title { get; set; }
protected override void Render(HtmlTextWriter writer)
{
foreach(var panel in Panels)
{
ContentPlaceHolder.Controls.Add(
new LiteralControl("<fieldset class='step' Title='" + panel.Title + "'>"));
ContentPlaceHolder.Controls.Add(panel);
ContentPlaceHolder.Controls.Add(
new LiteralControl("</fieldset>"));
}
base.Render(writer);
}
}
I wanted to be able to make a custom control and add pages in markup (code in Default.aspx):
<ctrl:FormSlide runat="server" ID="frmSlide">
<Panels>
<ctrl:PanelControl runat="server" ID="page1"><asp:Button runat="server" ID="btnTest" OnClick="btnTest_Onclick" /></ctrl:PanelControl>
<ctrl:PanelControl runat="server" ID="PanelControl1">Page 2</ctrl:PanelControl>
</Panels>
</ctrl:FormSlide>
My question is, if I want to host the control in a normal aspx page, how can I connect my Button control (btnTest) to the OnClick event in the code behind of default.aspx? If I put a normal Panel control with a button I can attach events to the onclick but not with my custom control.
btw here is my code behind on default.aspx, the code which I want to run but can't:
protected void btnTest_Onclick(object sender, EventArgs e)
{
Response.Redirect("http://www.google.com");
}
Thanks.
Upvotes: 2
Views: 862
Reputation: 1355
Try moving this:
foreach(var panel in Panels)
{
ContentPlaceHolder.Controls.Add(
new LiteralControl("<fieldset class='step' Title='" + panel.Title + "'>"));
ContentPlaceHolder.Controls.Add(panel);
ContentPlaceHolder.Controls.Add(
new LiteralControl("</fieldset>"));
}
base.Render(writer);
into OnInit
rather than Render
. I think Render
is too late to register PostBack events with ASP.NET. I was able to reproduce your issue and then resolve it by moving that piece of code.
Of course, base.Render(writer);
should be replaced with base.OnInit(e);
Upvotes: 2