HtmlForm.Enctype 屬性

定義

取得或設定將表單資料傳送至伺服器時瀏覽器所使用的編碼類型。

public:
 property System::String ^ Enctype { System::String ^ get(); void set(System::String ^ value); };
public string Enctype { get; set; }
member this.Enctype : string with get, set
Public Property Enctype As String

屬性值

包含編碼類型的字串。 預設值是空字串 (""),表示使用了瀏覽器的預設內容類型。

範例

下列程式碼範例示範如何使用 Enctype 屬性來指定瀏覽器用來將資料傳回伺服器的編碼類型。

<%@ 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">

<script runat="server">

    protected void AddButton_Click(Object sender, EventArgs e)
    {

        int Answer;

        // Calculate and display the result.
        Answer = Convert.ToInt32(Value1.Value) + Convert.ToInt32(Value2.Value);
        AnswerMessage.InnerHtml = Answer.ToString();

    }
     
</script>

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head>
  <title>HtmlForm Example</title>
</head>
<body>

   <form method="Post"
         enctype="application/x-www-form-urlencoded" 
         runat="server" id="myform">

      <h3> HtmlForm Example </h3>

      <table>
         <tr>
            <td colspan="5">
               Enter integer values into the text boxes. <br />
               Click the Add button to add the two values. <br />
               Click the Reset button to reset the text boxes.
            </td>
         </tr>
         <tr>
            <td colspan="5">
                
            </td>
         </tr>
         <tr align="center">
            <td>
               <input id="Value1"
                      type="Text"
                      size="2"
                      maxlength="3"
                      value="1"
                      runat="server"/>
            </td>
            <td>
               + 
            </td>
            <td>
               <input id="Value2"
                      type="Text"
                      size="2"
                      maxlength="3"
                      value="1"
                      runat="server"/>
            </td>
            <td>
               =
            </td>
            <td>
               <span id="AnswerMessage"
                     runat="server"/>
            </td>
         </tr>
         <tr>
            <td colspan="2">
               <asp:RequiredFieldValidator
                    ID="Value1RequiredValidator"
                    ControlToValidate="Value1"
                    ErrorMessage="Please enter a value.<br />"
                    Display="Dynamic"
                    runat="server"/>

               <asp:CompareValidator
                    ID="Value1MinCompareValidator"
                    ControlToValidate="Value1"
                    Operator="LessThan"
                    Type="Integer"
                    ValueToCompare="100"
                    ErrorMessage="Please enter an integer less than 100.<br />"
                    Display="Dynamic"
                    runat="server"/>

               <asp:CompareValidator
                    ID="Value1MaxCompareValidator"
                    ControlToValidate="Value1"
                    Operator="GreaterThan"
                    Type="Integer"
                    ValueToCompare="0"
                    ErrorMessage="Please enter an integer greater than 0.<br />"
                    Display="Dynamic"
                    runat="server"/>
            </td>
            <td colspan="2">
               <asp:RequiredFieldValidator
                    ID="Value2RequiredValidator"
                    ControlToValidate="Value2"
                    ErrorMessage="Please enter a value.<br />"
                    Display="Dynamic"
                    runat="server"/>

               <asp:CompareValidator
                    ID="Value2MinCompareValidator"
                    ControlToValidate="Value2"
                    Operator="LessThan"
                    Type="Integer"
                    ValueToCompare="100"
                    ErrorMessage="Please enter an integer less than 100.<br />"
                    Display="Dynamic"
                    runat="server"/>

               <asp:CompareValidator
                    ID="Value2MaxCompareValidator"
                    ControlToValidate="Value2"
                    Operator="GreaterThan"
                    Type="Integer"
                    ValueToCompare="0"
                    ErrorMessage="Please enter an integer greater than 0.<br />"
                    Display="Dynamic"
                    runat="server"/>

            </td>
            <td>
                
            </td>
         </tr>
         <tr align="center">
            <td colspan="4">
               <input type="Submit"
                      name="AddButton"
                      value="Add"
                      onserverclick="AddButton_Click"
                      runat="server"/>

                  

               <input type="Reset"
                      name="AddButton"
                      value="Reset"
                      runat="server"/>
            </td>
            <td>
                
            </td>
         </tr>
      </table>

   </form>
</body>
</html>
<%@ 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">

<script runat="server">

  Protected Sub AddButton_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim Answer As Integer
    
    ' Calculate and display the result.
    Answer = Convert.ToInt32(Value1.Value) + Convert.ToInt32(Value2.Value)
    AnswerMessage.InnerHtml = Answer.ToString()

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head>
  <title>HtmlForm Example</title>
</head>
<body>

   <form method="Post"
         enctype="application/x-www-form-urlencoded" 
         runat="server" id="myform">

      <h3> HtmlForm Example </h3>

      <table>
         <tr>
            <td colspan="5">
               Enter integer values into the text boxes. <br />
               Click the Add button to add the two values. <br />
               Click the Reset button to reset the text boxes.
            </td>
         </tr>
         <tr>
            <td colspan="5">
                
            </td>
         </tr>
         <tr align="center">
            <td>
               <input id="Value1"
                      type="Text"
                      size="2"
                      maxlength="3"
                      value="1"
                      runat="server"/>
            </td>
            <td>
               + 
            </td>
            <td>
               <input id="Value2"
                      type="Text"
                      size="2"
                      maxlength="3"
                      value="1"
                      runat="server"/>
            </td>
            <td>
               =
            </td>
            <td>
               <span id="AnswerMessage"
                     runat="server"/>
            </td>
         </tr>
         <tr>
            <td colspan="2">
               <asp:RequiredFieldValidator
                    ID="Value1RequiredValidator"
                    ControlToValidate="Value1"
                    ErrorMessage="Please enter a value.<br />"
                    Display="Dynamic"
                    runat="server"/>

               <asp:CompareValidator
                    ID="Value1MinCompareValidator"
                    ControlToValidate="Value1"
                    Operator="LessThan"
                    Type="Integer"
                    ValueToCompare="100"
                    ErrorMessage="Please enter an integer less than 100.<br />"
                    Display="Dynamic"
                    runat="server"/>

               <asp:CompareValidator
                    ID="Value1MaxCompareValidator"
                    ControlToValidate="Value1"
                    Operator="GreaterThan"
                    Type="Integer"
                    ValueToCompare="0"
                    ErrorMessage="Please enter an integer greater than 0.<br />"
                    Display="Dynamic"
                    runat="server"/>
            </td>
            <td colspan="2">
               <asp:RequiredFieldValidator
                    ID="Value2RequiredValidator"
                    ControlToValidate="Value2"
                    ErrorMessage="Please enter a value.<br />"
                    Display="Dynamic"
                    runat="server"/>

               <asp:CompareValidator
                    ID="Value2MinCompareValidator"
                    ControlToValidate="Value2"
                    Operator="LessThan"
                    Type="Integer"
                    ValueToCompare="100"
                    ErrorMessage="Please enter an integer less than 100.<br />"
                    Display="Dynamic"
                    runat="server"/>

               <asp:CompareValidator
                    ID="Value2MaxCompareValidator"
                    ControlToValidate="Value2"
                    Operator="GreaterThan"
                    Type="Integer"
                    ValueToCompare="0"
                    ErrorMessage="Please enter an integer greater than 0.<br />"
                    Display="Dynamic"
                    runat="server"/>

            </td>
            <td>
                
            </td>
         </tr>
         <tr align="center">
            <td colspan="4">
               <input type="Submit"
                      name="AddButton"
                      value="Add"
                      onserverclick="AddButton_Click"
                      runat="server"/>

                  

               <input type="Reset"
                      name="AddButton"
                      value="Reset"
                      runat="server"/>
            </td>
            <td>
                
            </td>
         </tr>
      </table>

   </form>
</body>
</html>

備註

Enctype使用 屬性來指定瀏覽器用來將資料傳回伺服器的編碼類型。

下表顯示常見的編碼類型。

編碼類型 描述
application/x-www-form-urlencoded 表單資料會編碼為名稱/值組。 這是標準編碼格式。
multipart/form-data 表單資料會編碼為訊息,其中包含頁面上每個控制項的個別部分。
text/plain 表單資料會以純文字編碼,不含任何控制項或格式化字元。

如需編碼類型的詳細資訊,請參閱 World Wide Web Consortium (W3C 網站

適用於

另請參閱