473,499 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query regarding control names on an asp.net page

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

Aug 25 '06 #1
2 1235
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
<ja*********@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.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

Aug 25 '06 #2
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:
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
<ja*********@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.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
Aug 28 '06 #3

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

Similar topics

5
4359
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm...
0
1439
by: raf_z | last post by:
Hi, I think i'm doing something simple, although i may be wrong. I was able to load a Crystal report file up until today, and even now, its only 1 report that's misbehaving. I'll confess that i...
3
7641
by: Jim Lewis | last post by:
I have read several things that state accessing a Web Service through a Query String should work. However, when I try to execute http://localhost/webservice1/service1.asmx/HelloWorld I get the...
3
3273
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when...
4
1507
by: SandyIsCool | last post by:
Hi, I am newbie to asp.net. I have small doubt regarding page life cycle. MSDN documentaion says that init event does blhah, blah load event does blah blah etc. But Id ont see any init or load...
7
2863
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
10
1486
by: Rob | last post by:
I am reading a book that says that the "name" property can be altered only at design time and cannot be modified at runtime. Please explain this given the code below... If you click Button3......
6
6820
by: Phil Stanton | last post by:
I am running a query that calls a function used to format addresses depending on the width of a control on a report that shows that address. The same query is used as the RecordSource of lots of...
9
3608
by: Sinner | last post by:
Hi, I have a field name 'USER' in tableMAIN. How do I replace the user names with corresponding user names. I can do that in xl using vlookup but now I'm trying to find a way to do that in...
0
7130
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
7171
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
7220
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...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
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...
1
4918
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...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.