Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Query regarding control names on an asp.net page

Reply
Thread Tools

Query regarding control names on an asp.net page

 
 
jaffar.kazi@gmail.com
Guest
Posts: n/a
 
      08-25-2006
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

 
Reply With Quote
 
 
 
 
Tim_Mac
Guest
Posts: n/a
 
      08-25-2006
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
<> wrote in message
news: 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
>



 
Reply With Quote
 
 
 
 
jaffar.kazi@gmail.com
Guest
Posts: n/a
 
      08-28-2006
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
> <> wrote in message
> news: 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
> >


 
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
Query regarding master page archana ASP .Net 1 02-01-2010 09:57 AM
Converting 'flat' gate level names to hierarchical names Paddy McCarthy VHDL 3 09-24-2004 05:34 PM
table field names vs. display names Bob ASP .Net 1 07-30-2004 05:06 PM
WSDL- Mapping Application Defined Names to XML Names Craig XML 0 02-09-2004 04:14 PM
XSL rules applying to XSD (XML schema) defined type names (as opposed to node names) Lewis G. Pringle, Jr. XML 0 09-30-2003 10:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57