Asp.net 大写字母的用户控件

Asp.net 大写字母的用户控件,asp.net,user-controls,Asp.net,User Controls,我想创建一个用户控件[textbox],它转换所有大写文本。 在普通网页中,我可以通过 <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox> <style type="text/css"> #TextBox1 { text-transform: uppercase; } </style> 我看到的更简单的方

我想创建一个用户控件[textbox],它转换所有大写文本。 在普通网页中,我可以通过

<asp:TextBox ID="TextBox1" runat="server">
    </asp:TextBox>

    <style type="text/css">
    #TextBox1
       { 
          text-transform: uppercase; 
       } 
</style>

我看到的更简单的方法是,您可以创建一个从TextBox继承的自定义控件,并重写Text属性,以便将文本更改为大写

Public Class UppertextBox
        Inherits Web.UI.WebControls.TextBox

        Public Overrides Property Text As String
            Get
                Return MyBase.Text.ToUpper
            End Get
            Set(ByVal value As String)
                MyBase.Text = value.ToUpper ' no check for null, add it if necessary
            End Set
        End Property

    End Class
大写字母可能取决于控件的属性,以允许您使用它自定义页面中的文本框

Public Class UppertextBox
        Inherits Web.UI.WebControls.TextBox

        Public Property Uppercase As Boolean

        Public Overrides Property Text As String
            Get
                If Uppercase AndAlso Not String.IsNullOrEmpty(MyBase.Text) Then
                    Return MyBase.Text.ToUpper
                Else
                    Return MyBase.Text
                End If
            End Get
            Set(ByVal value As String)
                If Uppercase AndAlso Not String.IsNullOrEmpty(value) Then
                    MyBase.Text = value.ToUpper
                Else
                    MyBase.Text = value
                End If

            End Set
        End Property

    End Class
您继承的控件也可以将其样式设置为大写,但如果您不想在每次使用时设置样式,我建议您使用主题

编辑

C版本看起来像

public class UppercaseTextBox : System.Web.UI.WebControls.TextBox
    {
        public Boolean Uppercase { get; set; }
        public override string Text
        {
            get
            {
                return Uppercase && !string.IsNullOrEmpty(base.Text) ? base.Text.ToUpper() : base.Text;
            }
            set
            {
                base.Text = Uppercase && !string.IsNullOrEmpty(value) ? value.ToUpper() : value;
            }
        }
    }
使用样式表

输入[类型=文本] {

文本转换:大写; }


它在所有文本框中的简易效果

首先,您可以使用
web用户控件
只包含一个
TextBox
Boolean
名为
大写的属性

代码隐藏

public Boolean UpperCase
{
    get;
    set;
}
.aspx


使用您在问题中提供的代码片段创建用户控件,或者使用
javascript
keypress
event@Chris:我在答案中加了一个C版本。基本上,除了extern关键字外,您发布的内容看起来不错。除非您使用的是不同版本的.net(我用3.5对其进行了测试),否则我发布的代码会正常工作,在您删除extern:)后,您的代码也会正常工作,但这将在服务器端而不是客户端进行转换side@samy请稍等。我正在落实你给我的建议。如果我遇到任何问题,我会在5分钟内给你回复。我希望你能帮助我。@Miroprocessor是吗?:-(我希望它位于客户端。@Miroprocessor@Chris:text transform样式将更改文本在客户端上的显示,控件将更改它在服务器上的显示。如果您只使用text transform,则值在服务器上仍以其原始大小写传输,即使它在客户端上显示大写。或者,y您只能使用javascript更改客户端上的文本,但客户端上任何禁用的javascript都会破坏uppercasing@samy我无法将其转换为c#。我使用的是我想要单独的控件,我不能让我的所有文本框都是大写。这是正确答案。用户此代码将创建自定义控件。很好。它可以工作,但是,您将丢失文本框中的事件,并且可能必须将它们包装在自定义控件中以使其冒泡+1我可以从中创建控件吗?因为我希望它位于工具箱中!:-(很抱歉打扰您。@samy您谈论了哪些事件?@Chris您可以从
解决方案资源管理器
拖动
web用户控件
,然后将其放入页面的设计部分
public Boolean UpperCase
{
    get;
    set;
}
<%if(UpperCase) {%>
<style type="text/css">
#TextBox1
   { 
      text-transform: uppercase; 
   } 
</style>
<%} %>
<uc1:TextControl ID="TextControl1" runat="server" UpperCase="false"/>
  <%if(UpperCase) {%>
<style type="text/css">
#<%=TextBox1.ClientID%>
   { 
      text-transform: uppercase; 
   } 
</style>
<%} %>