62,074
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" UseSubmitBehavior="false" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class test : System.Web.UI.Page
{
#region Properties
protected System.Web.UI.WebControls.Label lblTest = new Label();
#endregion Properties
//可以自己写个Page_Load
protected void MyPageLoad(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.btnTest.Text = "Click Me";
//虽然是动态添加的控件,注意运行时,属性是持久的
this.lblTest.Text = "I am lblTest";
}
}
//接管OnInit,自己加点东西到页面
protected override void OnInit(EventArgs e)
{
this.InitializeComponent();
base.OnInit(e);
}
protected void InitializeComponent()
{
//注册控件
this.Controls.Add(this.lblTest);
//注册事件
this.btnTest.Click += new EventHandler(btnTest_Click);
//注册自定义的Page_Load
this.Load += new EventHandler(MyPageLoad);
}
protected void btnTest_Click(object sender, EventArgs e)
{
this.txtTest.Text = "The Button Have Been Clicked";
}
}