asp.net DoPostBack 引发回传的原理

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>

posted @ 2012-03-28 11:02  虎头  阅读(4583)  评论(1编辑  收藏  举报