Home All Groups Group Topic Archive Search About

Query regarding control names on an asp.net page

Author
25 Aug 2006 7:03 AM
jaffar.kazi
Hi,
What is the exact name to use to get a control reference on an asp.net
page using FindControl?
My experience is the following:
1. Page.FindControl does not work; it always returns null, even if I
give the exact control name I'm using, eg. txtFirstName.
However, this.FindControl works.

2. There is a page that includes 2 user controls. I'm using
AddressControl.ascx and have included it twice on the page as
AddressControl1.ascx and AddressControl2.ascx.
Now, there will be 2 instances of txtFirstName, but they will be unique
somehow. If we look into the code of the page, this is what we see:
        <td>
            <input name="AddressControl1$txtFirstName" type="text"
maxlength="20" id="AddressControl1_txtFirstName" />
            <span id="AddressControl1_lblFirstName" style="color:Red;"></span>
        </td>

and

        <td>
            <input name="AddressControl2$txtFirstName" type="text"
maxlength="20" id="AddressControl2_txtFirstName" />
            <span id="AddressControl2_lblFirstName" style="color:Red;"></span>
        </td>

So we see that the UserControl name is prefixed to the 2 controls to
make them unique. The interesting thing is that there are 2 things, a
name, which is AddressControl2$txtFirstName and and ID, which is
AddressControl2_txtFirstName

Which do we search for, the name or the ID?
I've given the name to search for, i.e., AddressControl2$txtFirstName,
and it found my control, but it couldn't find it by the ID.

The final question here is the syntax, i.e., (UserControlVarName $
UserControlFieldName). How can we ensure that this is not going to
change in the future with a newer version of ASP.NET? Or is there any
other way to do this?

Environment: I'm using .NET 2.0 on Windows XP.

Thanks in Advance,
--Jaffar

Author
25 Aug 2006 4:47 PM
Tim_Mac
hi jaffar,
i believe the Id is the correct one to use, rather than the name, are you
sure you didn't forget the underscore character?

if you are calling FindControl from within the user control, you can just
use this.FindControl("txtName") and you don't need to worry about the
renamed client ID of the text box.

did you try using an approach like this from your page code-behind:

    this.UserControl1.FindControl("txtName");

this is probably safer than relying on the naming convention you have
correctly identified of UserControlId_ControlId

tim



--------------------------
blog: http://tim.mackey.ie
<jaffar.k***@gmail.com> wrote in message
Show quote
news:1156489419.542076.12630@i42g2000cwa.googlegroups.com...
> Hi,
> What is the exact name to use to get a control reference on an asp.net
> page using FindControl?
> My experience is the following:
> 1. Page.FindControl does not work; it always returns null, even if I
> give the exact control name I'm using, eg. txtFirstName.
> However, this.FindControl works.
>
> 2. There is a page that includes 2 user controls. I'm using
> AddressControl.ascx and have included it twice on the page as
> AddressControl1.ascx and AddressControl2.ascx.
> Now, there will be 2 instances of txtFirstName, but they will be unique
> somehow. If we look into the code of the page, this is what we see:
> <td>
> <input name="AddressControl1$txtFirstName" type="text"
> maxlength="20" id="AddressControl1_txtFirstName" />
> <span id="AddressControl1_lblFirstName" style="color:Red;"></span>
> </td>
>
> and
>
> <td>
> <input name="AddressControl2$txtFirstName" type="text"
> maxlength="20" id="AddressControl2_txtFirstName" />
> <span id="AddressControl2_lblFirstName" style="color:Red;"></span>
> </td>
>
> So we see that the UserControl name is prefixed to the 2 controls to
> make them unique. The interesting thing is that there are 2 things, a
> name, which is AddressControl2$txtFirstName and and ID, which is
> AddressControl2_txtFirstName
>
> Which do we search for, the name or the ID?
> I've given the name to search for, i.e., AddressControl2$txtFirstName,
> and it found my control, but it couldn't find it by the ID.
>
> The final question here is the syntax, i.e., (UserControlVarName $
> UserControlFieldName). How can we ensure that this is not going to
> change in the future with a newer version of ASP.NET? Or is there any
> other way to do this?
>
> Environment: I'm using .NET 2.0 on Windows XP.
>
> Thanks in Advance,
> --Jaffar
>
Author
28 Aug 2006 12:01 PM
jaffar.kazi
Hi Tim.
Yes,
I came to the same conclusion of using:
this.UserControl1.FindControl("txtName");

instead of going into the area of "guessing" the final object name/ID.

The problem was that I was calling it from the main page, not from the
user-control, and I was trying to fix this. Did this by first finding
the control, then finding the child controls inside the user control,
exactly how you're suggesting.

Thanks for this.
Regards,
--Jaffar

Tim_Mac wrote:
Show quote
> hi jaffar,
> i believe the Id is the correct one to use, rather than the name, are you
> sure you didn't forget the underscore character?
>
> if you are calling FindControl from within the user control, you can just
> use this.FindControl("txtName") and you don't need to worry about the
> renamed client ID of the text box.
>
> did you try using an approach like this from your page code-behind:
>
>     this.UserControl1.FindControl("txtName");
>
> this is probably safer than relying on the naming convention you have
> correctly identified of UserControlId_ControlId
>
> tim
>
>
>
> --------------------------
> blog: http://tim.mackey.ie
> <jaffar.k***@gmail.com> wrote in message
> news:1156489419.542076.12630@i42g2000cwa.googlegroups.com...
> > Hi,
> > What is the exact name to use to get a control reference on an asp.net
> > page using FindControl?
> > My experience is the following:
> > 1. Page.FindControl does not work; it always returns null, even if I
> > give the exact control name I'm using, eg. txtFirstName.
> > However, this.FindControl works.
> >
> > 2. There is a page that includes 2 user controls. I'm using
> > AddressControl.ascx and have included it twice on the page as
> > AddressControl1.ascx and AddressControl2.ascx.
> > Now, there will be 2 instances of txtFirstName, but they will be unique
> > somehow. If we look into the code of the page, this is what we see:
> > <td>
> > <input name="AddressControl1$txtFirstName" type="text"
> > maxlength="20" id="AddressControl1_txtFirstName" />
> > <span id="AddressControl1_lblFirstName" style="color:Red;"></span>
> > </td>
> >
> > and
> >
> > <td>
> > <input name="AddressControl2$txtFirstName" type="text"
> > maxlength="20" id="AddressControl2_txtFirstName" />
> > <span id="AddressControl2_lblFirstName" style="color:Red;"></span>
> > </td>
> >
> > So we see that the UserControl name is prefixed to the 2 controls to
> > make them unique. The interesting thing is that there are 2 things, a
> > name, which is AddressControl2$txtFirstName and and ID, which is
> > AddressControl2_txtFirstName
> >
> > Which do we search for, the name or the ID?
> > I've given the name to search for, i.e., AddressControl2$txtFirstName,
> > and it found my control, but it couldn't find it by the ID.
> >
> > The final question here is the syntax, i.e., (UserControlVarName $
> > UserControlFieldName). How can we ensure that this is not going to
> > change in the future with a newer version of ASP.NET? Or is there any
> > other way to do this?
> >
> > Environment: I'm using .NET 2.0 on Windows XP.
> >
> > Thanks in Advance,
> > --Jaffar
> >

AddThis Social Bookmark Button