Adding Event Handler to Dynamically added Controls

J

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
 
B

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?
 
J

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
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?

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

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
 
J

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");
}
}
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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top