HtmlInputButton 服务器控件声明性语法
[本文档仅供预览,在以后的发行版中可能会发生更改。包含的空白主题用作占位符。]
创建服务器端控件,该控件映射到 <input type=button>、<input type=submit> 和 <input type=reset> HTML 元素,并允许您分别创建命令按钮、提交按钮或重置按钮。
<input
Type="Button|Reset|Submit"
EnableViewState="False|True"
Id="string"
Visible="False|True"
OnDataBinding="OnDataBinding event handler"
OnDisposed="OnDisposed event handler"
OnInit="OnInit event handler"
OnLoad="OnLoad event handler"
OnPreRender="OnPreRender event handler"
OnServerClick="OnServerClick event handler"
OnUnload="OnUnload event handler"
runat="server"
/>
备注
使用 HtmlInputButton 控件可以对 <input type=button>、<input type=submit> 和 <input type=reset> HTML 元素进行编程。 用户单击 HtmlInputButton 控件时,来自嵌有该控件的窗体的输入被发送到服务器并得到处理。 然后,将响应发送回请求浏览器。
通过为 ServerClick 事件提供自定义事件处理程序,可以在单击控件时执行特定的指令集。
备注
reset 按钮不支持 ServerClick 事件。单击 reset 按钮时,不一定必须清除页上的所有输入控件。相反,在加载页时,它们返回到它们的原始状态。例如,如果文本框最初包含值“JohnDoe”,则单击 reset 按钮会使文本框返回到该值。
与 HtmlInputText 和 HtmlTextArea 控件一起使用时,可创建可以在服务器上处理的用户输入页或身份验证页。
备注
此控件不需要结束标记。
示例
下面的示例演示如何使用 HtmlInputButton 控件将窗体提交给服务器进行处理。
<%@ 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
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> HtmlInputButton Example </title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<h3> HtmlInputButton 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 an 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 an 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 id="Submit1" type="Submit"
name="AddButton"
value="Add"
onserverclick="AddButton_Click"
runat="server" />
<input id="Reset1" type="Reset"
name="AddButton"
value="Reset"
runat="server"/>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<%@ 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;
Answer = Convert.ToInt32(Value1.Value) +
Convert.ToInt32(Value2.Value);
AnswerMessage.InnerHtml = Answer.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title> HtmlInputButton Example </title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<h3> HtmlInputButton 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 an 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 an 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 id="Submit1" type="Submit"
name="AddButton"
value="Add"
onserverclick="AddButton_Click"
runat="server" />
<input id="Reset1" type="Reset"
name="AddButton"
value="Reset"
runat="server"/>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
请参见
任务
How to: Add HTML Server Controls to a Web Forms Page using ASP.NET Syntax