dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersPhagu Mahato
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » JQuery »

Problem with static nature of webmethod in jquery.ajax


Posted Date: 17 Aug 2012      Posted By:: imtiyaz     Member Level: Bronze    Member Rank: 1668     Points: 2   Responses: 4



how to call webmethod and register a script block using jquery.ajax?or is there any other way to call a method from client side which registers scriptblock also and returns value too



Responses

#684709    Author: Ajatshatru Upadhyay      Member Level: Gold      Member Rank: 19     Date: 19/Aug/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

You can use json data with jQuery to call a web method from jQuery and you can also return some value to the client script.
See below example in which I am fetching the records from database on textbox keyup event and return it to the client script which displays it in label control in turn:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("#TextBox1").keyup(function () {
var input = $(this).val();
alert(input);

$.ajax({
type: "POST",
url: "AutoCompleteWithJQueryAndjson.aspx/AutoComplate",
data: "{'args': '" + input + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",

success: function (output) {
$("#<%=Label1.ClientID%>").html(output.d);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>


And below is the web method signature in code behind:


[System.Web.Services.WebMethod(Description = "Per session Hit Counter", EnableSession = true)]
public static string AutoComplate(string args)
{
// code goes here...
//retrun statement
}


Hope it'll help you.
Regards
Ajatshatru



 
#685077    Author: imtiyaz      Member Level: Bronze      Member Rank: 1668     Date: 21/Aug/2012   Rating: 2 out of 52 out of 5     Points: 1

but i want to call a javascript function too in web method...if i mark method to be static i wont be able to acess the page....


 
#685096    Author: Ajatshatru Upadhyay      Member Level: Gold      Member Rank: 19     Date: 21/Aug/2012   Rating: 2 out of 52 out of 5     Points: 3

Hi,

You cannot do that. You are calling a web method on the server through Ajax and a web method knows nothing about the page. This is not a regular method which is bind to the page class and thus knows everything about the page.
However on work around could be return values (those you want to pass in the JavaScript function) to client script and the call that function from within client side.

Hope it'll help you.
Regards
Ajatshatru






 
#685180    Author: imtiyaz      Member Level: Bronze      Member Rank: 1668     Date: 22/Aug/2012   Rating: 2 out of 52 out of 5     Points: 1

Thanks for your response will try to find an alternate way...the method u suggested to get the values may not work..because data is huge and its dynamic


 
Post Reply
You must Sign In to post a response.

Next : JQuery Syntax Not easy to understand?
Previous : Does jQuery mobile works without HTML5
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.