dynamically creating buttons with event handlers

Discussion in 'ASP .Net' started by Michael Dawson, Feb 22, 2006.

  1. I am creating a list of items which need to be clickable on a web page,
    but I am unfamiliar with ASP event handling. I created the following
    test control:

    public class MyCustomControl : System.Web.UI.WebControls.WebControl
    {
    //---
    public MyCustomControl()
    {
    // Init the class
    }
    //---
    protected override void Render(HtmlTextWriter output)
    {
    Button b = new Button();
    b.Text = "btn";
    b.CommandName = "Keyword_Click";
    b.Click += new EventHandler(this.b_Click);
    b.RenderControl(output);
    }
    //---
    void b_Click(object sender, EventArgs e)
    {
    throw new Exception("it works!");
    }
    }

    is it possible to do something like this?

    I would appreciate any input on this :) Thanks.
     
    Michael Dawson, Feb 22, 2006
    #1
    1. Advertising

  2. Yes, but from your example, it would make more sense to inherit your control
    directly from the Button class and get all the existing functionality out of
    it.


    karl

    P.S. - throwing an exception when something works is an interesting strategy
    :p

    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "Michael Dawson" <> wrote in message
    news:%...
    >I am creating a list of items which need to be clickable on a web page, but
    >I am unfamiliar with ASP event handling. I created the following test
    >control:
    >
    > public class MyCustomControl : System.Web.UI.WebControls.WebControl
    > {
    > //---
    > public MyCustomControl()
    > {
    > // Init the class
    > }
    > //---
    > protected override void Render(HtmlTextWriter output)
    > {
    > Button b = new Button();
    > b.Text = "btn";
    > b.CommandName = "Keyword_Click";
    > b.Click += new EventHandler(this.b_Click);
    > b.RenderControl(output);
    > }
    > //---
    > void b_Click(object sender, EventArgs e)
    > {
    > throw new Exception("it works!");
    > }
    > }
    >
    > is it possible to do something like this?
    >
    > I would appreciate any input on this :) Thanks.
     
    Karl Seguin [MVP], Feb 22, 2006
    #2
    1. Advertising

  3. That code does not throw an exception when run though, it posts back (I
    think all buttons do that automatically anyways?) but no exceptions ...
    so that that code does not run as intended, so is there something wrong
    with it? The way you put it makes it look valid code [o_O]

    Microsoft's auto complete thingy for events creates an exception like
    that by default :p I just changed the message.

    -- Michael D


    Karl Seguin [MVP] wrote:
    > Yes, but from your example, it would make more sense to inherit your control
    > directly from the Button class and get all the existing functionality out of
    > it.
    >
    >
    > karl
    >
    > P.S. - throwing an exception when something works is an interesting strategy
    > :p
    >
     
    Michael Dawson, Feb 22, 2006
    #3
  4. You need to do more work to make it postback. Your control itself needs to
    be postback aware, not just the controls within it. Hence my recommendation
    to inherit directly from Button so all that extra work is already done.

    For example, your controls should be created in CreateChildControls(). You
    also need to make sure that before the property of the control is accessed,
    you call EnsureChildControls() - this ensure that the button exists and the
    event hooked when it's accessed - my guess is that's why it doesn't work.
    Your button isn't created by the time it's being accessed.

    private Button _button;

    public Button Button
    {
    get { EnsureChildControls(); return _button; }
    set { EnsureChildControls() ; _button = value; }
    }

    protected override void CreateChildControls ()
    {
    Controls.Clear();
    _button = new Button();
    .....
    }


    that's just a rough outline :) composite controls can get pretty tricky

    Karl
    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "Michael Dawson" <> wrote in message
    news:...
    > That code does not throw an exception when run though, it posts back (I
    > think all buttons do that automatically anyways?) but no exceptions ... so
    > that that code does not run as intended, so is there something wrong with
    > it? The way you put it makes it look valid code [o_O]
    >
    > Microsoft's auto complete thingy for events creates an exception like that
    > by default :p I just changed the message.
    >
    > -- Michael D
    >
    >
    > Karl Seguin [MVP] wrote:
    >> Yes, but from your example, it would make more sense to inherit your
    >> control directly from the Button class and get all the existing
    >> functionality out of it.
    >>
    >>
    >> karl
    >>
    >> P.S. - throwing an exception when something works is an interesting
    >> strategy :p
    >>
     
    Karl Seguin [MVP], Feb 22, 2006
    #4
    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. Boban Dragojlovic

    Dynamic Buttons and Event Handlers, revisited

    Boban Dragojlovic, Jun 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,885
    Boban Dragojlovic
    Jun 24, 2003
  2. Hrvoje Vrbanc
    Replies:
    1
    Views:
    471
  3. bill
    Replies:
    1
    Views:
    423
    Ken Cox - Microsoft MVP
    Apr 9, 2006
  4. Nessuno

    Buttons and Event Handlers

    Nessuno, Feb 21, 2010, in forum: Java
    Replies:
    7
    Views:
    427
  5. Replies:
    2
    Views:
    265
Loading...

Share This Page