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>
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; charset=utf-8" /><title>
</title></head>
<body>
<form method="post" action="WebForm1.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="nt/N01/gSpnPPbmQT8jzelpFxgXESXcqwmzQSbOCl7+yPVoTPl06+3BPf/4cVUb97mIZalrN+6u4V9gBou3pxGHprpwSPwdYrYFO5PLiR5M=" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="xGF80Sv6EYOZg6IM3vlwEHP7wW8h6kCtXfDyyG9oOhSQN6pV+iPrl7wIyY41GuyD20461qF9G0+w6EWu7Dx789A+U4Gx5/8SRrh4wo2Jda5PiAfycbYrT2MV3dz+d0gN4z643DQCTTBC01q7NgZXFw==" />
</div>
<div>
<span id="Label1">宝贝儿姓名:</span>
<input name="tbName" type="text" id="tbName" />
<input type="submit" name="btHello" value="问候一下" id="btHello" />
</div>
</form>
</body>
</html>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="nt/N01/gSpnPPbmQT8jzelpFxgXESXcqwmzQSbOCl7+yPVoTPl06+3BPf/4cVUb97mIZalrN+6u4V9gBou3pxGHprpwSPwdYrYFO5PLiR5M=" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="xGF80Sv6EYOZg6IM3vlwEHP7wW8h6kCtXfDyyG9oOhSQN6pV+iPrl7wIyY41GuyD20461qF9G0+w6EWu7Dx789A+U4Gx5/8SRrh4wo2Jda5PiAfycbYrT2MV3dz+d0gN4z643DQCTTBC01q7NgZXFw==" />
<input type=submit ...>这是不需要写上什么“dom事件”就可以自动回发——提交这个<form />的。而提交时,在post参数中会有
&btHello=问候一下这样的数据。asp.net处理post数据时,看到btHello是一个Button控件,就会随后执行它的 Click。