ASP.NET Postback的Web Page生命周期
创始人
2024-04-25 02:31:55
0

说道ASP.NET Postback,就得说Web Page生命周期,但是Web Page生命周期却不是三言两语就能够说得清楚的,所以在这里单纯站的编程的角度,撇开Web Page生命周期浅谈Postback。

我们知道,无论是ASP.NET1.x,2.0,甚至是以后的版本,ASP.NET最终Render到Client端通过浏览器浏览的都是一样:一个单纯的HTML。Client通过Submit Form的方式将填入Form的数据提交给Server进行处理。我们现在来看看ASP.NET Postback程序处理的过程。

首先我们通过一个Sample来看ASP.NET如何处理一个通过Click一个Button引起的Postback。下面是Web Page的HTML:

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.
    aspx.cs"Inherits="_Default"%> 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> 
  4. <headrunatheadrunat="server"> 
  5. </FONT></STRONG>TestPage<STRONG><FONT color=#006699> 
  6.  
  7.  
  8. <formidformid="form1"runat="server"> 
  9.  
  10. <asp:Labelrunatasp:Labelrunat="server"ID="LabelMessage"ForeColor="red"> 
 
  •  
  • <asp:Buttonrunatasp:Buttonrunat="server"ID="Button1"Text="Button1"OnClick=
    "Button1_Click"OnCommand="Button_Command"CommandArgument="Button1"/> 
  • <asp:Buttonrunatasp:Buttonrunat="server"ID="Button2"Text="Button2"OnClick=
    "Button2_Click"OnCommand="Button_Command"CommandArgument="Button2"UseSubmitBehavior="false"/> 
  • <asp:Buttonrunatasp:Buttonrunat="server"ID="Button3"Text="Button3"OnClick=
    "Button3_Click"OnCommand="Button_Command"CommandArgument="Button3"UseSubmitBehavior="false"/> 
  •  
  •  
  •  
  •  
  • 很简单,定义了3个Button,分别注册了他们的两个Event:Click和Command。3个Button的Command Event Hander是一样的:Button_Command,通过指定的CommandArgument来让Event Handler判断到底是哪个Button触发了Command  Event。

    下面是Code Behind:

    1. usingSystem;  
    2. usingSystem.Data;  
    3. usingSystem.Configuration;  
    4. usingSystem.Web;  
    5. usingSystem.Web.Security;  
    6. usingSystem.Web.UI;  
    7. usingSystem.Web.UI.WebControls;  
    8. usingSystem.Web.UI.WebControls.WebParts;  
    9. usingSystem.Web.UI.HtmlControls;  
    10.  
    11. publicpartialclass_Default:System.Web.UI.Page  
    12. {  
    13.  
    14. protectedvoidPage_Load(objectsender,EventArgse)  
    15. {  
    16.  
    17. }  
    18. protectedvoidButton1_Click(objectsender,EventArgse)  
    19. {  
    20. stringstringmessage=string.Format("The{0}eventof{1}isfired","Click","Button1");  
    21. this.LabelMessage.Text=message;  
    22. }  
    23. protectedvoidButton2_Click(objectsender,EventArgse)  
    24. {  
    25. stringstringmessage=string.Format("The{0}eventof{1}isfired","Click","Button2");  
    26. this.LabelMessage.Text=message;  
    27. }  
    28. protectedvoidButton3_Click(objectsender,EventArgse)  
    29. {  
    30. stringstringmessage=string.Format("The{0}eventof{1}isfired","Click","Button3");  
    31. this.LabelMessage.Text=message;  
    32. }  
    33.  
    34. protectedvoidButton_Command(objectsender,CommandEventArgse)  
    35. {  
    36. stringstringmessage=string.Format("The{0}eventof{1}isfired",
      "Command",e.CommandArgument);  
    37. this.LabelMessage.Text+=";"+message;  
    38. }  
    39. }  

    以上介绍ASP.NET Postback的Web Page生命周期.

    【编辑推荐】

    1. 微软发布ASP.NET MVC 2预览版 多项功能更新
    2. ASP.NET服务器自定义控件安全准则
    3. ASP.NET编程规范之编码规范浅析
    4. 关于ASP.NET Session的一点认识
    5. ASP.NET编程工具ASP.NET Web Matrix详细介绍

    相关内容

    热门资讯

    PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
    网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
    各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
    什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
    如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
    如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
    Intel将Moblin社区控... 本周二,非营利机构Linux基金会宣布,他们将担负起Moblin社区的管理工作,而这之前,Mobli...
    P2P的自白|我不生产内容,我... 现在一提起P2P,人们就会联想到正在被有关部门“围剿”的互联网理财服务。×租宝事件使得劳...
    施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
    Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...