How to refer to a dynamically created control in event handler?

Discussion in 'ASP .Net' started by gnewsgroup, Feb 19, 2008.

  1. gnewsgroup

    gnewsgroup Guest

    Suppose I create a button on the fly and assign an event handler to it
    in the code-behind like so:

    protected Button CreateMyButton()
    {
    Button myButton = new Button();
    myButton.Text = "Test";
    myButton.Click += new EventHandler(myButton_Click);
    return myButton;
    }

    Now, in myButton_Click event handler, how do I get a reference to the
    newly created Button object called myButton?

    In the Click event handler, we cannot directly say something like

    myButton.Text = "Text has changed";

    which I tried, and for which I got this:

    System.NullReferenceException: Object reference not set to an instance
    of an object.

    I got the same exception even if I declared myButton as a class-level
    private member.

    So here is the question: How to refer to a dynamically created control
    in the event handler?

    Thank you.
    gnewsgroup, Feb 19, 2008
    #1
    1. Advertising

  2. gnewsgroup

    bruce barker Guest

    two options

    1) make Button myButton a class variable instead of a local, which is how
    the aspx page does it.

    2) the first arg to the event handler is the object (button) that caused the
    event. just cast it as a Button

    -- bruce (sqlwork.com)


    "gnewsgroup" wrote:

    > Suppose I create a button on the fly and assign an event handler to it
    > in the code-behind like so:
    >
    > protected Button CreateMyButton()
    > {
    > Button myButton = new Button();
    > myButton.Text = "Test";
    > myButton.Click += new EventHandler(myButton_Click);
    > return myButton;
    > }
    >
    > Now, in myButton_Click event handler, how do I get a reference to the
    > newly created Button object called myButton?
    >
    > In the Click event handler, we cannot directly say something like
    >
    > myButton.Text = "Text has changed";
    >
    > which I tried, and for which I got this:
    >
    > System.NullReferenceException: Object reference not set to an instance
    > of an object.
    >
    > I got the same exception even if I declared myButton as a class-level
    > private member.
    >
    > So here is the question: How to refer to a dynamically created control
    > in the event handler?
    >
    > Thank you.
    >
    >
    bruce barker, Feb 19, 2008
    #2
    1. Advertising

  3. "gnewsgroup" <> wrote in message
    news:...
    > Suppose I create a button on the fly and assign an event handler to it
    > in the code-behind like so:
    >
    > protected Button CreateMyButton()
    > {
    > Button myButton = new Button();
    > myButton.Text = "Test";
    > myButton.Click += new EventHandler(myButton_Click);
    > return myButton;
    > }
    >
    > Now, in myButton_Click event handler, how do I get a reference to the
    > newly created Button object called myButton?
    >
    > In the Click event handler, we cannot directly say something like
    >
    > myButton.Text = "Text has changed";
    >
    > which I tried, and for which I got this:
    >
    > System.NullReferenceException: Object reference not set to an instance
    > of an object.
    >
    > I got the same exception even if I declared myButton as a class-level
    > private member.
    >
    > So here is the question: How to refer to a dynamically created control
    > in the event handler?


    The "sender" parameter is the control that generated the event. You'll need
    to cast it to a Button.
    Scott Roberts, Feb 19, 2008
    #3
  4. gnewsgroup

    gnewsgroup Guest

    On Feb 19, 3:57 pm, "Scott Roberts" <-webworks-
    software.com> wrote:
    > "gnewsgroup" <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > Suppose I create a button on the fly and assign an event handler to it
    > > in the code-behind like so:

    >
    > > protected Button CreateMyButton()
    > > {
    > >  Button myButton = new Button();
    > >  myButton.Text = "Test";
    > >  myButton.Click += new EventHandler(myButton_Click);
    > >  return myButton;
    > > }

    >
    > > Now, in myButton_Click event handler, how do I get a reference to the
    > > newly created Button object called myButton?

    >
    > > In the Click event handler, we cannot directly say something like

    >
    > > myButton.Text = "Text has changed";

    >
    > > which I tried, and for which I got this:

    >
    > > System.NullReferenceException: Object reference not set to an instance
    > > of an object.

    >
    > > I got the same exception even if I declared myButton as a class-level
    > > private member.

    >
    > > So here is the question: How to refer to a dynamically created control
    > > in the event handler?

    >
    > The "sender" parameter is the control that generated the event. You'll need
    > to cast it to a Button.- Hide quoted text -
    >
    > - Show quoted text -


    Thank both of you. I was tring to see what methods intellisense would
    show me while I typed up sender dot. But none of those seemed to be
    helpful.

    Now that I realize that I can cast the sender object to the type of
    the object which raised the event, I also realize that I might have
    simplified my question.

    I am actually using Dundas to create some charts on the fly. For
    example, I create Chart1 on the fly and put it in a placeholder. In
    the Dundas GanttChart example, there is a Chart1_PostPaint event
    handler which draws stuff on the chart *after* the chart has been
    rendered (my understanding). The example code goes like this:

    private void Chart1_PostPaint(object sender,
    Dundas.Charting.WebControl.ChartPaintEventArgs e)
    {
    if(sender is ChartArea)
    {
    Series series = Chart1.Series["Tasks"];
    // [snip]
    }
    }

    Note that in their example, this Chart1 is declared in the aspx file.
    The problem for me is that whereas the *sender* is ChartArea, they are
    referring to Chart1, which is not a ChartArea, but some kind of parent
    of ChartArea. I can't seem to get a reference to the parent from the
    *sender* ChartArea. That's why I got stuck.

    Mark: I did try declaring Chart1 as a class-level private member, but
    it did not help.
    gnewsgroup, Feb 19, 2008
    #4
  5. gnewsgroup

    gnewsgroup Guest

    On Feb 19, 4:26 pm, gnewsgroup <> wrote:
    > On Feb 19, 3:57 pm, "Scott Roberts" <-webworks-
    >
    >
    >
    >
    >
    > software.com> wrote:
    > > "gnewsgroup" <> wrote in message

    >
    > >news:...

    >
    > > > Suppose I create a button on the fly and assign an event handler to it
    > > > in the code-behind like so:

    >
    > > > protected Button CreateMyButton()
    > > > {
    > > >  Button myButton = new Button();
    > > >  myButton.Text = "Test";
    > > >  myButton.Click += new EventHandler(myButton_Click);
    > > >  return myButton;
    > > > }

    >
    > > > Now, in myButton_Click event handler, how do I get a reference to the
    > > > newly created Button object called myButton?

    >
    > > > In the Click event handler, we cannot directly say something like

    >
    > > > myButton.Text = "Text has changed";

    >
    > > > which I tried, and for which I got this:

    >
    > > > System.NullReferenceException: Object reference not set to an instance
    > > > of an object.

    >
    > > > I got the same exception even if I declared myButton as a class-level
    > > > private member.

    >
    > > > So here is the question: How to refer to a dynamically created control
    > > > in the event handler?

    >
    > > The "sender" parameter is the control that generated the event. You'll need
    > > to cast it to a Button.- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Thank both of you.  I was tring to see what methods intellisense would
    > show me while I typed up sender dot.  But none of those seemed to be
    > helpful.
    >
    > Now that I realize that I can cast the sender object to the type of
    > the object which raised the event, I also realize that I might have
    > simplified my question.
    >
    > I am actually using Dundas to create some charts on the fly.  For
    > example, I create Chart1 on the fly and put it in a placeholder.  In
    > the Dundas GanttChart example, there is a Chart1_PostPaint event
    > handler which draws stuff on the chart *after* the chart has been
    > rendered (my understanding).  The example code goes like this:
    >
    > private void Chart1_PostPaint(object sender,
    > Dundas.Charting.WebControl.ChartPaintEventArgs e)
    > {
    > if(sender is ChartArea)
    >   {
    >      Series series = Chart1.Series["Tasks"];
    >      // [snip]
    >   }
    >
    > }
    >
    > Note that in their example, this Chart1 is declared in the aspx file.
    > The problem for me is that whereas the *sender* is ChartArea, they are
    > referring to Chart1, which is not a ChartArea, but some kind of parent
    > of ChartArea.  I can't seem to get a reference to the parent from the
    > *sender* ChartArea.  That's why I got stuck.
    >
    > Mark: I did try declaring Chart1 as a class-level private member, but
    > it did not help.- Hide quoted text -
    >
    > - Show quoted text -


    I had a method that returns the newly-created chart, now I declare
    Chart1 as private member, and the method is changed to return void and
    keeps updating chart1 contents and then in the PostPaoint event, I
    refer to it as this.Chart1. It works! I didn't think it through.
    Thanks for Mark!
    gnewsgroup, Feb 19, 2008
    #5
    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. =?Utf-8?B?QW5kcmV3IFdpZWQ=?=

    Adding event handler for dynamically created controls

    =?Utf-8?B?QW5kcmV3IFdpZWQ=?=, Apr 19, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,366
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Apr 25, 2004
  2. Beffmans
    Replies:
    3
    Views:
    340
    Beffmans
    Jul 19, 2005
  3. Replies:
    1
    Views:
    683
    Damien
    Feb 22, 2007
  4. msimmons
    Replies:
    0
    Views:
    454
    msimmons
    Jul 16, 2009
  5. Adi
    Replies:
    2
    Views:
    185
Loading...

Share This Page