views:

166

answers:

2

Hi,

I have a rather pesky problem when trying to add a confirm popup message to the onclick event of a button.

The problem occurs when trying to break the message onto a new line so the test wraps within the pop up window. If the \n is taken out of the string the code works fine.

string confirmationMessage = "text text text text text text text text text text text text text text text text text text \n text text text text ";

submitButton.Attributes.Add("onclick", "return confirm('" + confirmationMessage + "');");

Any help is apprciated thank you.

Not sure whether to post a separate question for this. I have a new problem but there seems to be a limit to to amount of text i can include. If the string is too long the confirmation message fails to display.

+3  A: 

I think your problem is about escape character, try this :

string confirmationMessage = "text text text text text text text text text text text text text text text text text text \\n text text text text ";

// or : 
// string confirmationMessage = @"text text text text text text text text text text text text text text text text text text \n text text text text ";

submitButton.Attributes.Add("onclick", "return confirm('" + confirmationMessage + "');");
Canavar
the double escape didnt work in this case but the second ption with the @ symbol worked perfect.
not sure whether to post a separate quetion for this. I have a new problem but there seems to be a limit to to amount of text i can include. If the string is too long the confirmation message fails to display.
How long is your confirmation ? I put 2500 characters, it works fine. I think you have some invalid characters in your confirmation text. can you post in your code ?
Canavar
+1  A: 

The compiler is probably evaluating the escape sequence instead of treating it as plain text. Add an "@" in front of the string and see if that works:

string confirmationMessage = @"text text text text text text text text text text text text text text text text text text \n text text text text ";
Rich
thanks that worked when using the @ symbol. Could you tell me what exactly the @ symbol does to the variable?
without the @ symbol, escape sequences like \n and \t are interpreted. To "escape and escape sequence" in a C# string, you either double up the escape character (ie. "\\n") or add the "@" at the beginning to tell the compiler not to evaluate ANY escape sequences.
Rich