TextBox.TagKey 属性

定义

获取文本框控件的 HTML 标记。 此属性是受保护的。

protected:
 virtual property System::Web::UI::HtmlTextWriterTag TagKey { System::Web::UI::HtmlTextWriterTag get(); };
protected override System.Web.UI.HtmlTextWriterTag TagKey { get; }
member this.TagKey : System.Web.UI.HtmlTextWriterTag
Protected Overrides ReadOnly Property TagKey As HtmlTextWriterTag

属性值

HtmlTextWriterTag

如果该文本框是多行的,则为 Textarea;否则为 Input

示例

下面的代码示例演示如何使用 TagKey 属性创建自定义文本框。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Custom TextBox - TagKey - C# Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom TextBox - TagKey - C# Example</h3>
            
            <aspSample:CustomTextBoxTagKey 
              id="TextBox1" 
              runat="server">Hello World!
            </aspSample:CustomTextBoxTagKey>
            
        </form>
    </body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Custom TextBox - TagKey - VB.NET Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom TextBox - TagKey - VB.NET Example</h3>
            
            <aspSample:CustomTextBoxTagKey 
              id="TextBox1" 
              runat="server">Hello World!
          </aspSample:CustomTextBoxTagKey>
          
        </form>
    </body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
  public sealed class CustomTextBoxTagKey : System.Web.UI.WebControls.TextBox
  {
    protected override System.Web.UI.HtmlTextWriterTag TagKey
    {
      get 
      {
        // If the TextMode is MultiLine, return a Textarea tag, 
        // else return an Input tag.
        if (this.TextMode == System.Web.UI.WebControls.TextBoxMode.MultiLine)
        {
          return System.Web.UI.HtmlTextWriterTag.Textarea;
        }
        else
        {
          return System.Web.UI.HtmlTextWriterTag.Input;
        }
      }
    }
  }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomTextBoxTagKey
        Inherits System.Web.UI.WebControls.TextBox

        Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
            Get
                ' If the TextMode is MultiLine, return a Textarea tag, else return an Input tag.
                If Me.TextMode = System.Web.UI.WebControls.TextBoxMode.MultiLine Then
                    Return System.Web.UI.HtmlTextWriterTag.Textarea
                Else
                    Return System.Web.UI.HtmlTextWriterTag.Input
                End If
            End Get
        End Property
    End Class
End Namespace

适用于