ASP.NET Tutorial/HTML Controls/HtmlControl

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

A survey form (VB)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Using ASP.NET HTML Server Controls</title>
    <script runat="server" language="VB">
      Sub Click(ByVal s As Object, ByVal e As EventArgs)
        Dim i As Integer
        feedbackLabel.Text = "Your name is: " & name.Value & "<br />"
        feedbackLabel.Text += "Your email is: " & email.Value & _
            "<br />"
        feedbackLabel.Text += "You like to work with:<br />"
        For i = 0 To serverModel.Items.Count - 1
          If serverModel.Items(i).Selected Then
            feedbackLabel.Text += " - " & _
                serverModel.Items(i).Text & "<br />"
          End If
        Next i
        feedbackLabel.Text += "You like .NET: " & likeDotNet.Value
      End Sub
    </script>
  </head>
  <body>
    <form runat="server">
      <h2>Take the Survey!</h2>
        Name:<br />
        <input type="text" id="name" runat="server" />
        Email:<br />
        <input type="text" id="email" runat="server" />
        Which server technologies do you use?<br />
        <select id="serverModel" runat="server" multiple="true">
          <option>ASP.NET</option>
          <option>PHP</option>
          <option>JSP</option>
          <option>CGI</option>
          <option>ColdFusion</option>
        </select>
        Do you like .NET so far?<br />
        <select id="likeDotNet" runat="server">
          <option>Yes</option>
          <option>No</option>
        </select>
        <button id="confirmButton" OnServerClick="Click"
            runat="server">Confirm</button>
        <asp:Label id="feedbackLabel" runat="server" />
    </form>
  </body>


Get action sender

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Button_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        btn.Text = (Int32.Parse(btn.Text) + 1).ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Button Counters</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    First Counter:
    <asp:Button
        id="Button1"
        Text="0"
        OnClick="Button_Click"
        Runat="server" />
    <br /><br />
    Second Counter:
    <asp:Button
        id="Button2"
        Text="0"
        OnClick="Button_Click"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Get event properties

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void myButton_Click(object sender, ImageClickEventArgs e)
    {
        lblX.Text = e.X.ToString();
        lblY.Text = e.Y.ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show EventArgs</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ImageButton
        id="myButton"
        ImageUrl="http://www.nfex.ru/style/logo.png"
        Runat="server" OnClick="myButton_Click" />
    <br />
    X Coordinate:
    <asp:Label
        id="lblX"
        Runat="server" />
    <br />
    Y Coordinate:
    <asp:Label
        id="lblY"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Handling Control Events

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Label1.Text = "nfex!";
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Button Click</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button
        id="btnSubmit"
        Text="Click Here"
        OnClick="btnSubmit_Click"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="Label1"
        Runat="server" />
    </div>
    </form>
</body>
</html>


HtmlControl Properties

Property         Description
Attributes       Provides a collection of all the attributes. 
                   
Controls         Provides a collection of all the controls contained inside the current control.
Disabled         Disables the control when set to true.
EnableViewState  Disables the automatic state management when set to false. 
Page             Provides a reference to the web page that contains this control as a System.Web.UI.Page object.
Parent           Provides a reference to the control that contains this control. 
                 If the control is placed directly on the page ), it will return a reference to the page object.
Style            Provides a collection of CSS style properties that can be used to format the control.
TagName          Indicates the name of the underlying HTML element (for example, img or div).
Visible          Hides the control when set to false and will not be rendered to the final HTML page that is sent to the client.


Understanding HTML Controls

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Load()
    {
        spanNow.InnerText = DateTime.Now.ToString("T");
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HTML Controls</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    At the tone, the time will be:
    <span id="spanNow" runat="server" />
    </div>
    </form>
</body>
</html>