1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5: using System.Web.Services;
6: using System.Web.Script.Services;
7: /// <summary>
8: /// Summary description for WebService
9: /// </summary>
10: [WebService(Namespace = "http://tempuri.org/")]
11: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
12: // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
13: [System.Web.Script.Services.ScriptService]
14: public class WebService : System.Web.Services.WebService
15: { 16:
17: public WebService() { } 18: [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
19: [WebMethod]
20: public Chapter GetNextChapter(int bookid, int currentchapter,int currentpage)
21: { 22: return new Chapter(bookid,currentchapter,++currentpage,"Texbox begin with Txt ,ComboBox begin with Cmb","Naming Convention") ;
23: }
24: [WebMethod]
25: [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
26: public string test()
27: { 28: return "Tweweweweweu";
29: }
30: }
31: public class Chapter
32: { 33: public int BookID;
34: public int currentChapter;
35: public int page;
36: public string content;
37: public string chaptertitle;
38: public Chapter(int bookid, int currentchapter, int page, string content,string chaptertitle)
39: { 40: this.BookID = bookid;
41: this.currentChapter = currentchapter;
42: this.page = page;
43: this.content = content;
44: this.chaptertitle = chaptertitle;
45: }
46: public Chapter() { } 47: }
1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2:
3: <html xmlns="http://www.w3.org/1999/xhtml" application="manifest-file">
4: <head>
5: <title>c#</title>
6: </head>
7: <body>
8: <input type="hidden" id="bookid" value="123" />
9: <input type="hidden" id="currentchapter" value="1" />
10: <input type="hidden" id="currentpage" value="1" />
11: <div id="ChapterTitle"><h3 id="ChapterTitleBar">Introduction</h3></div>
12: <div id="ChapterContent" >
13: <textarea id="ChapterContentText" style="width:600px;height:600px">
14: Whether you picked up this book from the bookstore, ordered it from an online site, or just got lucky and got a free copy, you had some notion of what you expected to be inside of it from the moment you first laid eyes on it. We discussed the concept with the publisher and others and it was interesting to hear the various perceptions that people had. The actual intent of this book is not to teach you how to "hack" ASP.NET applications from a security perspective, nor is it a guide about how to hack together a poorly written application. Its intent is to give you insight into techniques that you can use to build and deliver real-life applications using ASP.NET.
15:
16: Now that we have clarified the book's simple purpose, we should probably explain more about what it is and why it should be one of the few books you buy and recommend to everyone you know. Each of the authors of this book is a seasoned professional and is experienced with the .NET platform. All of us are Microsoft MVPs (Most Valuable Professionals) and have answered thousands of ASP.NET developer questions in various online and offline communities. In this book we share our insights into solutions for many questions we answer all the time. You can benefit from the cumulative experience we have gained in building real-life applications. Additionally, with the release of ASP.NET 2.0, we discuss a whole set of new issues in print for the first time.
17:
18: This is not an introductory book on ASP.NET, as you can find several on the market already that provide an excellent overview and introduction to ASP.NET application development. Further, this is not intended to be a reference guide that explains every feature and option in ASP.NET. Again, between the numerous books that focus on providing a reference source and the ever-improving MSDN documentation, the sources of rich reference information are numerous.
19:
20: What Is a Hack?
21: We are using the term hacks to refer to little-known solutions, undocumented features, and tips and tricks. Some people call them hacks; others call them creative solutions. You might have your own name for them, but they are all basically the same thing. Every application of any significance pushes the capabilities of ASP.NET and uses some form of a hack as part of the overall solution.
22:
23: Some of the past hacks that you might recognize are page templates, multiple forms, URL rewriting, and SQL cache dependencies. These popular hacks have found their way into countless production applications. For each of these there are hundreds of other hacks that simply did not become as widely accepted, and therefore the community (meaning ASP.NET developers) suffered from the lack of opportunity. This book exposes several little-known, but useful hacks that you, as developers, can employ in production environments.
24: </textarea>
25: </div>
26: <input type="button" value="GetPreviousChapter" />
27: <input type="button" value="GetNextChapter" onclick="getnext();"/>
28: <input type="button" value="test" onclick="gettest();"/>
29:
30: <script type="text/javascript">
31:
32: function makePOSTRequest(url, parameters) { 33: http_request = false;
34: if (window.XMLHttpRequest) { // Mozilla, Safari,... 35: http_request = new XMLHttpRequest();
36: if (http_request.overrideMimeType) { 37: // set type accordingly to anticipated content type
38: // http_request.overrideMimeType('text/xml'); 39: http_request.overrideMimeType('text/html'); 40: }
41: } else if (window.ActiveXObject) { // IE 42: try { 43: http_request = new ActiveXObject("Msxml2.XMLHTTP"); 44: } catch (e) { 45: try { 46: http_request = new ActiveXObject("Microsoft.XMLHTTP"); 47: } catch (e) { 48: alert("not good at all.."); 49: }
50: }
51: }
52: if (!http_request) { 53: alert('Cannot create XMLHTTP instance'); 54: return false;
55: }
56:
57: http_request.onreadystatechange = confirmResponse;
58: http_request.open('POST', url, true); 59: http_request.setRequestHeader("Content-Type", "application/json"); 60: http_request.send(parameters);
61: }
62:
63: function confirmResponse() { 64: if (http_request.readyState == 4) { 65: if (http_request.status == 200) { 66: result = http_request.responseText;
67: DisplayResult(result);
68: alert('Response:\n\n' + result); 69: } else { 70: alert('There was a problem with the request.'); 71: }
72: }
73: }
74: function DisplayResult(result) { 75: var thejson= eval("(" + result+ ")"); 76: var dc = document.getElementById('ChapterContentText'); 77: var ct = document.getElementById('ChapterTitleBar'); 78: dc.innerHTML = thejson.d.content;
79: ct.innerHTML = thejson.d.chaptertitle;
80: }
81:
82: function getnext() { 83: var url = 'http://localhost:9999/WebSite2/WebService.asmx/GetNextChapter';
84: var bookid = document.getElementById('bookid').defaultValue.toString(); 85: var currentchapter = document.getElementById('currentchapter').defaultValue.toString(); 86: var currentpage = document.getElementById('currentpage').defaultValue.toString(); 87: var string="{ 'bookid': " + bookid + ", 'currentchapter': " + currentchapter + ", 'currentpage': " +currentpage + "}"; 88: var request = makePOSTRequest(url, string);
89: }
90: function gettest() { 91: var url = 'http://localhost:9999/WebSite2/WebService.asmx/test';
92: var request = makePOSTRequest(url, "");
93: }
94: </script>
95: </body>
96: </html>
Now when you click next chapter it's going to get from the web service.
To consume it json from a raw string notice the eval function.
notice how to pass the parameter to the web service it's pattern {'paramname':valueifinteger,'paramname2','valueifstring'}
27 November 2008
Or you can use jquery plugin,
$(document).ready(function() {
$.ajaxDotNet('WebService.asmx/GetNextChapter',
{
verb: "POST", data: {param1,param2,..n},
error: function(d){alert(d.responseText);},
success: function(obj) {
//do what you want //$("#Text1").attr('value',obj.d);
}
});
});