Solved

Printing Client Script Value

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

16 Experts available now in Live!

Get 1:1 Help Now