468,544 Members | 1,728 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,544 developers. It's quick & easy.

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 5374
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tim_Mac | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.