?
Solved

Find a checkbox control on my page

Posted on 2011-10-17
8
Medium Priority
?
224 Views
Last Modified: 2012-05-12
I'm trying to find a checkbox on my page. I store the ID of the checkbox that I want to check in the DB, with my code, the controls just show up as null, so right now I am unable to grab the control. How can I get the control to check. In my code is several attempts to grab it.
string TypeofR = p2.TypeOfRelease;
                                if (!string.IsNullOrEmpty(TypeofR))
                                {
                                   ContentPlaceHolder mpContentPlaceHolder;
                                   mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl("MainContent");
                                   RadioButton tb = (RadioButton)Master.FindControl("MainContent").FindControl(TypeofR);
                                   Control myControl1 = mpContentPlaceHolder.FindControl(TypeofR);
                                   if (myControl1 != null)
                                   {
                                       ((CheckBox)myControl1).Checked = true;
                                   }
                                }

Open in new window

0
Comment
Question by:NickMalloy
  • 4
  • 4
8 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 1000 total points
ID: 36981144
You can use a recursive findcontrol method on the Page object...

http://www.mha.dk/post/Recursive-PageFindControl.aspx
0
 

Author Comment

by:NickMalloy
ID: 36981655
How do you call this function. how is this different than a findcontrol?
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36981794
FindControlRecursive(Page, "ControlIdToFind");

This is different because it will loop through all child controls of the first parameter and then all child controls of those controls, and all child controls of those controls...

FindControl will only find a control by id if it is a direct child of the selected control.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:NickMalloy
ID: 36981851
Does it matter if my page is within a masterpage? I did this

Control C = FindControlRecursive(Page, TypeofR);
 if (C != null)
 {
   
 }

I send the ID of a control in TypeofR. It is the ID of the control which is a physical control on the page. When I ran the code C comes back as null.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36981924
What exactly does "p2.TypeOfRelease" do? It doesn't seem like you actually are returning the Control ID on any control you added? Alternatively, are you sure you're recreating all the dynamic controls on the page each postback?
0
 

Author Comment

by:NickMalloy
ID: 36982034
In my database it holds the ID of the control they checked. It simply has the name like chkDelta is one option. So p2.TypeOfRelease is referencing the database field that stores the name. in my code I make sure TypeofRelease is not null before doing anything. In my test record I am looking for chkDelta.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36982092
First, debug your code and make sure the value of TypeofR is "chkDelta" as expected. Next, how are you adding chkDelta to the form? My guess is you create it dynamically. If this is the case, you need to make sure you recreate it on postback EVERY TIME.
0
 

Author Comment

by:NickMalloy
ID: 36982109
Actually it was a regular control, not dynamic. Still you got me thinking with the value in the DB. I was debugging all along but I wasn't trimming. That fixed it. Thanks

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question