Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

Finding Controls in User Control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fred Chateau - 03 Aug 2007 18:00 GMT
I am trying to reference a server control in a user control, from the
containing page. Is there a way to do that? I have tried
"UserControl1.FindControl("ControlName")" but I get a null reference.

Signature

Regards,

Fred Chateau
fchateauAtComcastDotNet

Fred Chateau - 03 Aug 2007 18:42 GMT
I should mention, in case it makes a difference, that I'm loading the user
control dynamically in Page_Init.

Signature

Regards,

Fred Chateau
fchateauAtComcastDotNet

>I am trying to reference a server control in a user control, from the
>containing page. Is there a way to do that? I have tried
>"UserControl1.FindControl("ControlName")" but I get a null reference.
tomisarobot@gmail.com - 03 Aug 2007 19:27 GMT
UserControl1.FindControl() will search in your user control, try
searching in the page object?
Milosz Skalecki [MCAD] - 04 Aug 2007 00:28 GMT
Hi Fred,

This is not proper way of solving the problem. It may not work because if
the control is nested within a container control, you have to call
FindControl recursively or use "$" separator when passing the control id to
FindControl method. There's another, much more effective and cleaner way of
doing this type of task, by exposing a property from the user control, which
then can be set or read by the containing page. Let's imagine your user
control has got a text box for extering a user's first name. Containing page
does not know the internal structure of the user control (well it shouldn't
know), therefore it's better to create a property to abstract the First Name:

-- user control --

public string FirstName
{
  get
 {
    return txtFirstName.Text;
 }
 set
 {
    txtFirstName.Text = value;
 }
}

now, on the containing page set / get the value of the property,
1. declaratively in the aspx code
<uc1:MyCustomControl runat="server" id="myControl" FirstName="Fred"/>
2. or programatically in the code behind:

protected void btn_Click(object sender, EventArgs e)
{
  SaveUserDataToDataBase(myControl.FirstName);
}

Hope this helps
Signature

Milosz

> I should mention, in case it makes a difference, that I'm loading the user
> control dynamically in Page_Init.
>
> >I am trying to reference a server control in a user control, from the
> >containing page. Is there a way to do that? I have tried
> >"UserControl1.FindControl("ControlName")" but I get a null reference.
Fred Chateau - 04 Aug 2007 03:04 GMT
I still have the same problem. When I load the User Control dynamically, I
can't seem to get a reference to it.

Signature

Regards,

Fred Chateau
fchateauAtComcastDotNet

> Hi Fred,
>
[quoted text clipped - 47 lines]
>> >containing page. Is there a way to do that? I have tried
>> >"UserControl1.FindControl("ControlName")" but I get a null reference.
Fred Chateau - 04 Aug 2007 03:34 GMT
I figured it out.

PlaceholderControl.Controls[0].FindControl("ControlName")

Signature

Regards,

Fred Chateau
fchateauAtComcastDotNet

>I still have the same problem. When I load the User Control dynamically, I
>can't seem to get a reference to it.
[quoted text clipped - 50 lines]
>>> >containing page. Is there a way to do that? I have tried
>>> >"UserControl1.FindControl("ControlName")" but I get a null reference.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.