|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Adding Event Handler to Dynamically added ControlslitText = 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 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 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 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 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
Datalist and LoadTemplate
Weird "defined in multiple places" error Referencing to .ascx How do I transfer a value from a Querystring (codebehind) to a JavaScript function? Form Post Back in ASP.Net 1.1 How do I set the value of a checkbox on my repeater control? Losing selectedIndex on Control inherted from ListBox during postback ASP.NET project and Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET How to get the file's name after click the Browse... in HtmlInputFile How to Remove item from DropDownList ? |
|||||||||||||||||||||||