Note that there are some explanatory texts on larger screens.

plurals
  1. POPage_load fires twice for UpdatePanel refresh whilst having a jQuery bound change event on a drop down list
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Can someone explain why the erroneous page_load event is firing please??</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" %&gt; &lt;%@ Import Namespace="System.Diagnostics" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head runat="server"&gt; &lt;script runat="server"&gt; 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(); } &lt;/script&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" language="javascript"&gt; $(document).ready(function() { Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler); $("#&lt;%=ddlTest.ClientID%&gt;").change(function() { alert('jQuery binding fired'); }); }); function beginRequestHandler() { alert("Started ajax call"); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager ID="SciptManager" runat="server" /&gt; &lt;asp:UpdatePanel ID="updTest" runat="server" UpdateMode="Conditional"&gt; &lt;ContentTemplate&gt; &lt;asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlTest_OnSelectedIndexChanged" &gt; &lt;asp:ListItem&gt;First&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;Second&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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!</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true"%&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head runat="server"&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" language="javascript"&gt; $(document).ready(function() { $('#ddlTest').change(function() { alert('jQuery event fired'); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:DropDownList id="ddlTest" runat="server" onchange="alert('element event');"&gt; &lt;asp:ListItem Text="First" /&gt; &lt;asp:ListItem Text="Second" /&gt; &lt;/asp:DropDownList&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload