How to Html.Encode in webforms

If you are on ASP.NET 4 or newer, you can use this syntax:

<%: Model.Username %>

Which will HTML-encode the expression. Scott Gu explains the benefit of this syntax:

We chose the <%: %> syntax so that it would be easy to quickly replace existing instances of <%= %> code blocks. It also enables you to easily search your code-base for <%= %> elements to find and verify any cases where you are not using HTML encoding within your application to ensure that you have the correct behavior.


You can use HttpUtility.HtmlEncode