8 HTML 服务器

HTML 服务器控件主要是保证服务端运行的增强型标准 HTML 控件。HTML 控件不是由服务器处理,而是被发送到浏览器进行显示,比如页面标题标签,链接标签及输入元素。

通过添加 runat = "server" 属性和一个 id 属性,它们可被特定地转化为一个服务器控件,应用于服务器端处理。

例如,HTML 输入控件:

  1. <input type="text" size="40">

它可以通过添加 runat 和 id 属性被转换成一个服务器控件:

  1. <input type="text" id="testtext" size="40" runat="server">

使用 HTML 服务器控件的优点

尽管 ASP.NET 服务器控件可以完成 HTML 服务器控件执行的每一项工作,HTML 控件在以下情况仍然具有优势:

  • 使用静态表达到布局目的。
  • 转换一个 HTML 页面到 ASP.NET 下运行。

下面这个表格介绍了 HTML 服务器控件:

控件名称 HTML 标签
HtmlHead <head>element
HtmlInputButton <input type=button|submit|reset>
HtmlInputCheckbox <input type=checkbox>
HtmlInputFile <input type = file>
HtmlInputHidden <input type = hidden>
HtmlInputImage <input type = image>
HtmlInputPassword <input type = password>
HtmlInputRadioButton <input type = radio>
HtmlInputReset <input type = reset>
HtmlText <input type = text|password>
HtmlImage <img> element
HtmlLink <link> element
HtmlAnchor <a> element
HtmlButton <button> element
HtmlButton <button> element
HtmlForm <form> element
HtmlTable <table> element
HtmlTableCell <td> and <th>
HtmlTableRow <tr> element
HtmlTitle <title> element
HtmlSelect <select&t; element
HtmlGenericControl 未列出的所有 HTML 控件

实例

以下实例使用了基本的 HTML 表格进行布局。它使用了用于从用户获得输入诸如姓名,地址,城市,州等的框,还有一个按钮控件,该控件被点击后能够获取该表最后一行中显示的用户数据。

页面在设计视图中应如下所示:

image 图片 8.1 image

内容页面的代码表明了 HTML 表格元素进行布局的应用。

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="htmlserver._Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6.  
  7. <head runat="server">
  8. <title>Untitled Page</title>
  9.  
  10. <style type="text/css">
  11. .style1
  12. {
  13. width: 156px;
  14. }
  15. .style2
  16. {
  17. width: 332px;
  18. }
  19. </style>
  20.  
  21. </head>
  22.  
  23. <body>
  24. <form id="form1" runat="server">
  25. <div>
  26. <table style="width: 54%;">
  27. <tr>
  28. <td class="style1">Name:</td>
  29. <td class="style2">
  30. <asp:TextBox ID="txtname" runat="server" style="width:230px">
  31. </asp:TextBox>
  32. </td>
  33. </tr>
  34.  
  35. <tr>
  36. <td class="style1">Street</td>
  37. <td class="style2">
  38. <asp:TextBox ID="txtstreet" runat="server" style="width:230px">
  39. </asp:TextBox>
  40. </td>
  41. </tr>
  42.  
  43. <tr>
  44. <td class="style1">City</td>
  45. <td class="style2">
  46. <asp:TextBox ID="txtcity" runat="server" style="width:230px">
  47. </asp:TextBox>
  48. </td>
  49. </tr>
  50.  
  51. <tr>
  52. <td class="style1">State</td>
  53. <td class="style2">
  54. <asp:TextBox ID="txtstate" runat="server" style="width:230px">
  55. </asp:TextBox>
  56. </td>
  57. </tr>
  58.  
  59. <tr>
  60. <td class="style1"> </td>
  61. <td class="style2"></td>
  62. </tr>
  63.  
  64. <tr>
  65. <td class="style1"></td>
  66. <td ID="displayrow" runat ="server" class="style2">
  67. </td>
  68. </tr>
  69. </table>
  70.  
  71. </div>
  72. <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Click" />
  73. </form>
  74. </body>
  75. </html>

按钮控件的后台代码为:

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. string str = "";
  4. str += txtname.Text + "<br />";
  5. str += txtstreet.Text + "<br />";
  6. str += txtcity.Text + "<br />";
  7. str += txtstate.Text + "<br />";
  8. displayrow.InnerHtml = str;
  9. }

观察以下陈述:

  • 标准 HTML 标签已被使用进行页面布局。
  • HTML 表格的最后一行用于数据显示。它需要服务器端进行加工,因此为其添加 ID 属性和 runat 属性。