NickMalloy
asked on
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;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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.
Control C = FindControlRecursive(Page,
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.
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?
ASKER
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.
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.
ASKER
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
ASKER