This articleArticleIt will help you how to implement the client to call the method defined in the background. Jquery makes life easier. For it, there is a very simple way to achieve this goal.
(Note: The purpose of this article is to explain how to use jquery to request a defined method in the background, rather than directly using background functions. There are many request backend methods. This is only one of the methods for the client to interact with the server. Of course, there are also popular WebServices. Some comments have in-depth discussions on the title of this article, so I would like to explain it here !)
Earlier, we knew a method: if we want to call a function added in the background from the client, we need to write something called webmethod. Now, through the following method, we will no longer need to use webmethod.
In the following example, I will write a method to delete a specified user when you click the delete key. I don't want to send back (PostBack, web pages send data back to the process on the same page on the server .), Therefore, you want to execute this event from the client. I wrote a method in the CS file on the server and called it on the client through jquery. This page is called test. aspx. This method is called deleterec ().
1 Private Void Deleterec () 2 { 3 Int Id = request. Form [ " ID " ]. Tostring (). tointeger (); 4 // Parameter send from client side 5 Int Userid = request. Form [ " Userid " ]. Tostring (). tointeger (); 6 // Parameter send from client side 7 Userbo lobjuserbo = New Userbo (); 8 Lobjuserbo. deleteuser (ID, userid ); 9 }
The following describes how to call this method:
1 Protected Void Page_load ( Object Sender, eventargs E) 2 { 3 If (! Page. ispostback) 4 { 5 # Region Ajax Methods 6 If (Request. Form [ " Methodname " ] = " Deleter " ) 7 // Same method name that we are specifying on client side (deleter) 8 { 9 Deleterec (); // Method defined on the page to delete the record 10 Return ; 11 } 12 # Endregion 13 } 14 }
This is what we need to add in the client (test. aspx.
1 <AID= "Adelete"Href= "Java <! -- No --> Script: void (0 );">Delete</A>
The script is required to call the server method when we click the anchor tag.
1 $ ( ' # Adelete ' ). Click (function () 2 { 3 VaR Datatosend = {ID: ID, methodname: ' Deleter ' , Userid: userid }; 4 VaR Options = 5 { 6 URL: ' <% = Resolveurl ("~ /Test. aspx ") %>? X = ' + New Date (). gettime (), 7 Data: datatosend, 8 Datatype: ' JSON ' , 9 Type: ' Post ' , 10 Success: function (response ){ 11 Window. Location. href = ' <% = Resolveurl ("~ /Test1.aspx ") %>/ ' + ID; 12 // After success will redirect to new page 13 } 14 } 15 $. Ajax (options );
We hope the above will call the server from the clientCodeIt is helpful.