Solved

Printing Client Script Value

Posted on 2006-10-30
4
240 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

11 Experts available now in Live!

Get 1:1 Help Now