2011-12-27 56 views
1

如何使用Ajax jQuery调用具有以下场景? 有没有办法在页面的PostBack期间使用jQuery只绑定DropDownList如何使用Ajax JQuery调用来创建Page.IsPostBack选项?

<asp:DropDownList ID="ddlState" runat="server" DataSourceID="StateDataSource" DataTextField="State1" 
    Width="155px" DataValueField="State1" TabIndex="10"> 
</asp:DropDownList> 
<asp:ObjectDataSource ID="StateDataSource" runat="server" SelectMethod="State_SelectALL" 
    TypeName="PL.BLL.StateController" EnableCaching="true"> 
</asp:ObjectDataSource> 

回答

1

ScriptManager类提供的属性IsInAsyncPostback因为3.5,但你通过的jQuery做Ajax请求,我想你不使用一个ScriptManager。

的jQuery实际上增加了一个HTTP报头添加到您可以使用服务器端的检查,如果请求ajaxed或不要求:"X-Requested-With" = "XMLHttpRequest"

// X-Requested-With header 
// For cross-domain requests, seeing as conditions for a preflight are 
// akin to a jigsaw puzzle, we simply never set it to be sure. 
// (it can always be set on a per-request basis or even using ajaxSetup) 
// For same-domain requests, won't change header if already provided. 
if (!s.crossDomain && !headers["X-Requested-With"]) { 
    headers[ "X-Requested-With" ] = "XMLHttpRequest"; 
} 

在你的后台代码,你可以添加一个属性你的页面(或你的基本页面 - 或提出延期法)来检查值:Detecting Ajax Events on the Server

1

public bool IsAjaxPostback 
{ 
    get 
    { 
     return HttpContext.Current.Request.Headers["X-Requested-With"] == "XMLHttpRequest"; 
    } 
} 

关于这个问题的好文章