简体   繁体   English

如何创建自定义TextBox控件?

[英]How to create custom TextBox control?

I want to perform Trim() method on each TexBox control on my page, before value is returned. 我想在返回值之前在我的页面上的每个TexBox控件上执行Trim()方法。 I dont' want to hard-code the same code for each TexBox control, I want to do it in more elegant way. 我不想为每个TexBox控件硬编码相同的代码,我想以更优雅的方式做到这一点。

I've found made the following class 我发现做了以下课程

namespace System.Web.UI.WebControls
{
    public partial class TrimmedTextBuox : TextBox
    {
        private string text;
        public override string Text
        {
            get { return string.IsNullOrEmpty(text) ? text : text.Trim(); }
            set { text = value; }
        }     
    }
}

but it fails, while debuggind the compiler doesn't get inside get{} and set{} . 但它失败了,而debuggind编译器没有进入get{}set{}

After that, I created a UserControl item, but it must be deriverd from System.Web.UI.UserControl , not System.Web.UI.WebControls.TextBox to get it work (there's an exception which points to that) 在那之后,我创建了一个UserControl的项目,但必须从deriverd System.Web.UI.UserControl ,不System.Web.UI.WebControls.TextBox得到它的工作(有它指向一个例外)

So, how can I do that ? 那么,我该怎么做呢?

First you have to register your control in your .aspx page like that: 首先,您必须在.aspx页面中注册您的控件,如下所示:

<%@ Register TagPrefix="customControls" Namespace="WebApplication.Custom.Controls" Assembly="WebApplication"%>

Then you can call it using the markup 然后你可以使用标记来调用它

<customControls:TrimmedTextBuox  ID="txtTrim" runat="server"/>

Plus you don't have to create another "text" property in your custom TextBox . 另外,您不必在自定义TextBox创建另一个“text”属性。 Instead, it can be done like that: 相反,它可以这样做:

namespace WebApplication.Custom.Controls
{
    public class TrimmedTextBuox : TextBox
    {
        public override string Text
        {
            get
            {                
                return base.Text;
            }
            set
            {
                if (!String.IsNullOrEmpty(value))
                    base.Text = value.Trim();
            }
        }
    }
}

This will trim recursively all text boxes before inserting. 这将在插入之前递归修剪所有文本框。

 public static void trimRecursive(Control root)
    {
      foreach (Control control in root.Controls)
      {
        if (control is TextBox)
        {
            var textbox = control as TextBox;
            textbox.Text = textbox.Text.Trim();
        }
        else
        {
            trimRecursive(control);
        }
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    trimRecursive(Page);
}

Simple Solution to your problem is to hide the Text property of your base class by using new keyword. 解决问题的简单方法是使用new关键字隐藏基类的Text属性。 sample code... 示例代码......

public class TrimmedTextBox : TextBox
{
    public new string Text
    {
        get
        {
             var t = (string) GetValue(TextProperty);
            return t != null ? t.Trim() : string.Empty;
        }
    }
}

For more info about how new keyword with property works refrer to this SO Question 有关具有属性的新关键字如何工作的更多信息,请参阅此SO 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM