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

说道ASP.NET Postback,就得说Web Page生命周期,可是Web Page生命周期却不是片言只语就可以说得清楚的,所以在这里单纯站的编程的视点,放下Web Page生命周期浅谈Postback。

ASP.NET Postback的Web Page生命周期  Web Postback 第1张

咱们知道,无论是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. publicpartialclass_Default:System.Web.UI.Page
  11. {
  12. protectedvoidPage_Load(objectsender,EventArgse)
  13. {
  14. }
  15. protectedvoidButton1_Click(objectsender,EventArgse)
  16. {
  17. stringstringmessage=string.Format("The{0}eventof{1}isfired","Click","Button1");
  18. this.LabelMessage.Text=message;
  19. }
  20. protectedvoidButton2_Click(objectsender,EventArgse)
  21. {
  22. stringstringmessage=string.Format("The{0}eventof{1}isfired","Click","Button2");
  23. this.LabelMessage.Text=message;
  24. }
  25. protectedvoidButton3_Click(objectsender,EventArgse)
  26. {
  27. stringstringmessage=string.Format("The{0}eventof{1}isfired","Click","Button3");
  28. this.LabelMessage.Text=message;
  29. }
  30. protectedvoidButton_Command(objectsender,CommandEventArgse)
  31. {
  32. stringstringmessage=string.Format("The{0}eventof{1}isfired",
    "Command",e.CommandArgument);
  33. this.LabelMessage.Text+=";"+message;
  34. }
  35. }

以上介绍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具体介绍
转载请说明出处
知优网 » ASP.NET Postback的Web Page生命周期

发表评论

您需要后才能发表评论