Monday, March 26, 2012

Call the method bound to an linkbutton after ajax callback

Hi everybody

I just have the following problem:

I created a ASP-LinkButton and added a method in the code-behind file for its onclick event. Then I added a break point at the beginning of the method to check if it's called.

Now I added a JS which calls the __doPostback("xxx", ""); exactly like the LinkButton does and everything works fine.

Now I add a Webservice, which validates my input and if everything works fine, its callback should call the click method of the asp-button. Guess what happens: the postback is called, but not the event of the button. If I do this before outside the callback - everything works fine, so I know the code is correct.

I even tried to add a "window.setInterval" timer which always checks a bool and calls the postback as soon as the bool is turned to true ... Then I set the bool after the callback and guess again: The postback is called but not the method ...

I don't have any further ideas for workarounds - anyone got a hint for me?

THANKS!

Hi,

Now I get a general idea of your situation. But your description doesn't give me sufficient information to find the cause.

Accordingly, I made a sample, please try it.

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %><!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"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services><asp:ServiceReference Path="WebService.asmx" InlineScript="true" /> </Services> </asp:ScriptManager> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton></div> </form> <script type="text/javascript"> var count = 0; function callBack(result) { if(result) __doPostBack('LinkButton1',''); } function callWS() { count++; WebService.ShouldCallBack(count, callBack); } window.setInterval(callWS, 2000); </script></body></html>
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partialclass Default3 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) { }protected void LinkButton1_Click(object sender, EventArgs e) { Response.Write("Linkbutton is clicked on " + DateTime.Now.ToString()); }}

<%@. WebService Language="C#" Class="WebService" %>using System;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService]public class WebService : System.Web.Services.WebService { [WebMethod] public bool ShouldCallBack(int count) { return count == 5; } }
Hope this helps.

No comments:

Post a Comment