Trying to find ClientID of web form

N

Neo Geshel

I need to get the ClientID of a form field for some JavaScript. About
25+ web sites I visited recommend the following method for finding the
client ID of a web form:

Web Form -> <asp:TextBox ID="datefield" type="hidden" Runat="Server" />
Client ID -> <%= datefield.ClientID() %>

Unfortunately, this only provides the following error:

Compilation Error
Compiler Error Message: BC30451: Name 'datefield' is not declared.
Source Error:
Line 200:
Line 201:
Line 202:<%= datefield.ClientID() %>
Line 203:
Line 204:

Why does this throw an error on my page? IF this is wrong, why did the
other 25+ MVP's (who recommended this method) also get it wrong?

As a hint, here are a few URL's where it gives the exact method that
fails for me:
http://www.syncfusion.com/FAQ/aspnet/WEB_c5c.aspx (#28.10, link doesn't
work)
http://www.startvbdotnet.com/aspsite/controls/default.aspx
http://www.thecodeproject.com/aspnet/resource_files_in_asp_net.asp
http://youngpup.net/2004/distro (second scrollbox)
http://www.ondotnet.com/pub/a/dotnet/2003/09/15/aspnet.html?page=last&x-order=date

And perhaps the clearest example that an author claims works, but
doesn't work with me:
http://www.janetsystems.co.uk/Default.aspx?tabid=72&itemid=137

Also, how does one make an <asp:TextBox /> invisible on the web page?
Using a Type="hidden" doesn't seem to do the trick (it is still visible).

TIA
....Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 
J

Juan T. Llibre

First, a comment :

None of the sites you provided links for are run by MVPs.
Please don't imply that 25+ MVPs "got it wrong".

Second, I ran the exact code segment you provided

<form runat="server">
<asp:TextBox ID="datefield" type="hidden" Runat="Server" /><br/>
<%= datefield.ClientID() %>
</form>

and it runs fine inline.
The clientID for the "datefield" textbox is returned as "datefield".

re:
Also, how does one make an <asp:TextBox /> invisible on the web page?
Using a Type="hidden" doesn't seem to do the trick (it is still visible).

You're mixing HTML syntax with ASP.NET controls syntax.

If you don't want the textbox visible, use :
<asp:TextBox ID="datefield" Runat="Server" Visible="false" />
 
N

Neo Geshel

Juan said:
First, a comment :

None of the sites you provided links for are run by MVPs.
Please don't imply that 25+ MVPs "got it wrong".

Okay, so I exaggerate a little.
Second, I ran the exact code segment you provided

<form runat="server">
<asp:TextBox ID="datefield" type="hidden" Runat="Server" /><br/>
<%= datefield.ClientID() %>
</form>

and it runs fine inline.
The clientID for the "datefield" textbox is returned as "datefield".

Unfortunately, it DOES NOT work for me.

Strangely enough, "dg.clientID" *does* work, but that's because "dg" is
the ID of the DataGrid, and "dg" (which is returned) is the full
clientID that one can see in the Table that is sent to the client.

However, [controlname].ClientID *DOES NOT WORK* for anything other than
the DataGrind ID. It throws an error that crashes the page.
re:



You're mixing HTML syntax with ASP.NET controls syntax.

If you don't want the textbox visible, use :
<asp:TextBox ID="datefield" Runat="Server" Visible="false" />

Ah. Thanks. That works, but not as required. I think I'll apply a style.



--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top