Query regarding control names on an asp.net page

J

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
 
T

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
 
J

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top