473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Finding Controls in User Control

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.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
Aug 3 '07 #1
5 1533
I should mention, in case it makes a difference, that I'm loading the user
control dynamically in Page_Init.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
"Fred Chateau" <fc******@127.0.0.1wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>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.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet


Aug 3 '07 #2
UserControl1.FindControl() will search in your user control, try
searching in the page object?

Aug 3 '07 #3
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
--
Milosz
"Fred Chateau" wrote:
I should mention, in case it makes a difference, that I'm loading the user
control dynamically in Page_Init.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
"Fred Chateau" <fc******@127.0.0.1wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet


Aug 3 '07 #4
I still have the same problem. When I load the User Control dynamically, I
can't seem to get a reference to it.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:F2**********************************@microsof t.com...
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
--
Milosz
"Fred Chateau" wrote:
>I should mention, in case it makes a difference, that I'm loading the
user
control dynamically in Page_Init.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
"Fred Chateau" <fc******@127.0.0.1wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>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.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet




Aug 4 '07 #5
I figured it out.

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

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
"Fred Chateau" <fc******@127.0.0.1wrote in message
news:OR**************@TK2MSFTNGP03.phx.gbl...
>I still have the same problem. When I load the User Control dynamically, I
can't seem to get a reference to it.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:F2**********************************@microsof t.com...
>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
--
Milosz
"Fred Chateau" wrote:
>>I should mention, in case it makes a difference, that I'm loading the
user
control dynamically in Page_Init.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
"Fred Chateau" <fc******@127.0.0.1wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .
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.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet




Aug 4 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: TB | last post by:
Howdy... I have an asp.net web form with an ImageButton control on the form. When clicked, the ImageButton's click event handler is supposed to pass X and Y coordinates of the mouse click's...
1
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
3
by: Simon Harvey | last post by:
Hi All. In one of my user controls I add a textbox to a placeholder sitting on the user control. txtUsername = new TextBox(); txtUsername.ID = "txtUsername";...
7
by: BradC | last post by:
(VB.NET 2002, Windows app). I'm going to be provided a two-letter string like "BV" or "TP" that represents a location. I then need to perform some actions on several form controls that have...
3
by: Tor Inge Rislaa | last post by:
Finding name and type In the activate procedure of a form I want to write to the debug window, name and type of all controls at that actual form. Is there a smart way to do that? Allso for...
2
by: accyboy1981 | last post by:
Hi, I'm creating an asp text box dynamically within an asp table. I know the id as this is not dynamic (txBxPass). I need to get the text of this text box when an asp button is pressed. I know...
7
by: Brad Baker | last post by:
I am trying to programmatically set a placeholder control in csharp which is nested inside a repeater control between <ItemTemplateand </ItemTemplate> tags, however I am running into problems. I've...
4
by: karthik25 | last post by:
Hi All, I have a problem in finding control in a dynamically created updated panel. I have given the code below. Following is just a starting effort in a completely dynamic user control. I am...
2
by: Hillbilly | last post by:
Anybody have any sage advice on this frustrating "feature" of ASP.NET? I have a TextBox in the EditItemTemplate of a DataList I can't seem to find for some reason that is I believe related to the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.