///
<summary>
/// Finds Controls on asp.net page or the pages masterpage.
///
</summary>
///
<param name="p">Page to look in.</param>
///
<param name="c">Will be filled with the control if found</param>
///
<param name="controlIDName">the ID of the control to find</param>
public
bool getControlByName(Page p,
ref Control c,
string controlIDName)
{
bool found =
false;
for (int i = 0; i < p.Controls.Count; ++i)
{
if (p.Controls[i]!=null)
{
if (p.Controls[i].ID !=
null)
{
if (!(found = (p.Controls[i].ID == controlIDName)))
{
if ((found = lookRecursiveForControl(ref c,
p.Controls[i], controlIDName)))
break;
}
else
{
//Control found
c = p.Controls[i];
break;
}
}
else
{
if ((found = lookRecursiveForControl(ref c,
p.Controls[i], controlIDName)))
break;
}
}
}
if (!found)
{
for (int i = 0; i < p.Master.Controls.Count; ++i)
{
if (p.Master.Controls[i] !=
null)
{
if (p.Master.Controls[i].ID !=
null)
{
if (!(found = (p.Master.Controls[i].ID == controlIDName)))
{
if ((found = lookRecursiveForControl(ref c,
p.Master.Controls[i], controlIDName)))
break;
}
else
{
//Control found
c = p.Master.Controls[i];
break;
}
}
else
{
if ((found = lookRecursiveForControl(ref c,
p.Master.Controls[i], controlIDName)))
break;
}
}
}
}
return
found;
}
///
<summary>
/// Looks for controls inside of a control recursively starting from the starting control to the very bottom of the tree.
///
</summary>
///
<param name="c">Control that would be returned if found</param>
///
<param name="controlToLookIn">Control that should be looked in to find the control</param>
///
<param name="controlIDName">the ID of the control your looking for.</param>
///
<returns>control was found</returns>
private
bool lookRecursiveForControl(ref
Control c, Control controlToLookIn,
string controlIDName)
{
bool found =
false;
for (int i = 0; i < controlToLookIn.Controls.Count; ++i)
{
if (controlToLookIn.Controls[i] !=
null)
{
if (controlToLookIn.Controls[i].ID !=
null)
{
if (!(found = (controlToLookIn.Controls[i].ID == controlIDName)))
{
if ((found = lookRecursiveForControl(ref c,
controlToLookIn.Controls[i], controlIDName)))
break;
}
else
{
//Control found
c = controlToLookIn.Controls[i];
break;
}
}
else
{
if ((found = lookRecursiveForControl(ref c,
controlToLookIn.Controls[i], controlIDName)))
Member
124 Points
85 Posts
Get a control in the masterpage or page from the current page
Feb 02, 2008 09:30 AM|carterwjeff|LINK
Just thought I would post this. You use it like this if you were looking for a TreeView.
g = new GetControl(); Control tvSel = new Control(); if (g.getControlByName(this, ref tvSel, "tvSel")) if(((TreeView)tvSel).SelectedNode!=null) "Selected: " + ((TreeView)tvSel).SelectedNode.Text; else "Control Found but Not Selected"; else "Control Not Found";And here is the class:
public class GetControl
{
public GetControl(){
}
/// <summary> /// Finds Controls on asp.net page or the pages masterpage. /// </summary> /// <param name="p">Page to look in.</param> /// <param name="c">Will be filled with the control if found</param> /// <param name="controlIDName">the ID of the control to find</param> public bool getControlByName(Page p, ref Control c, string controlIDName){
bool found = false; break; else if ((found = lookRecursiveForControl(ref c, p.Controls[i], controlIDName))) break; if (!found){
for (int i = 0; i < p.Master.Controls.Count; ++i){
if (p.Master.Controls[i] != null){
if (p.Master.Controls[i].ID != null){
if (!(found = (p.Master.Controls[i].ID == controlIDName))){
if ((found = lookRecursiveForControl(ref c, p.Master.Controls[i], controlIDName))) break; else //Control found break; else if ((found = lookRecursiveForControl(ref c, p.Master.Controls[i], controlIDName))) break; found;}
/// <summary> /// Looks for controls inside of a control recursively starting from the starting control to the very bottom of the tree. /// </summary> /// <param name="c">Control that would be returned if found</param> /// <param name="controlToLookIn">Control that should be looked in to find the control</param> /// <param name="controlIDName">the ID of the control your looking for.</param> /// <returns>control was found</returns> private bool lookRecursiveForControl(ref Control c, Control controlToLookIn, string controlIDName) bool found = false; for (int i = 0; i < controlToLookIn.Controls.Count; ++i) if (controlToLookIn.Controls[i] != null) if (controlToLookIn.Controls[i].ID != null) if (!(found = (controlToLookIn.Controls[i].ID == controlIDName))) if ((found = lookRecursiveForControl(ref c, controlToLookIn.Controls[i], controlIDName))) break; else //Control found break; else if ((found = lookRecursiveForControl(ref c, controlToLookIn.Controls[i], controlIDName)))}
}