Help with Response.Write and getElementById

P

Paul

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
 
C

Chris Jackson

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.
 
P

Paul

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
 
P

Paul

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
 
M

Marshal Antony

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/d.../frlrfsystemwebuiliteralcontrolclasstopic.asp

Hope this helps.

Regards,

Marshal Antony

http://dotnetmarshal.com
 
C

Chris Jackson

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.
 
P

Paul

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
 
P

Paul

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
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top