单文件模型
新建web窗体页面时,选上“将代码放在单独文件中”,逻辑代码放在<script>标签中,且包含runat = "server"
<%@ Page Language="C#"%> <!DOCTYPE html> <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <!-- script作为逻辑代码 --> <script runat = "server"> public void butSubmit_Click(object sender, EventArgs e) { lblMessage.Text = "不管你输入什么,我都喜欢你!"; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtInput" runat="server"></asp:TextBox> <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label><br /> <!-- onClick连接butSubmit_Click方法 --> <asp:Button ID="btnSubmit" runat="server" Text="确定" onClick="butSubmit_Click"/> </div> </form> </body> </html>
隐藏页模型
WebForm2.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Project2_2.WebForm2" %> <!DOCTYPE html> <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtInput" runat="server">请输入内容</asp:TextBox> <asp:Label ID="lblMessage" runat="server"></asp:Label><br /> <asp:Button ID="btnSubmit" runat="server" Text="Button" OnClick="btnSubmit_Click" /> </div> </form> </body> </html>
WebForm2.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Project2_2 { public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Text = "不管你输入什么,我都喜欢你!"; } } }