千家信息网

ASP.NET整个Postback程序处理的过程是怎么样的

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,ASP.NET整个Postback程序处理的过程是怎么样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。ASP.NET整个Postbac
千家信息网最后更新 2025年02月01日ASP.NET整个Postback程序处理的过程是怎么样的

ASP.NET整个Postback程序处理的过程是怎么样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

ASP.NET整个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. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" %>

  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

  4. <head runat="server">

  5. <title>Test Pagetitle>

  6. head>

  7. <body>

  8. <form id="form1" runat="server">

  9. <div>

  10. <asp:Label runat="server" ID="LabelMessage" ForeColor="red">asp:Label>

  11. div>

  12. <div>

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

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

  15. <asp:Button runat="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. using System;

  2. using System.Data;

  3. using System.Configuration;

  4. using System.Web;

  5. using System.Web.Security;

  6. using System.Web.UI;

  7. using System.Web.UI.WebControls;

  8. using System.Web.UI.WebControls.WebParts;

  9. using System.Web.UI.HtmlControls;

  10. public partial class _Default : System.Web.UI.Page

  11. {

  12. protected void Page_Load(object sender, EventArgs e)

  13. {

  14. }

  15. protected void Button1_Click(object sender, EventArgs e)

  16. {

  17. string message = string.Format("The {0} event of {1} is fired", "Click", "Button1");

  18. this.LabelMessage.Text = message;

  19. }

  20. protected void Button2_Click(object sender, EventArgs e)

  21. {

  22. string message = string.Format("The {0} event of {1} is fired", "Click", "Button2");

  23. this.LabelMessage.Text = message;

  24. }

  25. protected void Button3_Click(object sender, EventArgs e)

  26. {

  27. string message = string.Format("The {0} event of {1} is fired", "Click", "Button3");

  28. this.LabelMessage.Text = message;

  29. }

  30. protected void Button_Command(object sender, CommandEventArgs e)

  31. {

  32. string message = string.Format("The {0} event of {1} is fired",
    "Command", e.CommandArgument);

  33. this.LabelMessage.Text += "; " + message;

  34. }

  35. }

看完上述内容,你们掌握ASP.NET整个Postback程序处理的过程是怎么样的的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

0