"http://www.w3.org/1999/xhtml">"Server"> <title></title> <script src=".. /scripts/jquery-2.1.1.js"></script> <script type="Text/javascript">//1. Using the Pagemethod methodfunction GetBankName1 () {varAccount = document.getElementById ("TextBox1"). Value; Pagemethods.getbanknamebyaccount (account, onsucceed); } function Onsucceed (result) {document.getElementById ("Label1"). InnerText =result; } //2. Use Ajax for a refreshfunction GetBankName2 () {varAccount = $ ("#TextBox1"). Val (); $.ajax ({type:"Post", URL:"Getdatafromdb.aspx/getbanknamebyaccount", //data: "{' Account ': '" + account + "', ' Name ': '" + 456 + "'}",Data"{' account ': '"+ account +"'}", ContentType:"Application/json; Charset=utf-8", DataType:"JSON", Success:function (data) {document.getElementById ("Label1"). InnerText =DATA.D; } }); } //3. Using a generic XML requestfunction GetBankName3 () {varXMLHTTP; if(window. XMLHttpRequest) {XMLHTTP=NewXMLHttpRequest (); } Else{XMLHTTP=NewActiveXObject ("Microsoft.XMLHTTP"); } Xmlhttp.onreadystatechange=function (data) {if(Xmlhttp.readystate = =4&& Xmlhttp.status = = $) { //document.getElementById ("Label1"). InnerText = Xmlhttp.responsetext;alert (data); } } varAccount = document.getElementById ("TextBox1"). Value; Xmlhttp.open ("Post","Getdatafromdb.aspx/getname",true); Xmlhttp.send (); } </script>"Form1"runat="Server"> <div> <asp:label id="Label2"runat="Server"text="Label"></asp:Label> <asp:scriptmanager id="ScriptManager1"runat="Server"enablepagemethods="true"></asp:ScriptManager>Account Number :<asp:textbox id="TextBox1"runat="Server"></asp:textbox><br/>Bank Name:<asp:label id="Label1"runat="Server"text="Label"></asp:Label> <br/> <input id="Button1"Type="Button"Value="Use Page Method"onclick="GetBankName1 ();"/><br/> <input id="Button2"Type="Button"Value="Use AJAX"onclick="GetBankName2 ();"/> <input id="Button3"Type="Button"Value="Use XML Request"onclick="GetBankName3 ();"/> </div> </form></body>Background code:
Public Partial classGetDataFromD:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) {Label2.Text= DateTime.Now.ToString ();//detects if a local refresh is implemented} [WebMethod] Public Static stringGetbanknamebyaccount (intAccount ) { stringBankname =""; using(SqlConnection conn =NewSqlConnection (configurationmanager.connectionstrings["ConnStr"]. ConnectionString)) {Conn. Open (); stringsql ="Select Name from Customers where [email protected]"; SqlCommand cmd=NewSqlCommand (Sql,conn); SqlParameter P=NewSqlParameter ("@Account", account); Cmd. Parameters.Add (P); using(SqlDataReader r =cmd. ExecuteReader ()) {if(R.read ()) {Bankname= r[0]. ToString (); } } } returnBankname; } [WebMethod] Public Static stringGetName () {return "Hello"; } }Implement Ajax local refresh and the use of Pagemethod methods