|
| Get a control in the masterpage or page from the current page |
 |
Sat, 2 Feb 2008 14:30:43 +0000 |
Just thought I would post this. You use it like this if you were looking for a
TreeView.GetControl g = newGetControl();
Control tvSel = newControl();if (g.getControlByName(this, ref tvSel,
"tvSel"))
if(((TreeView)tvSel).SelectedNode!=null)lbl.Text = "Selected: " +
((TreeView)tvSel).SelectedNode.Text;
elselbl.Text = "Control Found but Not Selected";
else
lbl.Text = "Control Not Found";
And here is the class:
publicclassGetControl
{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>publicbool getControlByName(Page p, refControl 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>privatebool lookRecursiveForControl(refControl 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)))
break;
}
}
}
return found;
}
}
|
| Post Reply
|
|
|
|
|
|
|
|
|
|