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
Solved

Printing Client Script Value

Posted on 2006-10-30
4
243 Views
Last Modified: 2010-08-05
Experts, I need to print a client script value from a web control, but when printing the value the string seems to wrap to a new line thus triggering a JavaScript unterminated string constant error.

        protected override void Render(HtmlTextWriter writer)
        {
            writer.write("var MyVar = '" + this.ClientScriptValue + "';");
        }

Results in :

var MyVar = "<p>asdf</p>
<p>&nbsp;</p>";

Any ideas?

Thanks for the tips!

~ C
0
Comment
Question by:clickclickbang
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
nauman_ahmed earned 500 total points
ID: 17835191
Any specific reason for using HtmlTextWriter? An easier way would be to declare a literal control in ASPX
set its value to JS from code-behind

<asp:Literal id="lblJs" runat="server" />

from code-behind:

string strScript = "";
strScript = "<script langugage=\"javsacript\">var MyVar='"+this.ClientScriptValue+"';</script>"
lblJs.Value = strScript;

--Nauman.
0
 
LVL 1

Author Comment

by:clickclickbang
ID: 17835285
Nauman, this is actually a a web control not an aspx page.

public class MyCustomWebControl : WebControl
{

}

I could use a composite Literal inside the control, but wouldn't it present the same line wrapping issue as before?
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17835445
HtmlTextWriter causes the above output since it behaves differently when you try to write something to the output. Try using composite Literal control and see if it solves the issue.

--Nauman.
0
 
LVL 1

Author Comment

by:clickclickbang
ID: 17835956
I ran into the same problem with the Literal control. It seems to add line breaks in the string. The string source is a txt file.

Here is a sample from the txt file:

<p>This is a test</p>
<p>Hey how are you?</p>

If I manually remove the line break:

<p>This is a test</p><p>Hey how are you?</p>

It works okay. Is there a way to do this programatically?

Thanks for your help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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