Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

help with dynamic creation of Javascript

Posted on 2013-11-22
6
Medium Priority
?
303 Views
Last Modified: 2013-11-22
I'm creating Javascript dynamically, using the ClientScriptManager.  In general, my Javascript works fine.   However, I want to use document.getElementById to get a reference to a control on my page.  This does not work.  I'm using alert to display the variable and it's always null.

Can anyone see an issue with the code below?  What might be wrong?

 Here's a relevant portion of my ASP code:

 <asp:TextBox runat="server" id="xyzreq_item" MaxLength="100" columns="60" cssclass="field_input" rows="2" textmode="MultiLine"></asp:TextBox>


Here's a relevant portion of my VB code-behind code:

Dim scriptText As New StringBuilder()
Dim cs As ClientScriptManager = thePage.ClientScript

scriptText.Append(" var x123 = document.getElementById('<%= xyzreq_item.ClientID %>'); ")
scriptText.Append("alert(x123);")

cs.RegisterClientScriptBlock(thePage.GetType, SCRIPTID, scriptText.ToString)
0
Comment
Question by:HLRosenberger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 2000 total points
ID: 39669436
Just reference it directly. Should work as long as you want to put the value in the alert.

scriptText.Append(" var x123 = document.getElementById('xyzreq_item').value; ")

Open in new window

0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 39669472
That did not work either.  When the javascript runs, x123 is still NULL.   I don't get it.  

scriptText.Append(" var x123 = document.getElementById('xyzreq_item'); ")
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39669494
Try using RegisterClientScript instead of RegisterClientScriptBlock
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 1

Author Comment

by:HLRosenberger
ID: 39669501
Ah, if I add this to the asp:   ClientIDMode="Static"

<asp:TextBox runat="server" id="xyzreq_item" MaxLength="100" columns="60" cssclass="field_input" rows="2" ClientIDMode="Static" textmode="MultiLine"></asp:TextBox>

Then your way works.  Does ClientIDMode="Static" tell the browser or ASP to not make any changes to the ID?

What's the difference between this syntax:

var x123 = document.getElementById('<%= xyzreq_item.ClientID %>');

and this:

var x123 = document.getElementById('xyzreq_item');
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39669553
If memory serves me the default ClientID mode is Predictable. Setting it as static essentially tells the page not to change the control names from what you specify. By default the control names, I think, also include the container name. I'm a bit vague on it I'm afraid.
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 39669709
Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question