How to Access Server Data on the Client Side

  • 5 min 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

<dx:ASPxMemo ID="mShortDesc"  ClientInstanceName="mShortDescClient" runat="server">
//After a property has been specified, you can access its value on the client side.
var i = mShortDescrClient.cpMyAttribute;
//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.

<dx:ASPxGridView ID="gvProducts" runat="server" ClientInstanceName="gvProducts" AutoGenerateColumns="False"
     DataSourceID="dsProducts" KeyFieldName="ProductID" OnCustomButtonCallback="gvProducts_CustomButtonCallback"
     OnInit="gvProducts_Init" OnRowDeleting="gvProducts_RowDeleting">
     <ClientSideEvents EndCallback="gvProducts_EndCallback" />
     ...
</dx:ASPxGridView>
<dx:ASPxPopupControl ID="pcConfirm" runat="server" ClientInstanceName="pcConfirm"
     Modal="True" PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter"
     HeaderText="Row Deleting">
     <ContentCollection>
          <dx:PopupControlContentControl runat="server" SupportsDisabledAttribute="True">
               <table width="100%">
                    <tr>
                         <td colspan="2" align="center">
                              Delete Row?
                         </td>
                    </tr>
                    <tr>
                         <td align="center">
                              <a href="javascript:Yes_Click()">Yes</a>
                         </td>
                         <td align="center">
                              <a href="javascript:No_Click()">No</a>
                         </td>
                    </tr>
               </table>
          </dx:PopupControlContentControl>
     </ContentCollection>
</dx:ASPxPopupControl>
function gvProducts_EndCallback(s, e) {
     if (s.cpShowDeleteConfirmBox)
          pcConfirm.Show();
}
function Yes_Click() {
     pcConfirm.Hide();
     gvProducts.DeleteRow(gvProducts.cpRowIndex);            
}
function No_Click() {
     pcConfirm.Hide()
}
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

<dx:ASPxGridView ID="myGrid" ClientInstanceName="myClientGrid" ...
</dx:ASPxGridView>
function ReadJSON() {
    var obj = JSON.parse(myClientGrid.cpJSON)
    alert(obj.x + obj.y);
}
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

WebForms:

<dx:ASPxGridView ID="grid" OnCustomJSProperties="GridView_CustomJSProperties" ...>
    ...
</dx:ASPxGridView>
protected void GridView_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e) {
    e.Properties["cpVisibleRowCount"] = grid.VisibleRowCount;
    e.Properties["cpFilteredRowCountWithoutPage"] = GetFilteredRowCountWithoutPage();
}
function GetSelectedFilteredRowCount() {
    return grid.cpFilteredRowCountWithoutPage + grid.GetSelectedKeysOnPage().length;
}

MVC:

settings.CustomJSProperties = (s, e) => {
    var grid = s as MVCxGridView;
    e.Properties["cpVisibleRowCount"] = grid.VisibleRowCount;
    e.Properties["cpFilteredRowCountWithoutPage"] = GetFilteredRowCountWithoutPage(grid);
};
function GetSelectedFilteredRowCount() {
  return grid.cpFilteredRowCountWithoutPage + grid.GetSelectedKeysOnPage().length;
}

Online Examples

Online Demo

See Also