2

I'm using MVC and I've created a HtmlHelper extension function ImageLink. This function uses two TagBuilder objects to build an HTML image link (like <a><img/></a>).

It works fine, but now I have a page on which the action for the image button should first be confirmed. So, I added an onclick attribute with "javascript: return confirm('confirm me');" as the onclick code.

This renders as: onclick="javascript:return confirm('confirm me');"

I though this wouldn't work, but it does. But now when we enter:

"javascript: return confirm('confirm me\r\nnewline');"

this renders as:

onclick="javascript:return confirm('confirm me newline');"

which doesn't work.

I'd prefer for the TagBuilder not to encode anything at all and just let me take care of it. But since it does, I'm now looking for a way to get this working properly.

1 Answer 1

2

Backslash (\) is a special character in C# strings so you just have to escape it as you would normally do in any other situation when you want it to appear in the final string.

tag.MergeAttribute("onclick", "return confirm('first line\\n\\nsecond line');");
Sign up to request clarification or add additional context in comments.

1 Comment

Quoting H. Simpson: DOH! Thanks!

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.