Event handler not firing in custom control

  • Thread starter Thread starter Shannon Cayze
  • Start date Start date
S

Shannon Cayze

Hello all,

I'm trying to develop a control that contains LinkButtons for which I
need to attach event handlers to the Click event. The following works
fine and the event handler is invoked:

public class TestCompositeControl : CompositeControl {
private LinkButton _linkButton = null;

protected override void CreateChildControls() {
base.CreateChildControls();

this._linkButton = new LinkButton();
this._linkButton.ID = "Link_Button";
this._linkButton.Text = "Link Button";
this._linkButton.Click += new EventHandler(_linkButton_Click);
this.Controls.Add(this._linkButton);
}

protected void _linkButton_Click(object sender, EventArgs e) {
}
}

However, the following will not allow the event handler to be invoked
when the LinkButton is placed in a Panel. When viewing the source,
it's apparent that no reference to __doPostBack is generated for the
LinkButton. I'm just not sure why.

public class TestCompositeControl : CompositeControl {
private LinkButton _linkButton = null;
private Panel _panel = new Panel();

protected override void CreateChildControls() {
base.CreateChildControls();

this._panel = new Panel();
this._panel.ID = "TestPanel";

this._linkButton = new LinkButton();
this._linkButton.ID = "Link_Button";
this._linkButton.Text = "Link Button";
this._linkButton.Click += new EventHandler(_linkButton_Click);
this._panel.Controls.Add(this._linkButton);
}

protected override void Render(HtmlTextWriter writer) {
this._panel.RenderControl(writer);
writer.WriteBreak();
base.Render(writer);
}

protected void _linkButton_Click(object sender, EventArgs e) {
}
}

I do have a specific reason for trying to do this. I'm implementing a
control that inherits from DataList and implements paging. I haven't
found a way to successfully add the paging LinkButtons directly to the
DataList's Controls collection, so I'm trying to add them to a panel
and render that panel, followed by the DataList itself.

Can anyone tell me how to do what I'm trying to do or suggest a better
way?

Thanks in advance,
Shannon
 
you have several problem:

1) as the linkbutton is a child of the panel, which is not in page control
tree, there is no way for the page to find the link_button to fire the event
based on postback data.
2) as you only call render on the panel, which just call renders of the
link_button, no prerender event processing is run.
3) as the linkbutton doesn't have a parent page, no javascript is rendered.


-- bruce (sqlwork.com)
 
Back
Top