Introduction to ASP. NET client callback

Source: Internet
Author: User

Recently, I want to create a project: music player. General UI:

Click the "X" icon on the right to delete the selected song. Because the real interface code is copied, And the predecessors use concatenated strings, and then bound to the page. The ASP. NET Server Control is not used, so you need to click the icon and delete the event, which is different from what we usually do. I think there are three solutions to solve this problem.

1. The worst way is to present the UI interface using the GridView. The GridView is a server control, so the last column can be ImageButton, and then there will be a server control event. The solution is to delete the original HTML code and then re-write the code. This method is used by the students themselves. This method is easy to understand and modify.

2. AJAX + Handler is used to call an asp.net handler for processing. The advantage of handler processing is that the structure and code are separated, which is also easy to accept.

3. Use the callback function of ASP. NET. Specific reference: client callback implementation (C #) Sample http://msdn.microsoft.com/zh-cn/library/ms178210.aspx.

In the MSDN instance, the client callback provides a solution: the project needs to use the JavaScr client to operate the C # code in the background, and then it implements these principles at the underlying layer, these principles are encapsulated, And we can implement them according to the interfaces and structures provided by them.

<Html xmlns = "http://www.w3.org/1999/xhtml"> 
Result is the HTML code segment generated in the background.
Background instance code:
Public partial class TestPage: System. web. UI. page, System. web. UI. ICallbackEventHandler {protected void Page_Load (object sender, EventArgs e) {String cbReference = Page. clientScript. getCallbackEventReference (this, "arg", "ReceiveServerData", "context"); // callback JavaScript String callbackScript; callbackScript = "function CallServer (arg, context) "+" {"+ cbReference +" ;}"; // Add a javas code snippet Page to the Page. clientScript. registerClientScriptBlock (this. getType (), "CallServer", callbackScript, true); BindData ();} protected string returnValue; public String result = "<table>"; private void BindData () {DataTable mytable = new DataTable (); mytable = GenerateData (); for (int I = 0; I <mytable. rows. count; I ++) {// The value of column 0th of row I // result = mytable. rows [I] [0]. toString (); result + = "<tr> <td>" + mytable. rows [I] [0]. toString () + "</td>"; result + = "<td>" + mytable. rows [I] [1]. toString () + "</td>"; result + = "<td>" + mytable. rows [I] [2]. toString () + "</td>"; result + = "<td>" + mytable. rows [I] [3]. toString () + "</td>"; result + = "<td> <input type = 'button 'onclick = 'deleteit (this ); 'value = 'delete'/> </td> </tr> ";} result + =" </table> ";} // Generate the data in memory. protected DataTable GenerateData (){
...... } Public void RaiseCallbackEvent (String eventArgument) {// Delete the database in actual conditions. A String is returned for demonstration. ReturnValue = "deleted" + eventArgument + "record successful! ";}Public string GetCallbackResult () {return returnValue ;}}
The RaiseCallbackEvent (String eventArgumnet) and GetCallbackResult () methods are used to implement the ICallbackEventHandler interface. ,
Through this method, the interaction between the client and the server can be completed. Of course, in this instance, the page still needs to be refreshed to re-render the page.
This is my personal idea. I don't know if I have made any mistakes. Please give me some advice. Thank you. Instance code: http://www.bkjia.com/uploadfile/2011/1025/20111025024028619.zip

Author: Happy gossip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.