Xerratus
Happily stressed out, since 1974
Wednesday, August 09, 2006
<< Brian Gets a Haircut
Code-to-Html Color Formatter >>
Find any control on the calling page from within a custom user control
For when you need to access a control from within a user control where the ID of said control is passed in as a parameter. That was a mouthful. Basically, if you create a custom user control that takes in the ID of another control (let's say a TextBox) the user control cannot find a handle to that control using the standard Page.FindControl("TextBox1"). If TextBox1 exists on the main page (whether or not it's on a master page) and your custom control needs to access TextBox1, it cannot be accessed thru the Page.FindControl method directly. Luckily, there is recursion.
Here is the method to find the control:
public
Control FindControlByID(ControlCollection controls,
string
id)
{
Control found =
null
;
foreach
(Control control
in
controls)
{
if
(control.HasControls())
{
found = FindControlByID(control.Controls, id);
if
(found !=
null
)
{
break
;
}
}
if
(control.ID == id)
{
found = control;
break
;
}
}
return
found;
}
An example of this would be if you had a user control that had to populate a textbox that was passed in as a property.
Widget.ascx
<%
@
Control
Language
="C#"
AutoEventWireup
="true"
CodeFile
="Widget.ascx.cs"
Inherits
="_controls_Widget"
%>
Widget.ascx.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
_controls_Widget : System.Web.UI.UserControl
{
private
string
_controlToUpdate;
public
string
ControlToUpdate
{
get
{
return
_controlToUpdate; }
set
{ _controlToUpdate =
value
; }
}
public
override
void
DataBind()
{
if
(_controlToUpdate !=
""
)
{
Control control = FindControlByID(Page.Controls, _controlToUpdate);
if
(control
is
TextBox)
{
TextBox txt = (TextBox)control;
txt.Text =
"UPDATE HERE"
;
}
}
base
.DataBind();
}
public
Control FindControlByID(ControlCollection controls,
string
id)
{
Control found =
null
;
foreach
(Control control
in
controls)
{
if
(control.HasControls())
{
found = FindControlByID(control.Controls, id);
if
(found !=
null
)
{
break
;
}
}
if
(control.ID == id)
{
found = control;
break
;
}
}
return
found;
}
}
Test.aspx
<%
@
Page
Language
="C#"
MasterPageFile
="~/_masterPages/main.master"
AutoEventWireup
="true"
CodeFile
="Test.aspx.cs"
Inherits
="Test"
Title
="Untitled Page"
%>
<%
@
Register
TagPrefix
="uc"
TagName
="Widget"
Src
="~/_controls/Widget.ascx"
%>
<
asp
:
Content
ID
="Content1"
ContentPlaceHolderID
="MainBody"
Runat
="Server">
<
asp
:
TextBox
ID
="TextBox1"
runat
="server"
/>
<
uc
:
Widget
ID
="Widget1"
runat
="server"
ControlToUpdate
="TextBox1"
/>
</
asp
:
Content
>
Test.aspx.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
Test : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender,
EventArgs
e)
{
Widget1.DataBind();
}
}
Enjoy!
Wednesday, August 09, 2006 @ 03:13 PM (-07:00) Pacific Daylight Time
Comments (1)
tags:
Programming
admin: Edit | Remove
Wednesday, April 09, 2008 1:58:56 AM (Pacific Standard Time, UTC-08:00)
Beautiful, clear and crisp example thanks!
Tick
Name
E-mail
Home page
Remember Me
Comment (Some html is allowed:
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u
) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.
Enter the code shown (prevents robots):
Live Comment Preview
Site Navigation
About Me
Calendar
Disclaimer
Site Search
Sponsored Links
Calendar
<
November 2008
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
Monthly Archives
August, 2008 (1)
July, 2008 (1)
March, 2008 (2)
January, 2008 (3)
December, 2007 (3)
October, 2007 (1)
September, 2007 (1)
August, 2007 (5)
July, 2007 (5)
June, 2007 (3)
April, 2007 (5)
February, 2007 (2)
January, 2007 (2)
December, 2006 (9)
November, 2006 (15)
October, 2006 (19)
September, 2006 (3)
August, 2006 (5)
July, 2006 (4)
June, 2006 (6)
May, 2006 (12)
April, 2006 (20)
March, 2006 (11)
February, 2006 (14)
January, 2006 (14)
December, 2005 (23)
November, 2005 (23)
October, 2005 (42)
September, 2005 (4)
Categories
Alert (14)
Community Server (2)
Daily Quote (1)
Did you know (7)
Dumb Searches (1)
Dumbass (15)
General (133)
Haiku (6)
Holiday (5)
Movie quote (8)
Paladin (4)
Paris (1)
Parody (1)
Photo (24)
Political (2)
Priceless (7)
Programming (35)
Pytheus (2)
Rant (23)
Screen capture (15)
SQL (3)
Technical (39)
Video (4)
Vista (8)
Visual Studio 2005 (2)
Wifey (33)
XP (1)
Feeds
RSS
Atom
Good Reading
Code Better
Coding Horror
Computer Zen
Daily WTF
Days Bush Has Left
Dooce
Fargg
Me-Nikk
Post Secret
Rory Blythe
TechCrunch
Sign In