Skip to main content
A newer version of this page is available. .

How to Access Server Data on the Client Side

  • 6 minutes to read

DevExpress ASP.NET controls provide two special members that allow you to pass data from the server side to the client side: the JSProperties Property and the CustomJSProperties Event (see the List of Controls with the JSProperties and CustomJSProperties Members topic).

These members allow you to pass a collection of name/value pairs form the server side to the client side (but not vice versa). The only requirement is that property names must begin with the ‘cp‘ prefix, to avoid rewriting a control’s base properties. The supported value types are listed below:

  • null
  • DBNull
  • ValueType
  • string
  • IDictionary
  • IEnumerable

JSProperties Property

The JSProperties property represents a collection of property names and their values, enabling you to declare temporary client properties. A property can be accessed on the client once it is declared.

Declaration

//Specify a new property on the server side.
mShortDesc.JSProperties["cpMyAttribute"] = "1"

Example 1

Note

The complete sample project is available in the DevExpress Code Central database at http://www.devexpress.com/example=E4126. Depending on the target platform type (ASP.NET, WinForms, etc), you can either run this example online or download an auto-executable sample.

This example demonstrates how you can perform server-side actions when a user clicks the “Delete” command button, and then show the Delete confirmation PopupControl.

using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
...
protected void gvProducts_Init(object sender, EventArgs e) {
     ASPxGridView gridView = sender as ASPxGridView;
     gridView.JSProperties["cpShowDeleteConfirmBox"] = false;
}
protected void gvProducts_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e) {
     ASPxGridView gridView = sender as ASPxGridView;
     if (e.ButtonID == "btDelete") {
          //Server-side actions performed before showing popup here
          gridView.JSProperties["cpRowIndex"] = e.VisibleIndex;
          gridView.JSProperties["cpShowDeleteConfirmBox"] = true;
     }
}
protected void gvProducts_RowDeleting(object sender, ASPxDataDeletingEventArgs e) {
     //Custom row deleting code here
     throw new Exception("Data modifications are not allowed.");
     e.Cancel = true;
}

Example 2

How to send a JSON object to the client using the JSProperties property

Since a JSON object has a text format, you can use the JSProperties property ( or CustomJSProperties event) to send it as a string to the client side. Then, you can parse the string to get the object.

JSProperties

myGrid.JSProperties["cpJSON"] = "{ \"x\": \"Hello, \", \"y\": \"World!\" }";

CustomJSProperties Event

The CustomJSProperties event fires each time a control callback or page postback is sent to the server side. The event enables you to declare temporary client properties using the event parameter’s CustomJSPropertiesEventArgs.Properties property, which is a collection of property names and their values. A property can be accessed on the client after it has been declared.

Note

In most cases, it is more efficient to use the JSProperties property rather than handle the CustomJSProperties event, which is primarily declared for backwards compatibility.

Example

The example demonstrates how to determine if ASPxGridView is in the new row editing mode on the client side. The CustomJSProperties event is used to pass the required information from the server side to the client side.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web;

public partial class _Default : System.Web.UI.Page
{
    protected void ASPxGridView1_CustomJSProperties(object sender, DevExpress.Web.ASPxGridViewClientJSPropertiesEventArgs e) {
        e.Properties["cpIsNewRowEditing"] = (sender as ASPxGridView).IsNewRowEditing.ToString();
    }
}
See Also