Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Adding Event Handler to Dynamically added Controls

Reply
Thread Tools

Adding Event Handler to Dynamically added Controls

 
 
John Kilgo
Guest
Posts: n/a
 
      04-25-2005
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.a spx.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
 
Reply With Quote
 
 
 
 
Brock Allen
Guest
Posts: n/a
 
      04-25-2005
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.a spx.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




 
Reply With Quote
 
 
 
 
John Kilgo
Guest
Posts: n/a
 
      04-26-2005
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.a spx.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
 
Reply With Quote
 
Brock Allen
Guest
Posts: n/a
 
      04-26-2005
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.a spx.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




 
Reply With Quote
 
John Kilgo
Guest
Posts: n/a
 
      04-26-2005
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.a spx.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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically-added Button not triggering event handler. Why? parsifal ASP .Net 4 03-15-2007 04:57 PM
Event Handler that creates adds another event handler kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
Inserting Controls Dynamically - with Validators Also Added Dynamically Jeffrey Todd ASP .Net 1 06-02-2005 04:33 PM
Adding event handler for dynamically created controls =?Utf-8?B?QW5kcmV3IFdpZWQ=?= ASP .Net 3 04-25-2004 07:36 PM
Event firing with dynamically added server controls MS Newsgroups ASP .Net 5 12-13-2003 02:56 PM



Advertisments