ASP.NET Tutorial/HTML Controls/HtmlControl

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

A survey form (VB)

   <source lang="csharp">

<!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 & "
" feedbackLabel.Text += "Your email is: " & email.Value & _ "
" feedbackLabel.Text += "You like to work with:
" For i = 0 To serverModel.Items.Count - 1 If serverModel.Items(i).Selected Then feedbackLabel.Text += " - " & _ serverModel.Items(i).Text & "
" End If Next i feedbackLabel.Text += "You like .NET: " & likeDotNet.Value End Sub </script> </head> <body> <form runat="server">

Take the Survey!

       Name:
<input type="text" id="name" runat="server" /> Email:
<input type="text" id="email" runat="server" /> Which server technologies do you use?
<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?
<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></source>

Get action sender

   <source lang="csharp">

<%@ 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">
   First Counter:
   <asp:Button
       id="Button1"
       Text="0"
       OnClick="Button_Click"
       Runat="server" />
   

Second Counter: <asp:Button id="Button2" Text="0" OnClick="Button_Click" Runat="server" />
   </form>

</body> </html></source>


Get event properties

   <source lang="csharp">

<%@ 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">
   <asp:ImageButton
       id="myButton"
       ImageUrl="http://www.nfex.ru/style/logo.png"
       Runat="server" OnClick="myButton_Click" />
   
X Coordinate: <asp:Label id="lblX" Runat="server" />
Y Coordinate: <asp:Label id="lblY" Runat="server" />
   </form>

</body> </html></source>


Handling Control Events

   <source lang="csharp">

<%@ 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">
   <asp:Button
       id="btnSubmit"
       Text="Click Here"
       OnClick="btnSubmit_Click"
       Runat="server" />
   

<asp:Label id="Label1" Runat="server" />
   </form>

</body> </html></source>


HtmlControl Properties

   <source lang="csharp">

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.</source>


Understanding HTML Controls

   <source lang="csharp">

<%@ 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">
   At the tone, the time will be:
   <span id="spanNow" runat="server" />
   </form>

</body> </html></source>