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.

有帮助吗?

解决方案

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');");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top