Solved

help with dynamic creation of Javascript

Posted on 2013-11-22
6
283 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

828 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