1. 前端HTML代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 <head runat="server"> 7 <title>HTML服务器控件</title> 8 <script type="text/javascript" runat="server"> 9 //protected void Page_Load(Object sender, EventArgs e) //如果在当前页面设置Load事件, 那么.cs文件中的load的事件将会被覆盖 10 //{ 11 // this.MyText.Value = "hello world!"; //设置input的默认值 12 //} 13 </script> 14 <style type="text/css"> 15 #MyText 16 { 17 width:188px; 18 } 19 </style> 20 </head> 21 <body> 22 <form id="form1" runat="server"> 23 <input id="MyText" type="text" runat="server" /> 24 </form> 25 </body> 26 </html>
2.下面是.cs中的load事件, 如果页面中有Page_load事件, 那么该事件将会被覆盖 (Page_load事件→页面初始化事件)
1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.UI; 5 using System.Web.UI.WebControls; 6 7 public partial class _Default : System.Web.UI.Page 8 { 9 protected void Page_Load(object sender, EventArgs e) 10 { 11 if (!IsPostBack) 12 { 13 this.MyText.Value = "我是首次加载"; 14 } 15 else 16 { 17 this.MyText.Value = "我不是首次加载"; 18 } 19 } 20 }
时间: 2024-10-08 10:43:56