Client callback implementation (C #) Example

Source: Internet
Author: User

Demonstrate ASP. NET web pages that implement client callback. For more information, see implement client callback programmatically on ASP. NET web pages without sending back.

Example

The following sample code is divided into two parts. The first part of the example demonstrates an ASP. NET webpage (. aspx page ). The second part demonstrates the corresponding code hidden file (. aspx. cs file ).

Code

C # Copy code
<%@ Page Language="C#" AutoEventWireup="true"            CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %>            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML             1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">            

C # Copy code
using System;            using System.Data;            using System.Configuration;            using System.Collections;            using System.Web;            using System.Web.Security;            using System.Web.UI;            using System.Web.UI.WebControls;            using System.Web.UI.WebControls.WebParts;            using System.Web.UI.HtmlControls;            public partial class ClientCallback : System.Web.UI.Page,            System.Web.UI.ICallbackEventHandler            {            protected System.Collections.Specialized.ListDictionary catalog;            protected String returnValue;            protected void Page_Load(object sender, EventArgs e)            {            String cbReference =            Page.ClientScript.GetCallbackEventReference(this,            "arg", "ReceiveServerData", "context");            String callbackScript;            callbackScript = "function CallServer(arg, context)" +            "{ " + cbReference + ";}";            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),            "CallServer", callbackScript, true);            catalog = new System.Collections.Specialized.ListDictionary();            catalog.Add("monitor", 12);            catalog.Add("laptop", 10);            catalog.Add("keyboard", 23);            catalog.Add("mouse", 17);            ListBox1.DataSource = catalog;            ListBox1.DataTextField = "key";            ListBox1.DataBind();            }            public void RaiseCallbackEvent(String eventArgument)            {            if (catalog[eventArgument] == null)            {            returnValue = "-1";            }            else            {            returnValue = catalog[eventArgument].ToString();            }            }            public String GetCallbackResult()            {            return returnValue;            }            }            

Note

This web page simulates a database query to determine the supply or inventory quantity of a series of products (such as monitors and keyboards. To simplify this code example, the database is represented by a dictionary list containing a small number of items. For each item in the table, the key is the item name (such as monitor), and the value is the number of items in stock. However, the database will be used in the finished application.

When running this page, Control is bound to the hash table, Control to display the product list. This page also contains a button element (not a Button Web server control), and its onclick event is bound to a client function named LookUpStock. When you click a button, this button executes the LookUpStock function. This function obtains the selected content from the list box and calls the CallServer function to perform client callback.

The Code hides the page through Method to add a client script to this page. The script added to this page contains a function called CallServer, which is used to obtain Name of the method sent back to the server.

The client callback will call Method to Determine the available inventory of the products passed to it. Method returns this value. Note that the parameters sent between the client script and the server code can only be strings. To pass in or receive multiple values, you can concatenate these values in the input string or return string.

Security description:

This feature has potential security threats. Some insecure factors exist because callback parameters are not verified. You must check the parameters before using them. For more information, see script intrusion overview.

See how to implement client callback programmatically without sending back in ASP. NET webpage: client callback example with verification implementation in ASP. NET webpage

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.