tags:

views:

102

answers:

4

Hi,

I create a servercontrol for ASP.Net. For being short and precise I just list the things I do:

(1) Call EnsureChildControls in OnInit

(2) Create a dynamic table in CreateChildControls:

Table aTable = new Table();
aTable.ID = this.ID + "$" + T;
TableRow aRow = new TableRow();
aRow.ID = aTable.ID + "$R" + <COUNTER>.ToString();
TableCell aCell = new TableCell();

createLinkButton(row, col, caption, aCell, aRow.ID);

this.Controls.Add(aTable);

(3) Have a function like:

void aLinkButton_Command(object sender, CommandEventArgs e)
{
    // Some stuff
    return;
}

(4) Have a function like:

void createLinkButton(int row, int col, string caption, TableCell aCell, string baseID) {
    LinkButton lb = new LinkButton();
    lb.ID = baseID + "$" + row.ToString() + col.ToString();
    lb.Command += new CommandEventHandler(aLinkButton_Command);
    lb.Text = caption;
    lb.CommandName = "<command>";
    aCell.Controls.Add(lb);
    return;
}

But: The event is never called. The href is redered for postback, but the wired event never gets triggered.

No ideas anymore :(

Any ideas

A: 

Are you adding you table to the server controls .Controls collection, e.g.

this.Controls.Add(aTable);

Doesn't look like it from your code sample, so I expect this might be the problem

Paul Nearney
Hi,I ommit that call, but at the end of CreateChildcontrols it is added to the controls collection.Sascha
Sascha
+1  A: 

You will need to recreate the dynamically created LinkButton on Postback for it's associated EventHander to fire. Rather than explain the whole thing, I would highly recommend that you read Scott Mitchell's classic article on the subject.

Cerebrus
Hi,I've overriden OnInit and called EnsureChildControls. That should garantee that the controls are recreated on postback, shouldn't it?Sascha
Sascha
Nope... I don't believe it should guarantee this.
Cerebrus
As far as I understand, EnsureChildControls determines if a control contains childcontrols, if not it will create them.How would you garantee creation if not using this function?
Sascha
Tried to call CreateChildControls in OnInit: two times the table.Changed CreatedChildControls to something else (normal function, not overriding), calling it in OnInit: One time, still no events
Sascha
A: 

I think implementing IPostBackEventHandler interface may help.

In your case you can implement Interface and in RisePostBackEvent you can call your event handler method.

Hope this helps !

Canavar
Hi,no. not called at all. It isn't a plain control, but something tying different other asp.net controls dynamically together. So IPostBackEventHandler doesn't seem to fit in here.
Sascha
A: 

Look at the page (.aspx file) where you are using the server control. Make sure that you are adding the control to the page's control tree on both initial requests and postback requests. All events are handled on postback requests and if the control that generated an event is not present during the postback, the event handler will never fire.

William Gross
The server control is hard wired through a tag in the asp.net code. So it should be added to the control tree on both request types.
Sascha