Solved

help with dynamic creation of Javascript

Posted on 2013-11-22
6
273 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
Comment Utility
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
Comment Utility
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
Comment Utility
Try using RegisterClientScript instead of RegisterClientScriptBlock
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:HLRosenberger
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

763 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

6 Experts available now in Live!

Get 1:1 Help Now