ASP.NET and JavaScript

L

Lit

Hi,

Can anyone direct me to a hyperActive JavaScript NewsGroup(s).

I am feeling stupid today.

if I have the ClientID then why do I need the
document.getElementById(<myClientID>)??

Can't I just use it?

is it expensive to use document.getElementById

Is there an inexpensive way of getting a reference to my ClientID to begin
with.

I have many Controls in my ASPX form and to validate all I have to deal with
lots of JavaScript.

Thank you,

Lit
 
E

Eliyahu Goldin

In many scenarios you don't know what the exact ClientID will be. For
examples, for controls in GridView, Repeater or DataList items, in user
controls. In these cases you need to pass the ClientID from server to
client.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
M

Mark Rae [MVP]

if I have the ClientID then why do I need the
document.getElementById(<myClientID>)??

Because the ClientID can change for any number of reasons...
Can't I just use it?

Hard-coding a ClientID is just as bad as hard-coding any other variable...
is it expensive to use document.getElementById
No...

Is there an inexpensive way of getting a reference to my ClientID to
begin with.

I have many Controls in my ASPX form and to validate all I have to deal
with lots of JavaScript.

I usually do validation in blocks, creating a client-side variable at the
beginning of the block e.g.

<script type="text/javascript">
<!--
function validateForm()
{
var myTextBox1 =
document.getElementById('<%=MyTextBox1.ClientID%>');
if (myTextBox1.value.length == 0)
{
alert('MyTextBox1 cannot be blank');
myTextBox1.focus();
return false;
}
if (myTextBox1.value.indexOf('A') == 0)
{
alert('MyTextBox1 must contain the letter A');
myTextBox1.focus();
return false;
}

var myTextBox2 =
document.getElementById('<%=MyTextBox2.ClientID%>');
//etc
}
-->
</script>

<asp:Button ID="cmdSave" runat="server" Text="Save" OnClick="cmdSave_Click"
OnClientClick="return validateForm();" />
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Lit said:
Hi,

Can anyone direct me to a hyperActive JavaScript NewsGroup(s).

I am feeling stupid today.

if I have the ClientID then why do I need the
document.getElementById(<myClientID>)??

The ClientID is just a string. To get a reference to the element, you
need to use the getElementById method.
Can't I just use it?

Some browsers let you access the element by using the id as a variable,
but this is not standard behaviour so it's not recommended.
is it expensive to use document.getElementById

Is there an inexpensive way of getting a reference to my ClientID to begin
with.

As far as I know, there is no less expensive way than to use getElementById.
I have many Controls in my ASPX form and to validate all I have to deal with
lots of JavaScript.

A pretty neat shortcut for document.getElementById is to define a
function named $:

function $(id) { return document.getElementById(id); }

Now you can use $('TheId') instead of document.getElementById('TheId').
 
J

Joey

In many scenarios you don't know what the exact ClientID will be. For
examples, for controls in GridView, Repeater or DataList items, in user
controls. In these cases you need to pass the ClientID from server to
client.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net




Can anyone direct me to a hyperActive JavaScript NewsGroup(s).
I am feeling stupid today.
if I have the ClientID then why do I need the
document.getElementById(<myClientID>)??
Can't I just use it?
is it expensive to use document.getElementById
Is there an inexpensive way of getting a reference to my ClientID to
begin with.
I have many Controls in my ASPX form and to validate all I have to deal
with lots of JavaScript.
Thank you,
Lit- Hide quoted text -

- Show quoted text -

Prime example:

We upgraded a .net 1.1 app that had a .js script file containing lots
of javascript functions that referred to items by their hardcoded
client IDs. For asp.net, a textbox named 'txtMyText' in server side
code would have a client ID of 'txtMyText' in the browser. Then we
upgraded to .net 2.0 and used master pages. Suddenly all of these
items had different client IDs. For example, 'txtMyText' in server
side code became 'ctl00_conContainer1_txtMyText'. Most all of
javascript was instantly broken. If only we had used code to pull the
client ID and insert that into the javascript. That's kind of what
we're doing now. We dynamically create and register all the javascript
(inquiring about Client ID) when the pages load.
 
L

Lit

Goran,

You understood and answered all my questions ( but not this one: Any
HyperActive JavaScript Newgroups?)

Thanks to all others replies also,

Lit
 

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

Top