Home All Groups Group Topic Archive Search About

Adding Event Handler to Dynamically added Controls

Author
25 Apr 2005 7:03 PM
John Kilgo
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

Author
25 Apr 2005 9:05 PM
Brock Allen
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



Show quoteHide quote
> 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
Author
26 Apr 2005 1:10 AM
John Kilgo
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

Show quoteHide quote
>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
Author
26 Apr 2005 3:14 AM
Brock Allen
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



Show quoteHide quote
> 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
Author
26 Apr 2005 2:05 PM
John Kilgo
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");
  }
}

Show quoteHide quote
>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