开发者

Page_load fires twice for UpdatePanel refresh whilst having a jQuery bound change event on a drop down list

开发者 https://www.devze.com 2023-01-15 14:06 出处:网络
I\'ve tried to wrap up my problem with a complete example below - the original problem is part of a jQuery plug-in that I\'m writing to extend the behaviour of an ASP.NET ajax application I already ha

I've tried to wrap up my problem with a complete example below - the original problem is part of a jQuery plug-in that I'm writing to extend the behaviour of an ASP.NET ajax application I already have.

The aspx page below has one drop down list which is marked for auto post back. I've also bound a change event using jquery (which ultimately I will swap for a .live() event to maintain the binding after the update panel refresh.) The problem is, when the jQuery event is bound I see two ajax begin requests and page_loads fire but only one ddlTest_OnselectedIndexChanged event. One of the page loads is malformed too - the content-length states it's about 300 bytes long whilst the totalBytes is 0 and form data empty. This does not happen if I bind to the click event of a button.

Can someone explain why the erroneous page_load event is firing please??

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Diagnostics" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <script runat="server">
      private void Page_Load(object sender, EventArgs e)
      {
          Debug.WriteLine(Request.Headers["Content-Length"]);
          Debug.WriteLine(Request.TotalBytes);
          Debug.WriteLine(Request.Form.ToString());
          Debugger.Break();
      }
      private void ddlTest_OnSelectedIndexChanged(object sender, EventArgs e)
      {
          Debugger.Break();
      }
    </script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function() {
            Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler);
            $("#<%=ddlTest.ClientID%>").change(function() { alert('jQuery binding fired'); });
        });
        function beginRequestHandler() {
            alert("Started ajax call");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="SciptManager" runat="server" />
        <asp:UpdatePanel ID="updTest" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlTest_OnSelectedIndexChanged" >
                    <asp:ListItem>First</asp:ListItem>
                    <asp:ListItem>Second</asp:ListItem>
                </asp:DropDownList>
            </ContentTemplate>
        </asp:UpdatePan开发者_StackOverflow中文版el>
    </form>
</body>
</html>

Update : I've narrowed the problem down even further. Firefox and Chrome both work as I would expect whilst IE is having a problem. I've also simplified the erroneous code to the example below. It's hard to believe it's this simple but the jQuery .change() method on the drop down list triggers the onchange event on the select element twice! I'll look for other people who've had this problem and report back my findings!

<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function() {
            $('#ddlTest').change(function() { alert('jQuery event fired'); });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:DropDownList id="ddlTest" runat="server" onchange="alert('element event');">
            <asp:ListItem Text="First" />
            <asp:ListItem Text="Second" />
        </asp:DropDownList>
    </form>
</body>
</html>


The call of $("#<%=ddlTest.ClientID%>").change(... is probably trigger your drop down list and because of your autopostBack, you get an second refresh of your page.


It looks like I've uncovered a bug in the latest version of jQuery :(. If I change the test back to version 1.3.2 it works fine! I've also found a reference to a bug raised with jQuery.


Take out the autopostback=true = it is firing one postback and the change event is another.

0

精彩评论

暂无评论...
验证码 换一张
取 消