Event handling in composite server controls

L

luca

Hi all.
My problem is that I can't handle events raised from child components within
a composite server control when the control is created dynamically.
Everything works fine if the same control is defined within an aspx page
<MyTag:MyControl id="MyControl1" runat="server">: MyControl handle
succesfully events raised from its children.
When I try to create dynamically MyControl and to Add it to another control,
event handling doesn't work : its children raise events that it is not able
to handle anymore. Ie. : I create the control (MyControl control = new
myControl()) and add it to a table cell control
(cell.Controls.Add(control)).
Why? Till now the only event raised is my headache :)

Here is an example with some code more:

----- I define MyControl:
....
public class MyControl : WebControl
{

....

protected override void CreateChildControls()
{
ImageButton imgButton = new ImageButton();
//BUTTON CLICK EVENT HANDLER
imgButton.Click+=new
ImageClickEventHandler(this.imgButton_Click);
imgButton.AlternateText="ciao";
this.Controls.Add(imgButton);
}

//CLICK EVENT HANDLER DEFINITION
private void imgButton_Click(object sender, ImageClickEventArgs e)
{
...bla bla bla
}

...
}

---- case 1) MyControl succesfully handles events raised from its
children --------

I put MyControl within an aspx page: <MyTag:MyControl id="MyControl1"
runat="server"></MyTag:MyControl>, when I click on the button within
MyControl the event is caught and imgButton_Click correctly executed .


---- case 2) MyControl (damned!) is not able to handle its children
events -------

I put a table in the aspx page : <asp:Table id="_table" ></asp:Table> and
not MyControl.
In the code behind aspx page I hook MyControl to table's cells:

for(int j=0; j<numerorighe; j++)
{
row = new TableRow();
_table.Rows.Add(row);
for(int i=0; i<numerocolonne; i++)
{
cell = new TableCell();
//**GENERO IL CONTROLLO DINAMICAMENTE
MyControl control = new myControl();
cell.Controls.Add(control);
row.Cells.Add(cell);
}
}
The table is correctly generated (there is an istance of MyControl in each
cell of the table) but when I clik on the button contained in MyControl
nothing happen! imgButton_Click function, coupled to Click event of the
image button, is not executed at all!!


Bye.
Luca.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top