日期:2014-05-17  浏览次数:20493 次

asp.net生成xhtml的事件驱动原理
WebForm.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<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:Label ID="Label1" runat="server" Text="宝贝儿姓名:"></asp:Label>
            <asp:TextBox ID="tbName" runat="server"></asp:TextBox>
            <asp:Button ID="btHello" runat="server" Text="问候一下" OnClick="btHello_Click" />
        </div>
    </form>
</body>
</html>


WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btHello_Click(object sender, EventArgs e)
        {
            String str;
            StringBuilder StrScript = new StringBuilder();
            StrScript.Append("<script language=javascript>");
            if(this.tbName.Text != ""){
                str=this.tbName.Text.Trim() + ",你好!";
            }else{
                str="姓名不能为空";
            }

            StrScript.Append("alert('" + str + "');");
            StrScript.Append("</script>");
            RegisterStartupScript("MessageBox", StrScript.ToString());
        }
    }
}


执行后页面代码:
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html;