I have some code behind that I am trying to call javascript with on the client side. Everything seems to work fine except when I try and introduce a \n into the messagebox text. I have tried single and double quotes in the script line, but if I put anything
in there to do with \n \r environmene.newline or anything it does not fire. I can however place random text and it does work.
protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
Page.Validate("TransValidation");
if (Page.IsValid)
{
stamp_PDF(true);
create_PDF();
}
else
{
string msg = "";
foreach (IValidator aValidator in this.Validators)
{
if (!aValidator.IsValid)
{
msg += aValidator.ErrorMessage + "\n"; //Adding new line for each validator?
}
}
msgBox_error(msg);
}
}
public void msgBox_error(String message)
{
Label msgBox = new Label();
String msg = message;
msgBox.Text = "<" + "script language='javascript'>" + Environment.NewLine + "window.alert(\"" + msg + "\") </" + "script>";
Page.Controls.Add(msgBox);
}
countycowpok...
Participant
1411 Points
340 Posts
Calling a script from code behind not working..
Jun 20, 2012 05:00 PM|LINK
I have some code behind that I am trying to call javascript with on the client side. Everything seems to work fine except when I try and introduce a \n into the messagebox text. I have tried single and double quotes in the script line, but if I put anything in there to do with \n \r environmene.newline or anything it does not fire. I can however place random text and it does work.
protected void btnGeneratePDF_Click(object sender, EventArgs e) { Page.Validate("TransValidation"); if (Page.IsValid) { stamp_PDF(true); create_PDF(); } else { string msg = ""; foreach (IValidator aValidator in this.Validators) { if (!aValidator.IsValid) { msg += aValidator.ErrorMessage + "\n"; //Adding new line for each validator? } } msgBox_error(msg); } } public void msgBox_error(String message) { Label msgBox = new Label(); String msg = message; msgBox.Text = "<" + "script language='javascript'>" + Environment.NewLine + "window.alert(\"" + msg + "\") </" + "script>"; Page.Controls.Add(msgBox); }Thoughts?
bbcompent1
All-Star
32984 Points
8505 Posts
Moderator
Re: Calling a script from code behind not working..
Jun 20, 2012 05:35 PM|LINK
When we do a \n or \r, we need to escape it in C# so do that like this:
\n = \\n
\r = \\r
countycowpok...
Participant
1411 Points
340 Posts
Re: Calling a script from code behind not working..
Jun 20, 2012 06:23 PM|LINK
Thank you, I had thought the \ was the escape, but i guess that is part of the character.
bbcompent1
All-Star
32984 Points
8505 Posts
Moderator
Re: Calling a script from code behind not working..
Jun 21, 2012 01:38 PM|LINK
Correct :)