<!DOCTYPE html>
<html>
<head>
<title>ASP.NET Web Pages Form</title>
</head>
<body>
<h1>Simple Form Example</h1>
<form method="post" action="ProcessForm.cshtml">
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName" required>
<br>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName" required>
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
在这个例子中:
- 使用 <form> 元素创建一个HTML表单。
- 使用 method="post" 指定表单提交的HTTP方法为POST。
- 使用 action="ProcessForm.cshtml" 指定表单提交后处理的页面为 ProcessForm.cshtml。
- 使用 <label> 元素为输入框创建标签。
- 使用 <input> 元素创建文本框和提交按钮。
- 使用 required 属性确保用户在提交表单时必须填写必要的字段。
接下来,你可以在 ProcessForm.cshtml 页面中处理表单提交,例如:
@{
if (IsPost) {
var firstName = Request["firstName"];
var lastName = Request["lastName"];
var email = Request["email"];
// 在这里可以进行表单提交后的处理,比如保存到数据库
}
}
<!DOCTYPE html>
<html>
<head>
<title>Form Processing</title>
</head>
<body>
<h2>Form Submitted Successfully</h2>
<!-- 显示提交的数据或其他反馈信息 -->
</body>
</html>
请注意,这只是一个简单的例子。在实际应用中,你可能需要进行更多的验证和安全性处理,比如防止跨站脚本攻击(XSS)等。
转载请注明出处:http://www.pingtaimeng.com/article/detail/6532/ASP.NET