views:

235

answers:

1

Hello.

I have a blank user control (child) and during Page_Load i create some text boxes and a button. I also add an event to the button.

I then load that user control dynamically from a placeholder in another usercontrol (parent). Both controls are rendered correctly but when i click on the button, the event isnt fired. Any ideas on how to handle the event?

Child code:

 protected void Page_Load(object sender, EventArgs e)
{    
    /*... other user controls ..*/
    UpdateButton = new LinkButton();
    UpdateButton.ID = "buttonid";
    UpdateButton.Text = "text";
    UpdateButton.Click += new EventHandler(UpdateButton_Click);
    this.Controls.Add(UpdateButton);
}

protected override void Render(HtmlTextWriter writer)
{
    for (int i = 0; i < this.Controls.Count; i++)
    {
        this.Controls[i].RenderControl(writer);
    }
}

public void UpdateButton_Click(object sender, EventArgs e)
{
    //Do stuff
}

Parent code:

protected void Page_Load(object sender, EventArgs e)
{    
    placeholder.Controls.Clear();
    ChildControl uc = (ChildControl)LoadControl("~/UserControls/ChildControl.ascx");
    placeholder.Controls.Add(uc);
}

If i use the child control directy (ie without the parent the control) the click event is raised and handled nicely. But when i use the parent control, the debugger wont even hit a breakpoint inside UpdateButton_Click().

Thanks in advance.

A: 

I think I know what might be happening. In your parent Page_Load you are calling placeholder.Controls.Clear(); This does what you would imagine and clears the control, including any events that have occurred. What happens when remove this line? Do you get an additional one created on each postback?

Daniel Dyson
I removed that line and get just one child control per postback, but i still can't catch the event.
Luís Carlos