ASP.NET Tutorial/Custom Controls/TextBox

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Smart box

<%@ Register TagPrefix="sp" TagName="SmartBox"
    Src="Control.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Creating ASP.NET Web Server Controls</title>
    <script runat="server" language="C#">
      protected void Page_Load()
      {
        nameSb.LabelText = "Name:";
        addressSb.LabelText = "Address:";
        countrySb.LabelText = "Country:";
        phoneSb.LabelText = "Phone:";
      }
    </script>
  </head>
  <body>
    <form id="Form1" runat="server">
      <sp:SmartBox id="nameSb" runat="server" LabelText="Name:" />
      <sp:SmartBox id="addressSb" runat="server"
          LabelText="Address:" />
      <sp:SmartBox id="countrySb" runat="server"
          LabelText="Country:" />
      <sp:SmartBox id="phoneSb" runat="server"
          LabelText="Phone:" />
    </form>
  </body>
</html>
File: Control.ascx
<script runat="server" language="C#">
  public string LabelText 
  {
    set
    {
      myLabel.Text = value;
    }
  }
  public string Text
  {
    get
    {
      return myTextBox.Text;
    }
  }
</script>
  <asp:Label ID="myLabel" runat="server" Text="" Width="100" />
  <asp:TextBox ID="myTextBox" runat="server" Text="" Width="200"
      MaxLength="20" />


Smart box (VB)

<%@ Register TagPrefix="sp" TagName="SmartBox"
    Src="Control.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Creating ASP.NET Web Server Controls</title>
    <script runat="server" language="VB">
      Protected Sub Page_Load()
        nameSb.LabelText = "Name:"
        addressSb.LabelText = "Address:"
        countrySb.LabelText = "Country:"
        phoneSb.LabelText = "Phone:"
      End Sub
    </script>
  </head>
  <body>
    <form id="Form1" runat="server">
      <sp:SmartBox id="nameSb" runat="server" LabelText="Name:" />
      <sp:SmartBox id="addressSb" runat="server"
          LabelText="Address:" />
      <sp:SmartBox id="countrySb" runat="server"
          LabelText="Country:" />
      <sp:SmartBox id="phoneSb" runat="server"
          LabelText="Phone:" />
    </form>
  </body>
</html>
File: Control.ascx
<script runat="server" language="VB">
  Public WriteOnly Property LabelText() As String
    Set(ByVal value As String)
      myLabel.Text = value
    End Set
  End Property
  
  Public ReadOnly Property Text() As String
    Get
      Text = myTextBox.Text
    End Get
  End Property
</script>
  <asp:Label ID="myLabel" runat="server" Text="" Width="100" />
  <asp:TextBox ID="myTextBox" runat="server" Text="" Width="200"
      MaxLength="20" />