如何让文本框接受html标签
我的 aspx 页面中有一个文本框。用户希望能够在文本框中输入html标签。到目前为止,我知道绕过验证错误的唯一方法是将 ValidateRequest 设置为 false。我尝试了其他几种方法: (1) 在这样的 javascript 中使用 Server.HtmlEncode
<%@ Import Namespace="System.Web" %>
var tb = document.getElementById("<%=synopsisTextBox.ClientID%>");
var value =Server.HtmlEncode(tb.value);
但我遇到了编译器错误。谁能告诉我我做错了什么?
(2)创建我自己的编码函数
function escapeHTML (str)
{
var div = document.createElement('div');
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
}
不知何故它工作得不太好。当我单击某个导致同一页面中回发的按钮时,它会卡住。
谁能告诉我是否有更好的方法让你的文本框接受 html 标签?谢谢。
I have a textbox in my aspx page. The user wants to have the ability to input html tags in the textbox. The only way so far I know to by pass the validation error is set the ValidateRequest to false. I tried couple of other ways:
(1) using Server.HtmlEncode in a javascript like this
<%@ Import Namespace="System.Web" %>
var tb = document.getElementById("<%=synopsisTextBox.ClientID%>");
var value =Server.HtmlEncode(tb.value);
But I got compiler error. Can anyone tell me what I did wrong?
(2) creating my own encode function
function escapeHTML (str)
{
var div = document.createElement('div');
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
}
somehow it didn't work quite well. When I click some button that causes postback in the same page, it gets stuck.
Can anyone tell me if there is a better way to make your textbox accept html tags? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需禁用输入验证(仅适用于页面)并确保对页面中其他文本框的输入进行编码。默认情况下,输入验证处于启用状态,并不是因为它不应该被禁用,而是因为您应该知道自己在做什么并明确执行。这样您就一定会注意并进行自己的输入验证。
Just disable input validation (for the page only) and make sure you encode input from other textboxes in the page. Input validation is on by default not because it shouldn't be disabled ever but because you should know what you are doing and do it explicitly. This way you are sure to pay attention and do your own input validation.
如果您需要对 html 标签进行编码并在浏览器上显示它们,请记住这
会产生以下输出:
Web 浏览器将显示为:
您对 Server.HTMLEncode 的试用无效,因为它有效当数据位于服务器上并且需要在发送到浏览器之前进行编码时。在您的示例中,数据位于浏览器上,请求在服务器接收之前会在验证时被阻止。
如果您希望用户编辑文本框并输入 html 标签,您可以通过
或在整个应用程序的 web.config 中禁用此功能:
另一个智能解决方案是通过用户编写的 JavaScript 文本进行替换,以确保验证安全。
<代码>< tag> ,而不是
被认为是安全的!If you need to encode html tags and show them on a browser remember that
produces the following output:
that will be displayed by a Web browser as:
Your trial with Server.HTMLEncode is not working, since it works when data are on the server and need to be encoded before sending to the browses. In your sample, data are on the browser and request is blocked on validation before being recieved on the server .
If you want user to edit TextBox and enter html tags you can disable this via
or in the web.config for your entire application:
Another smart solution is to replace via javascript text written by user to make it safe for validation.
< tag>
, instead of<tag>
is considered safe!IMO,您有以下解决方案:
IMO, you have these following solutions: