Solved

help with dynamic creation of Javascript

Posted on 2013-11-22
6
277 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
  • 3
  • 3
6 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now