Event handler not firing in custom control

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
 
B

bruce barker

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)
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
Attaching event handler dynamically to a control in javascript

There are several ways to attach an event handler to a control dynamically using javascript.

document.getElementById("btn").attachEvent("onclick", NewFuntion);
document.getElementById("btn").onclick = NewFuntion;
Lets say a button object in your HTML code already has a function assigned in its onclick event onclick="function1();" and you want to add another function (say NewFuntion() ) to the onclick attribute of the button, you can do so in more than one ways.

function function1()
{
....
}
function NewFuntion()
{
...
}
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top