Cancelling Async post backhttp://forums.asp.net/t/1078406.aspx/1?Cancelling+Async+post+backFri, 23 Feb 2007 17:52:59 -050010784061589323http://forums.asp.net/p/1078406/1589323.aspx/1?Cancelling+Async+post+backCancelling Async post back <p>Hi,</p> <p>&nbsp;From the quickstart tutorials, Cancelling an Async Post Back </p> <p><a href="http://ajax.asp.net/docs/ViewSample.aspx?sref=UpdatePanelTutorial17/cs/CancelPostback.js">http://ajax.asp.net/docs/ViewSample.aspx?sref=UpdatePanelTutorial17/cs/CancelPostback.js</a></p> <p>Works fine as it is, but if i add a Session variable in the Default.aspx page_load, i can no longer cancel the </p> <p>Async post back from client ? means it cancells only after the first call returns !jhj</p> <p>Pls Help</p> <p>Thx,</p> <p>Gopi</p> <p>&nbsp;</p> 2007-02-22T01:46:22-05:001589408http://forums.asp.net/p/1078406/1589408.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back Are you checking for IsCallBack? What does your code look like in Page_Load event? 2007-02-22T03:03:39-05:001589874http://forums.asp.net/p/1078406/1589874.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back <p>It is the exact code that the tutorial has here [http://ajax.asp.net/docs/ViewSample.aspx?sref=UpdatePanelTutorial17/cs/default.aspx]</p> <p>in the page_load event i added,&nbsp;</p> <p>&nbsp;protected void Page_Load(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>Session[&quot;Test&quot;] = &quot;123&quot;;&nbsp; // Line added by me !<br> </strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!IsPostBack)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HeadlineList.DataSource = GetHeadlines();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HeadlineList.DataBind();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; }<br> </p> <p>After adding that session line, when i press refresh button, a small cancel button is visible to cancel the async postback !</p> <p>It no longer cancels with that added line, but if u remove that Line, it works magically ! So something with session variables and call back !?</p> <p>Thx,</p> <p>Gopi</p> 2007-02-22T11:16:48-05:001591166http://forums.asp.net/p/1078406/1591166.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back <p>You don't need to set Session variable everytime Page is posted back. Change your code as follows and see if it works:</p> <p>If (! this.IsPostBack &amp;&amp; ! this.IsCallBack ) {</p> <p><strong>Session[&quot;Test&quot;] = &quot;123&quot;;&nbsp;&nbsp;</strong>&nbsp;</p> <p>}</p> 2007-02-23T02:27:12-05:001591177http://forums.asp.net/p/1078406/1591177.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back <p>What exactly do you mean &quot;it cancels only after the first call returns&quot;?&nbsp; (What's different between the Session version and the non-Session version?)</p> 2007-02-23T02:35:23-05:001591192http://forums.asp.net/p/1078406/1591192.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back <p>Steve,</p> <p>You have run this tutorial to see what i meant !&nbsp; here is the url : <a href="http://ajax.asp.net/docs/Samples/UpdatePanelTutorial17/cs/Default.aspx"> http://ajax.asp.net/docs/Samples/UpdatePanelTutorial17/cs/Default.aspx</a></p> <p>and here is the original tutorial : <a href="http://ajax.asp.net/docs/tutorials/CancelAsyncPostback.aspx"> http://ajax.asp.net/docs/tutorials/CancelAsyncPostback.aspx</a></p> <p>In that page, click Refresh button, after the click&nbsp;u will see a cancel link, and if u click the cancel the page aborts the async post back !</p> <p>My problem is, as soon as i start using any session variables on the page_load or on the call back method , the cancel button action [which aborts the async post back!] </p> <p>no longer works ! Just for reference i said, on page_load, but on any of those page methods, if i start using, say, session[&quot;Test&quot;] = &quot;123&quot;; the cancel action no longer works !!</p> <p>Nilesh, </p> <p>It doesn't matter&nbsp; -- If (! this.IsPostBack &amp;&amp; ! this.IsCallBack ) { --}&nbsp; I tried all combinations, as soon as i use any session variables, any where on the page, the cancel action </p> <p>stops working ! </p> <p>Defenetly there is something wrong here !!&nbsp; :)</p> <p>Thx,</p> <p>Gopi</p> 2007-02-23T02:50:23-05:001591213http://forums.asp.net/p/1078406/1591213.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back <p>Are you checking whether AsyncPostBack is still underway? What if postback already is been processed?</p> <p>Take a look at this tutorial where it checks status of Asynchronous postback before trying to cancel it.</p> <p><a href="http://ajax.asp.net/docs/tutorials/CancelAsyncPostback.aspx">http://ajax.asp.net/docs/tutorials/CancelAsyncPostback.aspx</a></p> <p><strong>PS:</strong> If you are trying to cancel an operation. It should be atomic. For example you are setting session state and cancel the operation before processing further. This will result in unwanted results.</p> 2007-02-23T03:16:37-05:001591227http://forums.asp.net/p/1078406/1591227.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back I don't understand what you mean by &quot;the cancel button no longer works.&quot;&nbsp; What do you expect the button to do that it's not doing?&nbsp; When I use the sample, the visible effect of pressing the cancel button is that that panel disappears.&nbsp; Is that what's no longer working? 2007-02-23T03:33:43-05:001591236http://forums.asp.net/p/1078406/1591236.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back <p>Nevermind, I recreated the sample on my machine so I could see what was going on.</p> <p>What's happening is that using Session variables means that only one request to the page can be handled at a time.&nbsp; This is because the server acquires&nbsp;a session lock to avoid contention issues.</p> <p>The &quot;cancel&quot; button in the sample does <em>two </em>things.&nbsp; First, it cancels the current async postback (which really just means that the client ignores the result... there's no way to stop the server from doing its work), and then it starts a new async postback which hides the alert.</p> <p>The second postback has to wait for the first one to complete because of the session locking I described above.&nbsp; So the postback is getting canceled right away; it's the second postback that's being delayed.</p> <p>&nbsp;</p> <p>A better implementation than what's in the sample would have been to hide the alert on the client so that it disappears right away when you cancel the async postback.&nbsp; The next one would still take extra long (waiting for the server to finish its work from the previous one), but it would be less confusing to the user.</p> 2007-02-23T03:50:05-05:001591681http://forums.asp.net/p/1078406/1591681.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back <p>Thanks Steve, for understanding my problem ! clarification : what i meant when i say cancelling postback is i cannot execute any other action from client until the first</p> <p>&nbsp;call completes [as u said it is because of session lock]. so&nbsp; i went and modified the code like this..</p> <font color="#0000ff" size="2"> <p>if</font><font size="2"> (!IsPostBack &amp;&amp; !IsCallback &amp;&amp; !ScriptManager1.IsInAsyncPostBack){</p> <p></font><font color="#008080" size="2">HttpContext</font><font size="2">.Current.Session[</font><font color="#800000" size="2">&quot;test&quot;</font><font size="2">] = </font><font color="#800000" size="2">&quot;adfasd&quot;</font><font size="2">;</font></p> <p><font size="2">HeadlineList.DataSource = GetHeadlines();</p> <p>HeadlineList.DataBind();</p> <p>}</p> <font color="#0000ff" size="2"> <p>protected</font><font size="2"> </font><font color="#0000ff" size="2">void</font><font size="2"> NewsClick_Handler(</font><font color="#0000ff" size="2">object</font><font size="2"> sender, </font><font color="#008080" size="2">EventArgs</font><font size="2"> e){</font></p> <p><font size="2">System.Threading.</font><font color="#008080" size="2">Thread</font><font size="2">.Sleep(10000);</p> </font><font size="2"> <p>HeadlineList.DataSource = GetHeadlines();</p> <p>HeadlineList.DataBind();</p> <p>}</p> </font> <p>Now the session code never executes [so no locking] while the Async call is going on (NewsClick_Handler for 10 sec) but still I'm not able to cancel the postback, the clicking of cancel</p> <p>waits until the 10 sec wait is over&nbsp;!&nbsp; But if i remove the Session line it beautiful works ! One more point that i observed [in the original sample, without session] is, </p> <p>even if i cancel the async post back from client and perform another action [like moving to anther page] the original call back completes, and that is the exact senario i was looking, </p> <p><strong>kind of fire &amp; forgot.</strong></p> <p>Thanks for all your help, let me know if u can somehow find a magic situation where i can use the session without affecting Async actions !</p> <p>Gopi</p> </font> 2007-02-23T11:10:23-05:001592303http://forums.asp.net/p/1078406/1592303.aspx/1?Re+Cancelling+Async+post+backRe: Cancelling Async post back <p>Good point; as you noticed, the session lock happens even if you don't actually touch the session during that call.&nbsp; So if you set a session variable anywhere (even another page), your postbacks will have to wait for previous ones to finish.</p> 2007-02-23T17:52:59-05:00