如何在asp.net的文本框中提交HTML标签?

首先,我想让每个人都知道我使用的是aspx引擎而不是Razor引擎。

我在表格中有一张桌子。 我的一个文本框包含html标签


Phone:
814-888-9999
Email:
aaa@gmail.com.

当我去构建它时它会给我一个错误

从客户端检测到潜在危险的Request.Form值(QuestionAnswer="...ics Phone:
814-888-9999<br...")

我尝试了validation请求=“false”但它没有用。

对不起,我没有添加我的HTML代码供你查看到目前为止。 如果需要的话,我正在向我提出一些可以编辑的问题。

  <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>  EditFreqQuestionsUser    $(document).ready(function () { $("#freqQuestionsUserUpdateButton").click(function () { $("#updateFreqQuestionsUser").submit(); }); });  

Edit Freq Questions User

<%List UserRoleList = Session["UserRoles"] != null ? (List)Session["UserRoles"] : new List(); %> <form id="updateFreqQuestionsUser" action="" method="post" onsubmit+>
Freq Questions User Details <input type ="hidden" value="" name="freqQuestionsUserId"/>
Question Description: <input type="text" maxlength="2000" name="QuestionDescription" value=" " />
QuestionAnswer: <input type="text" maxlength="2000" name="QuestionAnswer" value="" />

Save Cancel

在页面提交之前,您需要使用window.escape(…)对文本框的值进行html编码

如果您需要服务器端的未转义文本,请使用HttpUtility.UrlDecode(...)方法。

非常快的样本:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SO.WebForm1" %>        

对代码进行以下更改:

  

使用[ValidateInput]属性装饰控制器操作:

 [ValidateInput(false)] [HttpPost] public ActionResult Foo(MyViewModel model) { ... } 

客户端JavaScript:

 function codificarTags() { document.getElementById('txtDescripcion').value = document.getElementById('txtDescripcion').value.replace(//g,'>'); }; 

服务器:

 protected void Page_Load(object sender, EventArgs e) { txtDescripcion.Text = txtDescripcion.Text.Replace(@"<", @"<").Replace(@">", @">"); 

在文本框中使用html不是一个好习惯,可能使用换行符( Environment.NewLine )或\ r \ n而不是br ? .NET参考

示例(在C#中):

 textBox1.Multiline = true; textBox1.Text = "test" + Environment.NewLine + "test2"; 

我建议使用AjaxControlToolkit的HTML编辑器。 我现在正在实施。 如果你的文本框是多行的,并且足够大以容纳HTML,那么为什么不把它提升到HTML编辑器。 您的用户也会更快乐。

http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/HTMLEditor/HTMLEditor.aspx