|
|
|
start date: Fri, 3 Aug 2007 12:00:36 -0500,
posted on: microsoft.public.dotnet.framework.aspnet
back
| Thread Index |
|
1
Fred Chateau
|
|
2
Fred Chateau
|
|
3
Milosz Skalecki [MCAD]
|
|
4
Fred Chateau
|
|
5
Fred Chateau
|
|
6
unknown
|
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
Date:Fri, 3 Aug 2007 12:00:36 -0500
Author:
|
Re: Finding Controls in User Control
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" wrote in message
news:%23MY8Q$e1HHA.5980@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
>
>
Date:Fri, 3 Aug 2007 12:42:26 -0500
Author:
|
Re: Finding Controls in User Control
UserControl1.FindControl() will search in your user control, try
searching in the page object?
Date:Fri, 03 Aug 2007 11:27:30 -0700
Author:
|
Re: Finding Controls in User Control
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" wrote in message
> news:%23MY8Q$e1HHA.5980@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
> >
> >
>
>
>
Date:Fri, 3 Aug 2007 16:28:00 -0700
Author:
|
Re: Finding Controls in User Control
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]" wrote in message
news:F2F06F4C-1ECA-4CD2-A679-BBA964048C66@microsoft.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" wrote in message
>> news:%23MY8Q$e1HHA.5980@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
>> >
>> >
>>
>>
>>
Date:Fri, 3 Aug 2007 21:04:57 -0500
Author:
|
Re: Finding Controls in User Control
I figured it out.
PlaceholderControl.Controls[0].FindControl("ControlName")
--
Regards,
Fred Chateau
fchateauAtComcastDotNet
"Fred Chateau" wrote in message
news:ORfgcvj1HHA.1204@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]" wrote in message
> news:F2F06F4C-1ECA-4CD2-A679-BBA964048C66@microsoft.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" wrote in message
>>> news:%23MY8Q$e1HHA.5980@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
>>> >
>>> >
>>>
>>>
>>>
>
>
Date:Fri, 3 Aug 2007 21:34:14 -0500
Author:
|
|
|