473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dynamic Checkbox Problem in ASP .NET

Hi All,

I created a dynamic checkbox in ASP .Net inside a Button1_Click event method (outside the page_load event) and performed the event handling method for the CheckedChanged event and when I check the checkbox at runtime nothing happens (checkBoxObj.check is always false). Does anyone have information on how to make this work? Note: I can get it to work inside Page_Load event. I need help in getting it to work in a different event method. See my below code snippet:


Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e) 
  2.  
  3. // Create new CheckBox control. 
  4. CheckBox NewCheckBox = new CheckBox(); 
  5. NewCheckBox.ID = "CheckBoxId"; 
  6. NewCheckBox.Text = "Dynamic CheckBox"; 
  7. NewCheckBox.AutoPostBack = true; 
  8.  
  9.  
  10. //// Register the event-handling method for the CheckedChanged event. 
  11. NewCheckBox.CheckedChanged += new EventHandler(this.Check_Change); 
  12.  
  13. // Add the control to the Controls collection of the 
  14. // PlaceHolder control. 
  15. Place.Controls.Clear(); 
  16. Place.Controls.Add(NewCheckBox); 
  17.  
  18.  
  19.  
  20.  
  21. void Check_Change(Object sender, EventArgs e) 
  22.  
  23. // Retrieve the CheckBox control from the PlaceHolder control. 
  24. CheckBox check = (CheckBox)Place.FindControl("CheckBoxId"); 
  25.  
  26. // Display the appropriate message based on the state of the 
  27. // CheckBox control. 
  28. if (check.Checked) 
  29. TextBox1.Text = "I'm checked"; 
  30. else 
  31. TextBox1.Text = "I'm not checked"; 
  32.  


Thanks!
Sep 1 '08 #1
1 5980
Frinavale
9,735 Expert Mod 8TB
You need to initialize your CheckBox control in the Page_Init code otherwise the control will not be loaded during postback and your event for the control will be lost.

Check out the article on how to use dynamic controls in ASP.NET

-Frinny
Sep 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: pizzy | last post by:
INTRO: I tried to clean it up for easy reading. I hope I didn't make any mistakes. PROBLEM: WOW, this is some crazy sh!t. I can't get my checkbox (see "TAGSELECTED") to print my textboxes (see...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
5
by: Irfan Akram | last post by:
Hi Mr.Steve, First of all many thanks for your kind response. The thing is that I am trying to control the user's action of deselecting a checkbox, once he has selected it. Also the checkbox has...
5
by: lucius | last post by:
Can someone provide a sample of how to add a new CheckBox control to an ASP.NET page, dynamically completely in code-behind? I am having a problem understanding which event should handle the...
3
by: Raymond | last post by:
I am having a problem about the dynamic checkbox Private Sub Page_Load Me.NumberOfChkControls = 0 Dim cbCheckBox As New CheckBox cbCheckBox.Text = .Id.ToString cbCheckBox.ID = "ControlID_" +...
3
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hello, I've created a dynamic button and checkbox and i want to create an event for each one of them. altough i've created the event and assigned them to each control, nothing happens, the...
2
by: scottSD | last post by:
Hi everyone. this is my first post here, but i've found quite a bit of great information from reading the forums in the past. i'm not sure if what i'm trying to do is possible or not, but here it...
0
by: =?Utf-8?B?SWRlcm9ja3M=?= | last post by:
Hi All, I created a dynamic checkbox in ASP .Net inside a Button1_Click event method (outside the page_load event) and performed the event handling method for the CheckedChanged event and I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.