473,474 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with Response.Write and getElementById

Hi, my problem is that I can't get getElementById to find the ID of any
controls on my page. To whit:

The following code-behind returns the error, "lblErrorText is
undefined.":

Response.Write("<script language=javascript>var errorText =
document.GetElementById(lblErrorText).innerHTML; alert('SQL error: ' +
errorText)</script>")

With 'lblErrorText' I get: "Object doesn't support this property or
method."

With "lblErrorText" I get: "Comma, ), or valid expression continuation
expected." from the compiler.
Does this have something to do with the object not existing during a
postback? I've tried the line in Page_Load() and in lblErrorText_Load()
to no avail. What am I missing?

Thanks - Paul

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
8 5689
With ASP.NET, control names are often decorated if they are part of the
page. To guarantee that you are emitting the ID that the client side code
can access, you can use the ClientID property, as such:

Response.Write("<script type='text/javascript'>var errorText =
document.getElementByID(" + lblErrorText.ClientID + "...");

Also keep in mind that javascript is case sensitive, so you need to properly
capitalize the names of your functions.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"Paul" <no*****************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi, my problem is that I can't get getElementById to find the ID of any
controls on my page. To whit:

The following code-behind returns the error, "lblErrorText is
undefined.":

Response.Write("<script language=javascript>var errorText =
document.GetElementById(lblErrorText).innerHTML; alert('SQL error: ' +
errorText)</script>")

With 'lblErrorText' I get: "Object doesn't support this property or
method."

With "lblErrorText" I get: "Comma, ), or valid expression continuation
expected." from the compiler.
Does this have something to do with the object not existing during a
postback? I've tried the line in Page_Load() and in lblErrorText_Load()
to no avail. What am I missing?

Thanks - Paul

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #2
Thanks, Chris. Unfortunately it didn't help. I get the same
"undefined" error using the ClientID property:
Response.Write("<script language=javascript>var errorText =
document.GetElementById(" + lblErrorText.ClientID + ").innerHTML;
This is what the server returns to the browser, so I know the ID must be
correct, including case:

<span id="lblErrorText"></span>
Also, the same error happens with **any** control on the page, so it's
more generic than I thought. Thanks again for any clues for this
problem that is driving me slightly batty. ~Paul

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
Paul,
If your control ID is lblErrorText,try this
document.getElementById('lblErrorText').innerHTML

Hope this will solve your problem.
Regards,
Marshal Antony
http://dotnetmarshal.com

"Paul" <no*****************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi, my problem is that I can't get getElementById to find the ID of any
controls on my page. To whit:

The following code-behind returns the error, "lblErrorText is
undefined.":

Response.Write("<script language=javascript>var errorText =
document.GetElementById(lblErrorText).innerHTML; alert('SQL error: ' +
errorText)</script>")

With 'lblErrorText' I get: "Object doesn't support this property or
method."

With "lblErrorText" I get: "Comma, ), or valid expression continuation
expected." from the compiler.
Does this have something to do with the object not existing during a
postback? I've tried the line in Page_Load() and in lblErrorText_Load()
to no avail. What am I missing?

Thanks - Paul

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #4
Thanks Marshall, but I already tried that. I tried several ways:

With 'lblErrorText' I get: "Object doesn't support this property
ormethod."

With "lblErrorText" I get: "Comma, ), or valid expression
continuationexpected." from the compiler.

I get the "undefined" error when I used no quotes, or " + lblErrorText +
"

I think the "undefined" error is correct - that the object isn't on the
page when the script runs and tries to find it. If this is true I have
no clue how to fix that, and I've been searching for hours to give me
clues.

Any ideas? ~Paul


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #5
Paul,
I think I know what your problem is :
Try this code in your Page_Load:

LiteralControl li=new LiteralControl();

li.Text=("<script language=javascript>var errorText =
document.getElementById('" + lblErrorText.ClientID +
"').innerHTML;alert(errorText);</script>");

Page.Controls.Add(li);

The problem you have is when you use Response.Write,the
javascript is put in the beginning of the page(even before HTML tag) and

it is trying to find out the control with id,lblErrorText which is under
the form tag RUNAT="Server".JavaScript will not allow you to do that way.

With the above solution the javascript is put at the end of the page and
the problem is solved.

To learn more about literalcontrol here is the link :
http://msdn.microsoft.com/library/de...classtopic.asp

Hope this helps.

Regards,

Marshal Antony

http://dotnetmarshal.com




"Paul" <no*****************@msn.com> wrote in message
news:uo**************@TK2MSFTNGP10.phx.gbl...
Thanks Marshall, but I already tried that. I tried several ways:

With 'lblErrorText' I get: "Object doesn't support this property
ormethod."

With "lblErrorText" I get: "Comma, ), or valid expression
continuationexpected." from the compiler.

I get the "undefined" error when I used no quotes, or " + lblErrorText +
"

I think the "undefined" error is correct - that the object isn't on the
page when the script runs and tries to find it. If this is true I have
no clue how to fix that, and I've been searching for hours to give me
clues.

Any ideas? ~Paul


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #6
I don't think it's the manner in which it is being written that is
inherently problematic, but more importantly that script will execute as
soon as it is read - which could well be before the page is done rendering.
Set that script up in a function, and call that function in response to the
onload event of the page body - that is the only way to be sure that the
object has been created and, in fact, exists and can be found.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"Marshal Antony" <do***********@yahoo.com> wrote in message
news:OS**************@TK2MSFTNGP12.phx.gbl...
Paul,
I think I know what your problem is :
Try this code in your Page_Load:

LiteralControl li=new LiteralControl();

li.Text=("<script language=javascript>var errorText =
document.getElementById('" + lblErrorText.ClientID +
"').innerHTML;alert(errorText);</script>");

Page.Controls.Add(li);

The problem you have is when you use Response.Write,the
javascript is put in the beginning of the page(even before HTML tag) and

it is trying to find out the control with id,lblErrorText which is under
the form tag RUNAT="Server".JavaScript will not allow you to do that way.

With the above solution the javascript is put at the end of the page and
the problem is solved.

To learn more about literalcontrol here is the link :
http://msdn.microsoft.com/library/de...classtopic.asp

Hope this helps.

Regards,

Marshal Antony

http://dotnetmarshal.com




"Paul" <no*****************@msn.com> wrote in message
news:uo**************@TK2MSFTNGP10.phx.gbl...
> Thanks Marshall, but I already tried that. I tried several ways:
>
> With 'lblErrorText' I get: "Object doesn't support this property
> ormethod."
>
> With "lblErrorText" I get: "Comma, ), or valid expression
> continuationexpected." from the compiler.
>
> I get the "undefined" error when I used no quotes, or " + lblErrorText

+
> "
>
> I think the "undefined" error is correct - that the object isn't on the
> page when the script runs and tries to find it. If this is true I have
> no clue how to fix that, and I've been searching for hours to give me
> clues.
>
> Any ideas? ~Paul
>
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!


Nov 18 '05 #7

Marshal,
Bingo ..... the literal control with the technique to insert the script
at end of page worked like a charm. Thanks so much, I was too much of a
newbie to figure that one out on my own. ~Paul


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #8

Thanks, Chris, I am spitting out the script at the end of the page by
creating a literal control at Page_Load time and assigning the script to
it. Then the getElementById finds its mark. Thanks again. ~Paul

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #9

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

Similar topics

1
by: Tim_Mac | last post by:
hi, i'm guessing you just can't write to the page after Response.End(), but i would be interested to hear if anyone has a work around for my situation. i have designed a 'SmartButton' control...
5
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button....
1
by: zwieback89 | last post by:
Hi, I am still not able to get this working in a simple page. Please help me. I am badly stuck.... I am trying to view the contents of the Beverage Category. I have built it like a hierarchy...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
1
by: colleen1980 | last post by:
Hi: I am trying to pull all the values from the listbox. But the ASP code shows only the last record. Needs help HTML <html> <head>
0
by: JenniferG | last post by:
I've worked with ASP.NET but don't know much about ASP at all. I have the following code. I would like to get a person' birthday and phone number text box populated when the person's name is selected...
5
by: loveshack | last post by:
Can anyone help me please (i am quite a novice, but having fun learning). Im not sure if this is an ASP problem, a javascript problem or a browser problem. Firstly, everything i have written...
2
by: registry | last post by:
<%@Language="VBSCRIPT" CODEPAGE="1252"%> <html> <head> <title>Registry Network Hospital Detail</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script...
0
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.