Adding Event Handler to Dynamically added Controls

Discussion in 'ASP .Net Web Controls' started by John Kilgo, Apr 25, 2005.

  1. John Kilgo

    John Kilgo Guest

    I'm dynamically adding literals and .net controls to a Placeholder control. Everything works to a point(my literals are added and my controls are added). But I need to add an event handler, specificall for a checkbox. My code is something like this:
    litText = new Literal();
    litText.ID = "anID";
    phContent.Controls.Add(litText);
    litText.Text = "Some Text";
    CheckBox myCheckBox = new CheckBox();
    myCheckBox.ID = "aCheckBox";
    myCheckBox.TextAlign = TextAlign.Right;
    myCheckBox.Text = "SomeText";
    phContent.Controls.Add(myCheckBox);

    Then I'm trying to do something like the following:
    myCheckBox.CheckedChanged += new System.EventHandler(myCheckBox_CheckedChanged);

    It burps with the message "c:\inetpub\wwwroot\MainPetitionDynamic\WebForm1.aspx.cs(34): The name 'myCheckBox_CheckedChanged' does not exist in the class or namespace 'MainPetitionDynamic.WebForm1'

    I've a couple of other syntaxes but nothing seems to work. How can I add the event handler dynamically.

    Thanks,
    John

    ___
    Newsgroups brought to you courtesy of www.dotnetjohn.com
     
    John Kilgo, Apr 25, 2005
    #1
    1. Advertising

  2. John Kilgo

    Brock Allen Guest

    This should work as long as you have a method called "myCheckBox_CheckedChanged"
    in the context where you're code is executing. So if this code below is inside
    your page, then as long as the page has "myCheckBox_CheckedChanged" (with
    the correct signature) then it should work. Do you have any more info?

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > I'm dynamically adding literals and .net controls to a Placeholder
    > control. Everything works to a point(my literals are added and my
    > controls are added). But I need to add an event handler, specificall
    > for a checkbox. My code is something like this:
    >
    > litText = new Literal();
    >
    > litText.ID = "anID";
    >
    > phContent.Controls.Add(litText);
    >
    > litText.Text = "Some Text";
    >
    > CheckBox myCheckBox = new CheckBox();
    >
    > myCheckBox.ID = "aCheckBox";
    >
    > myCheckBox.TextAlign = TextAlign.Right;
    >
    > myCheckBox.Text = "SomeText";
    >
    > phContent.Controls.Add(myCheckBox);
    >
    > Then I'm trying to do something like the following:
    > myCheckBox.CheckedChanged += new
    > System.EventHandler(myCheckBox_CheckedChanged);
    >
    > It burps with the message
    > "c:\inetpub\wwwroot\MainPetitionDynamic\WebForm1.aspx.cs(34): The name
    > 'myCheckBox_CheckedChanged' does not exist in the class or namespace
    > 'MainPetitionDynamic.WebForm1'
    >
    > I've a couple of other syntaxes but nothing seems to work. How can I
    > add the event handler dynamically.
    >
    > Thanks,
    > John
    > ___
    > Newsgroups brought to you courtesy of www.dotnetjohn.com
     
    Brock Allen, Apr 25, 2005
    #2
    1. Advertising

  3. John Kilgo

    John Kilgo Guest

    For now at least I'm executing this code in the Page_Load event. I'm not sure how I can add a method (dynamically) within that block of code. By "method" do you mean the usual "private void myCheckBox_CheckedChanged(object sender, EventArgs e) type of method? I'm doing this dynamically because until I read the database I'm not going to know how many checkbox controls I must add. They will all require CheckedChanged methods.

    Thanks,
    John

    >This should work as long as you have a method called "myCheckBox_CheckedChanged"
    > in the context where you're code is executing. So if this code below is inside
    > your page, then as long as the page has "myCheckBox_CheckedChanged" (with
    > the correct signature) then it should work. Do you have any more info?
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    > > I'm dynamically adding literals and .net controls to a Placeholder
    > > control. Everything works to a point(my literals are added and my
    > > controls are added). But I need to add an event handler, specificall
    > > for a checkbox. My code is something like this:
    > >
    > > litText = new Literal();
    > >
    > > litText.ID = "anID";
    > >
    > > phContent.Controls.Add(litText);
    > >
    > > litText.Text = "Some Text";
    > >
    > > CheckBox myCheckBox = new CheckBox();
    > >
    > > myCheckBox.ID = "aCheckBox";
    > >
    > > myCheckBox.TextAlign = TextAlign.Right;
    > >
    > > myCheckBox.Text = "SomeText";
    > >
    > > phContent.Controls.Add(myCheckBox);
    > >
    > > Then I'm trying to do something like the following:
    > > myCheckBox.CheckedChanged += new
    > > System.EventHandler(myCheckBox_CheckedChanged);
    > >
    > > It burps with the message
    > > "c:\inetpub\wwwroot\MainPetitionDynamic\WebForm1.aspx.cs(34): The name
    > > 'myCheckBox_CheckedChanged' does not exist in the class or namespace
    > > 'MainPetitionDynamic.WebForm1'
    > >
    > > I've a couple of other syntaxes but nothing seems to work. How can I
    > > add the event handler dynamically.
    > >
    > > Thanks,
    > > John
    > > ___
    > > Newsgroups brought to you courtesy of www.dotnetjohn.com

    >


    >
    >


    ___
    Newsgroups brought to you courtesy of www.dotnetjohn.com
     
    John Kilgo, Apr 26, 2005
    #3
  4. John Kilgo

    Brock Allen Guest

    You must have a method pre-declared to do what you're looking to do. It's
    ok to have all 30+ checkboxes have the same change event though -- the first
    parameter (object sender) helps you determine which control raised the event.
    So you'll have to do something dynamically to "do the right thing", whatever
    that is for your dynamic checkbox page. HTH

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > For now at least I'm executing this code in the Page_Load event. I'm
    > not sure how I can add a method (dynamically) within that block of
    > code. By "method" do you mean the usual "private void
    > myCheckBox_CheckedChanged(object sender, EventArgs e) type of method?
    > I'm doing this dynamically because until I read the database I'm not
    > going to know how many checkbox controls I must add. They will all
    > require CheckedChanged methods.
    >
    > Thanks,
    > John
    >> This should work as long as you have a method called
    >> "myCheckBox_CheckedChanged" in the context where you're code is
    >> executing. So if this code below is inside your page, then as long as
    >> the page has "myCheckBox_CheckedChanged" (with the correct signature)
    >> then it should work. Do you have any more info?
    >>
    >> -Brock
    >> DevelopMentor
    >> http://staff.develop.com/ballen
    >>> I'm dynamically adding literals and .net controls to a Placeholder
    >>> control. Everything works to a point(my literals are added and my
    >>> controls are added). But I need to add an event handler, specificall
    >>> for a checkbox. My code is something like this:
    >>>
    >>> litText = new Literal();
    >>>
    >>> litText.ID = "anID";
    >>>
    >>> phContent.Controls.Add(litText);
    >>>
    >>> litText.Text = "Some Text";
    >>>
    >>> CheckBox myCheckBox = new CheckBox();
    >>>
    >>> myCheckBox.ID = "aCheckBox";
    >>>
    >>> myCheckBox.TextAlign = TextAlign.Right;
    >>>
    >>> myCheckBox.Text = "SomeText";
    >>>
    >>> phContent.Controls.Add(myCheckBox);
    >>>
    >>> Then I'm trying to do something like the following:
    >>> myCheckBox.CheckedChanged += new
    >>> System.EventHandler(myCheckBox_CheckedChanged);
    >>>
    >>> It burps with the message
    >>> "c:\inetpub\wwwroot\MainPetitionDynamic\WebForm1.aspx.cs(34): The
    >>> name 'myCheckBox_CheckedChanged' does not exist in the class or
    >>> namespace 'MainPetitionDynamic.WebForm1'
    >>>
    >>> I've a couple of other syntaxes but nothing seems to work. How can
    >>> I add the event handler dynamically.
    >>>
    >>> Thanks,
    >>> John
    >>> ___
    >>> Newsgroups brought to you courtesy of www.dotnetjohn.com

    > ___
    > Newsgroups brought to you courtesy of www.dotnetjohn.com
     
    Brock Allen, Apr 26, 2005
    #4
  5. John Kilgo

    John Kilgo Guest

    Brock, thank you. The following code works! -- John

    private void Page_Load(object sender, System.EventArgs e)
    {
    CheckBox myCheckBox = new CheckBox();
    myCheckBox.ID = "myCheckBox";
    myCheckBox.Text = "Enter different sentence for each count?";
    myCheckBox.TextAlign = TextAlign.Right;
    myCheckBox.AutoPostBack = true;
    myCheckBox.CheckedChanged += new System.EventHandler(Checked_Changed);
    phContent.Controls.Add(myCheckBox);
    }

    private void Checked_Changed (object sender, System.EventArgs e)
    {
    string strControlID = ((CheckBox)sender).ID;
    if (strControlID == "myCheckBox")
    {
    Response.Write("Found it");
    }
    }

    >You must have a method pre-declared to do what you're looking to do. It's
    > ok to have all 30+ checkboxes have the same change event though -- the first
    > parameter (object sender) helps you determine which control raised the event.
    > So you'll have to do something dynamically to "do the right thing", whatever
    > that is for your dynamic checkbox page. HTH
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    > > For now at least I'm executing this code in the Page_Load event. I'm
    > > not sure how I can add a method (dynamically) within that block of
    > > code. By "method" do you mean the usual "private void
    > > myCheckBox_CheckedChanged(object sender, EventArgs e) type of method?
    > > I'm doing this dynamically because until I read the database I'm not
    > > going to know how many checkbox controls I must add. They will all
    > > require CheckedChanged methods.
    > >
    > > Thanks,
    > > John
    > >> This should work as long as you have a method called
    > >> "myCheckBox_CheckedChanged" in the context where you're code is
    > >> executing. So if this code below is inside your page, then as long as
    > >> the page has "myCheckBox_CheckedChanged" (with the correct signature)
    > >> then it should work. Do you have any more info?
    > >>
    > >> -Brock
    > >> DevelopMentor
    > >> http://staff.develop.com/ballen
    > >>> I'm dynamically adding literals and .net controls to a Placeholder
    > >>> control. Everything works to a point(my literals are added and my
    > >>> controls are added). But I need to add an event handler, specificall
    > >>> for a checkbox. My code is something like this:
    > >>>
    > >>> litText = new Literal();
    > >>>
    > >>> litText.ID = "anID";
    > >>>
    > >>> phContent.Controls.Add(litText);
    > >>>
    > >>> litText.Text = "Some Text";
    > >>>
    > >>> CheckBox myCheckBox = new CheckBox();
    > >>>
    > >>> myCheckBox.ID = "aCheckBox";
    > >>>
    > >>> myCheckBox.TextAlign = TextAlign.Right;
    > >>>
    > >>> myCheckBox.Text = "SomeText";
    > >>>
    > >>> phContent.Controls.Add(myCheckBox);
    > >>>
    > >>> Then I'm trying to do something like the following:
    > >>> myCheckBox.CheckedChanged += new
    > >>> System.EventHandler(myCheckBox_CheckedChanged);
    > >>>
    > >>> It burps with the message
    > >>> "c:\inetpub\wwwroot\MainPetitionDynamic\WebForm1.aspx.cs(34): The
    > >>> name 'myCheckBox_CheckedChanged' does not exist in the class or
    > >>> namespace 'MainPetitionDynamic.WebForm1'
    > >>>
    > >>> I've a couple of other syntaxes but nothing seems to work. How can
    > >>> I add the event handler dynamically.
    > >>>
    > >>> Thanks,
    > >>> John
    > >>> ___
    > >>> Newsgroups brought to you courtesy of www.dotnetjohn.com

    > > ___
    > > Newsgroups brought to you courtesy of www.dotnetjohn.com

    >


    >
    >


    ___
    Newsgroups brought to you courtesy of www.dotnetjohn.com
     
    John Kilgo, Apr 26, 2005
    #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. MS Newsgroups
    Replies:
    5
    Views:
    1,534
    Anders Borum
    Dec 13, 2003
  2. =?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,413
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Apr 25, 2004
  3. Jeffrey Todd
    Replies:
    1
    Views:
    4,152
    Peter Blum
    Jun 2, 2005
  4. Replies:
    1
    Views:
    736
    Damien
    Feb 22, 2007
  5. parsifal
    Replies:
    4
    Views:
    672
    Alexey Smirnov
    Mar 15, 2007
Loading...

Share This Page