Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Finding Controls in User Control

Reply
Thread Tools

Finding Controls in User Control

 
 
Fred Chateau
Guest
Posts: n/a
 
      08-03-2007
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


 
Reply With Quote
 
 
 
 
Fred Chateau
Guest
Posts: n/a
 
      08-03-2007
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" <fchateau@127.0.0.1> wrote in message
news:%23MY8Q$(E-Mail Removed)...
>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
>
>



 
Reply With Quote
 
 
 
 
tomisarobot@gmail.com
Guest
Posts: n/a
 
      08-03-2007
UserControl1.FindControl() will search in your user control, try
searching in the page object?

 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      08-03-2007
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" <fchateau@127.0.0.1> wrote in message
> news:%23MY8Q$(E-Mail Removed)...
> >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
> >
> >

>
>
>

 
Reply With Quote
 
Fred Chateau
Guest
Posts: n/a
 
      08-04-2007
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <fchateau@127.0.0.1> wrote in message
>> news:%23MY8Q$(E-Mail Removed)...
>> >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
>> >
>> >

>>
>>
>>



 
Reply With Quote
 
Fred Chateau
Guest
Posts: n/a
 
      08-04-2007
I figured it out.

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

--
Regards,

Fred Chateau
fchateauAtComcastDotNet


"Fred Chateau" <fchateau@127.0.0.1> wrote in message
news:(E-Mail Removed)...
>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]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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" <fchateau@127.0.0.1> wrote in message
>>> news:%23MY8Q$(E-Mail Removed)...
>>> >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
>>> >
>>> >
>>>
>>>
>>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Control to Control Other User Control Jonathan Wood ASP .Net 4 02-02-2010 03:41 PM
persisting changes to a control outside a user control from the user control? Mad Scientist Jr ASP .Net 0 03-22-2006 08:02 AM
Change web controls in one user control from another user control =?Utf-8?B?REo=?= ASP .Net 2 12-01-2005 08:26 AM
using javascript in User controls to access server controls of the user control Faizan Ahmed ASP .Net Building Controls 1 01-04-2005 04:09 PM
using javascript in User controls to access server controls of the user control Faizan Ahmed ASP .Net Web Controls 1 01-04-2005 04:09 PM



Advertisments