如何创建派生的 TextBox 控件?

发布于 2024-12-14 02:55:12 字数 1237 浏览 1 评论 0 原文

我需要创建一个允许用户输入 HTML 标记的自定义 TextBox 控件。我添加了一个名为 HtmlEnabled 的新属性,默认值为 false。如果为 false,则其行为与原始 TextBox 完全相同;如果设置为 true,则会调用 Server.HtmlEncode 对文本进行编码。我从不创建自定义控件,谁能告诉我我需要做什么?我需要重写什么函数?谢谢。

我创建了我的 TextBoxEx 类,如下所示: 当我将 HtmlEnabled 设置为 true 时,我仍然收到验证错误,有人能告诉我出了什么问题吗?

namespace WebApplication1
{
    [ToolboxData("<{0}:TextBoxEx runat=server></{0}:TextBoxEx")]
    public class TextBoxEx : System.Web.UI.WebControls.TextBox
    {
        public bool HtmlEnabled
        {
            get
            {
                return (bool)ViewState["HtmlEnabled"];
            }
            set
            {
                ViewState["HtmlEnabled"] = value;
            }
        }

        public TextBoxEx()
        {
            ViewState["HtmlEnabled"] = false;
        }

        public override string Text
        {
            get
            {
                if (HtmlEnabled)
                    return HttpUtility.HtmlEncode(base.Text);
                else return base.Text;
            }

            set
            {
                if (HtmlEnabled)
                    base.Text = HttpUtility.HtmlDecode(value);
                else base.Text = value;
            }
        }


    }
}

I need to create a custom TextBox control that allows user input HTML tags. I added a new property called HtmlEnabled, default is false. If it is false, it will act exactly like the original TextBox; if it is set to true, it will call Server.HtmlEncode to encode the text. I never creat a custom control, can anyone tell me what do I need to do? What function I need to override? Thanks.

I created my TextBoxEx class as following: I still get the validation error when I set HtmlEnabled to true, can anybody tell me what is wrong?

namespace WebApplication1
{
    [ToolboxData("<{0}:TextBoxEx runat=server></{0}:TextBoxEx")]
    public class TextBoxEx : System.Web.UI.WebControls.TextBox
    {
        public bool HtmlEnabled
        {
            get
            {
                return (bool)ViewState["HtmlEnabled"];
            }
            set
            {
                ViewState["HtmlEnabled"] = value;
            }
        }

        public TextBoxEx()
        {
            ViewState["HtmlEnabled"] = false;
        }

        public override string Text
        {
            get
            {
                if (HtmlEnabled)
                    return HttpUtility.HtmlEncode(base.Text);
                else return base.Text;
            }

            set
            {
                if (HtmlEnabled)
                    base.Text = HttpUtility.HtmlDecode(value);
                else base.Text = value;
            }
        }


    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪落纷纷 2024-12-21 02:55:12

听起来您可以从 TextBox 控件继承并覆盖 Text 属性。本文应该帮助您开始了解如何执行此操作:

https://web.archive.org/web/20211020203142/https://www.4guysfromrolla.com/articles/100103-1.aspx

Sounds like you could just inherit from the TextBox control and override the Text property. This article should get you started on how to go about doing it:

https://web.archive.org/web/20211020203142/https://www.4guysfromrolla.com/articles/100103-1.aspx

月亮是我掰弯的 2024-12-21 02:55:12

为了允许页面接受 HTML 标签,您需要禁用请求验证。

<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="MyNamespace.TestPage" %>

这与文本框控件无关,请求验证会检查所有页面输入(查询字符串参数、cookie、标头和表单字段),以确保请求中不存在潜在的恶意脚本。请注意,关闭它后,您需要验证输入对您自己没有危害。

In order to allow the page to accept HTML tags, you need to disable request validation.

<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="MyNamespace.TestPage" %>

This has nothing to do with the textbox control, the request validation checks all page input (query string parameters, cookies, headers, and form fields) to ensure that there are no potentially malicious scripts in the request. Be aware that by turning it off, you will need to validate that the input isn't harmful yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文