1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 <style type="text/css"> 9 .content-href{ 10 color:red;font-size:large; 11 } 12 </style> 13 </head> 14 <body> 15 <form id="form1" runat="server"> 16 <div> 17 <input runat="server" id="btn" type="button" value="OK"/> 18 </div> 19 <div id="content" runat="server"> 20 <a class="content-href" href="http://www.baidu.com"><%=contentHTML %>的网址是:<%=getHTML() %></a> 21 </div> 22 </form> 23 </body> 24 </html>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class index : System.Web.UI.Page 9 { 10 public string contentHTML; 11 protected void Page_Load(object sender, EventArgs e) 12 { 13 if (!Page.IsPostBack) 14 { 15 //不涉及到客户端页面的代码,放在这里可以保证不会重复执行 16 //Speak(); 17 } 18 //事件注册的代码放在这里比较好 19 btn.ServerClick += Say; 20 } 21 public void Speak() 22 { 23 string msg = btn.Value; 24 Response.Write("<script type='text/javascript'>alert('"+msg+"')</script>"); 25 } 26 public void Say(object hander,EventArgs e) 27 { 28 contentHTML = "百度"; 29 Response.Write("<script type='text/javascript'>alert('Welcome to China!');</script>"); 30 } 31 public string getHTML() 32 { 33 string tmp = "http://www.baidu.com/"; 34 return tmp; 35 } 36 37 }