1

I write program with aspx and C# in VS2010. I want to alert all the SoftWareNotes in loop. (it's a varchar value in my DB). so I did that:

<% for(Software s in Db.Softwares){ %>
<script>
            var GetTextArea= '<%=s.SoftwareNotes%>' ;
            alert(GetTextArea);
</script>
<%}%>

and it's work fine , until that SoftwareNotes contain /r . for example in this string: "fdbdfb\r\ndfbdf\r\ndfb" (from the debugger) , it's not print nothing, and continue to the next one.

Why and how can I fix that?

*The field SoftwareNotes contain /r cause it's populate by TextAreas ..so if someone make enter it save it like a /r.


edit 2:20: I found one solution , but it's not the best.. I write this code above the code before:

 <%
                String tryRemoveNL="";
                if(s.SoftwareNotes!=null)
                    tryRemoveNL= Regex.Replace(s.SoftwareNotes, @"\r\n?|\n", " ");
                 %>

and now when I write:

var GetTextArea= '<%=tryRemoveNL%>' ;

it works. but I want to keep the newLines.. maybe you have other solutions?

2
  • Which browser is this for? Running your example alert("fdbdfb\r\ndfbdf\r\ndfb") in Chrome and IE 10 shows the alert with line breaks, no problem. Commented Mar 10, 2014 at 0:10
  • Google Chrome , but tried in IE too. maybe the problem from casting this variable from C# to js? (but without the /r that work fine) Commented Mar 10, 2014 at 0:15

1 Answer 1

1

It's possible to do it using the following:

var GetTextArea= '<%=s.SoftwareNotes.Replace(Environment.NewLine, "\\r\\n"))%>' ;
alert(GetTextArea);

This keeps the line breaks within the alert message.

The issue was because the literal linebreaks were coming through into the javascript, creating invalid syntax, similar to:

var GetTextArea = 'fdbdfb
dfbdf
dfb'; //Invalid syntax for multiline javascript string

So instead, this solution will write out:

var GetTextArea = 'fdbdfb\r\ndfbdf\r\ndfb'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.