Find a checkbox control on my page

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

NickMalloyAsked:
Who is Participating?
 
strickddConnect With a Mentor Commented:
You can use a recursive findcontrol method on the Page object...

http://www.mha.dk/post/Recursive-PageFindControl.aspx
0
 
NickMalloyAuthor Commented:
How do you call this function. how is this different than a findcontrol?
0
 
strickddCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
NickMalloyAuthor Commented:
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
 
strickddCommented:
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
 
NickMalloyAuthor Commented:
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
 
strickddCommented:
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
 
NickMalloyAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.