Solved

Printing Client Script Value

Posted on 2006-10-30
4
242 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

786 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