2

I have a condition where i need to call a jquery function using a webmethod as below:

[WebMethod]
public static void BindData(String Site)
{
    String HTML = "";
    Int32 i = 1;
    DataTable dt = new DataTable();
    dt = obj.GetAll(objprop);
    if (dt.Rows[0]["UserId"].ToString() != "0")
    {
        foreach (DataRow item in dt.Rows)
        {
            string Email = Bal.Common.Decryptdata(item["Email"].ToString());
            string SentInvitation = item["SentInvitation"].ToString();

            SentInvitation = SentInvitation.ToString() == "1" ? "Already Invited" : "";
            if (i % 2 == 0)
                HTML += "<div class=~other_wish_row2~><div class=~friend_list_box1~><input type=~checkbox~ class=~chkEmail~ id=~chkId^" + i + "~/></div><div class=~friend_list_box2~><p><label id=~lbl" + i + "~>" + Email.ToString() + "</label><label class=~SentInvitationLbl~ id=~lblSentInvitation" + i + "~>" + SentInvitation + "</label></p></div><div class=~friend_list_box3~></div><div class=~clear~></div></div>";
            else
                HTML += "<div class=~other_wish_row3~><div class=~friend_list_box1~><input type=~checkbox~ class=~chkEmail~ id=~chkId^" + i + "~/></div><div class=~friend_list_box2~><p><label id=~lbl" + i + "~>" + Email.ToString() + "</label><label class=~SentInvitationLbl~ id=~lblSentInvitation" + i + "~>" + SentInvitation + "</label></p></div><div class=~friend_list_box3~></div><div class=~clear~></div></div>";

            i = i + 1;
        }
        ScriptManager.RegisterStartupScript((Page)(HttpContext.Current.Handler), typeof(Page), "hdrEmpty1", "Test('" + HTML + "');", true); return;
    }
    else
    {

    }
}

Jquery Code is:

function Test(data) {
   alert('hi');
}

function Binddata(SocialSite) {

$.ajax({
    type: "POST",
    url: "InviteFriends.aspx/BindData",
    data: "{SocialSite:'" + SocialSite + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {

    }
 });
}

I am not able to fire Test(), please help to resolve this.

4
  • 1
    Why can't you return the html string in the AJAX function where you are calling this Web Method and Set this HTML by calling your Test() function from JavaScript itself ? Commented Sep 3, 2013 at 8:31
  • Show code where BindData method called from client Commented Sep 3, 2013 at 8:31
  • @Raman Did you put breakpoint on your webmethod and checked whether it hits the breakpoint? Commented Sep 3, 2013 at 9:20
  • @raman return html as result of BindData method and process response as proposed in Alex's answer (change dataType option of $.ajax function parameter to html) Commented Sep 3, 2013 at 9:39

1 Answer 1

2

You can't do this from a web service (either an .asmx file or a WebMethod) as it does not run in the context of a normal page. I see you're using AJAX, you'll have to do the handling in the success callback method of your jQuery.ajax() call, like so:

 function Test(data) {
    alert('hi');
 }

 function Binddata(SocialSite) {

 $.ajax({
     type: "POST",
     url: "InviteFriends.aspx/BindData",
     data: "{SocialSite:'" + SocialSite + "'}",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (data) {
         Test(data);
     }
  });
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.