温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何分析ASP.NET Postback的Web Page生命周期

发布时间:2021-11-26 17:54:45 来源:亿速云 阅读:140 作者:柒染 栏目:编程语言

这篇文章给大家介绍如何分析ASP.NET Postback的Web Page生命周期,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

说道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. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

  3. <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> 

  4. <headrunatheadrunat="server"> 

  5. <title>TestPage</title> 

  6. </head> 

  7. <body> 

  8. <formidformid="form1"runat="server"> 

  9. <div> 

  10. <asp:Labelrunatasp:Labelrunat="server"ID="LabelMessage"ForeColor="red"></asp:Label> 

  11. </div> 

  12. <div> 

  13. <asp:Buttonrunatasp:Buttonrunat="server"ID="Button1"Text="Button1"OnClick=
    "Button1_Click"OnCommand="Button_Command"CommandArgument="Button1"/> 

  14. <asp:Buttonrunatasp:Buttonrunat="server"ID="Button2"Text="Button2"OnClick=
    "Button2_Click"OnCommand="Button_Command"CommandArgument="Button2"UseSubmitBehavior="false"/> 

  15. <asp:Buttonrunatasp:Buttonrunat="server"ID="Button3"Text="Button3"OnClick=
    "Button3_Click"OnCommand="Button_Command"CommandArgument="Button3"UseSubmitBehavior="false"/> 

  16. </div> 

  17. </form> 

  18. </body> 

  19. </html> 

很简单,定义了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生命周期就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI