Event handler not firing in custom control

Discussion in 'ASP .Net' started by Shannon Cayze, Dec 1, 2008.

  1. 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
     
    Shannon Cayze, Dec 1, 2008
    #1
    1. Advertising

  2. Shannon Cayze

    bruce barker Guest

    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)


    "Shannon Cayze" wrote:

    > 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
    >
     
    bruce barker, Dec 1, 2008
    #2
    1. Advertising

  3. Shannon Cayze

    elizas

    Joined:
    Jan 27, 2010
    Messages:
    27
    Location:
    India
    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()
    {
    ...
    }
     
    elizas, Apr 22, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jespey
    Replies:
    0
    Views:
    554
    jespey
    Nov 12, 2003
  2. Wee Bubba
    Replies:
    0
    Views:
    554
    Wee Bubba
    May 24, 2004
  3. Replies:
    0
    Views:
    696
  4. TS
    Replies:
    4
    Views:
    11,779
    Steven Cheng[MSFT]
    Aug 22, 2006
  5. Replies:
    1
    Views:
    750
    Damien
    Feb 22, 2007
Loading...

Share This Page