asp.net DoPostBack 引發回傳的原理

來源:互聯網
上載者:User

asp.net有兩種方法引發回傳

1、通過button,ImageButton直接引發回傳

2、其他控制項通過__doPostBack 函數引發回傳

具體原理如下

一、Button和ImageButton直接引發回傳,當在這兩種控制項上觸發事件時,控制項的ID會作為參數傳給伺服器,從而使伺服器得知該映射到哪個後端事件
1、Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="dopostback._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Button ID="btnQuery1" runat="server" Text="query1"
    onclick="btnQuery_Click" />
    <br />
    <asp:Button ID="btnQuery2" runat="server" Text="query2"
    onclick="btnQuery2_Click" />

</asp:Content>

2、產生Html代碼

<form method="post" action="default.aspx" id="ctl01">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTAxODQ0NzY0N2Rk3QmLGwellfxyiCeAIAA4oqYSwo1xlvB7VFxjQu4b4vI=" />
</div>

<div class="aspNetHidden">
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKVtZf9DwLGp+bIDgLhkITeCIMB/c9fsbMu0t/9zhPZn6jrsl6JLzQfhKgCAW5b2NYc" />
</div>       
    <input type="submit" name="ctl00$MainContent$btnQuery1" value="query1" id="MainContent_btnQuery1" />
    <br />
    <input type="submit" name="ctl00$MainContent$btnQuery2" value="query2" id="MainContent_btnQuery2" />
</form>

3、點擊query1
submit表單,通過Request.Form,可以取到AllKeys,有三個key
1、__VIEWSTATE
2、__EVENTVALIDATION
3、ctl00$MainContent$btnQuery1
button控制項引發PostBack,會把button的Id做為Request.Form的一個Key,通過枚舉key值,尋找控制項執行個體,如果控制項是button控制項,則判斷是由
該button控制項引發事件

二、使用__doPostBack的javascript方法來觸發回傳

1、Default2.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="dopostback.Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:LinkButton ID="linkButton1" runat="server" Text="linkButton1" onclick="linkButton1_Click" ></asp:LinkButton>
</asp:Content>

2、Default2.aspx產生的Html
其中,__EVENTTARGET和__EVENTARGUMENT兩個隱藏欄位會被自動聲明,分別指向觸發事件的控制項ID和相應的附加參數

(比如,該控制項可觸發多個事件,則附加參數會指明是哪個事件被觸發而引起回傳)

ASP.NET正是通過這兩個隱藏欄位的值來判斷該如何將用戶端事件映射為對應的伺服器端事件的

<form method="post" action="Default2.aspx" id="ctl01">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTExNzk1NzM2OWRkw4zwv+DAA5BZznQNZ/w0ocuq/niWGiRu8JdCZ3cteC8=" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['ctl01'];
if (!theForm) {
    theForm = document.ctl01;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>
<div class="aspNetHidden">
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKv5vfMCAL1wdXpD5IlsVoH5XmXvrw/IzUitXA/gZD45wTh4pUMTbi3gkVb" />
</div>           
<a id="MainContent_linkButton1" href="javascript:__doPostBack(&#39;ctl00$MainContent$linkButton1&#39;,&#39;&#39;)">linkButton1</a>
    </form>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.