Hi All!
Im having a problem regarding PostBack issues. Ive got a user control (WebUserControl1.ascx) which dynamically creates labels according to the value it is set from the Default.aspx page.
Code In Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
WebUserControl1 _control = new WebUserControl1();
protected void Page_Init(object sender, EventArgs e)
{
_control = Page.LoadControl("WebUserControl1.ascx") as WebUserControl1;
_control.clickedEvent += new EventHandler(_control_clickedEvent);
}
protected void Button1_Click(object sender, EventArgs e)
{
_control.Count = 5;
_control.clickedEvent += new EventHandler(_control_clickedEvent);
_control.draw();
PlaceHolder1.Controls.Add(_control);
}
protected void _control_clickedEvent(object sender, EventArgs e)
{
Response.Write("Test");
}
Code In WebUserControl1.ascx.cs
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public event EventHandler clickedEvent;
private int _count;
public int Count
{
get { return _count; }
set { _count = value; }
}
public void draw()
{
for (int i = 0; i <= _count; i++)
{
Button button = new Button();
button.Text = Convert.ToString(i);
button.Click += new EventHandler(button_Click);
Panel1.Controls.Add(button);
}
}
void button_Click(object sender, EventArgs e)
{
clickedEvent(this, e);
}
}
Now, on Default.aspx I have a button which creates the instance of a control and assigns the value to count which is then placed on the PlaceHolder. However when I click on the buttons (the ones dynamically created) the click event isnt firing, and the WebUserControl in Page_Init is set as null, meaning its losing its reference
Can anyone help out please?
Thanks