Back to Devexpress

Passing Values from the Server to the Client

aspnet-11816-common-concepts-client-side-functionality-passing-values-between-client-and-server-sides.md

latest16.1 KB
Original Source

Passing Values from the Server to the Client

  • Mar 04, 2025
  • 5 minutes to read

DevExpress ASP.NET controls provide two members that allow you to pass data from the server side to the client side: the JSProperties property and the CustomJSProperties event. These members allow you to pass a collection of name/value pairs from the server side to the client side (but not vice versa).

Run Demo: ASPxGridView - Select All Rows

How to Access Server Data on the Client Side

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

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

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

JSProperties Property

The JSProperties property is a collection of property names and values for which you can declare temporary client properties. A property can be accessed on the client side once it is declared.

Declaration

aspx
<dx:ASPxMemo ID="mShortDesc" ClientInstanceName="mShortDescClient" runat="server">
csharp
// Specify a new property on the server side.
mShortDesc.JSProperties["cpMyAttribute"] = "1"
vb
' Specify a new property on the server side.
mShortDesc.JSProperties("cpMyAttribute") = "1"
javascript
// After a property has been specified, you can access its value on the client side.
var i = mShortDescrClient.cpMyAttribute;

Example: How to show the Deleting Confirmation box after performing some server-side actions

View Example

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.

aspx
<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>
csharp
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;
}
vb
Imports DevExpress.Web.ASPxGridView
Imports DevExpress.Web.Data
' ...
Protected Sub gvProducts_Init(ByVal sender As Object, ByVal e As EventArgs)
     Dim gridView As ASPxGridView = TryCast(sender, ASPxGridView)
     gridView.JSProperties("cpShowDeleteConfirmBox") = False
End Sub
Protected Sub gvProducts_CustomButtonCallback(ByVal sender As Object, ByVal e As ASPxGridViewCustomButtonCallbackEventArgs)
     Dim gridView As ASPxGridView = TryCast(sender, ASPxGridView)
     If e.ButtonID = "btDelete" Then
          ' Server-side actions performed before showing popup here
          gridView.JSProperties("cpRowIndex") = e.VisibleIndex
          gridView.JSProperties("cpShowDeleteConfirmBox") = True
     End If
End Sub
Protected Sub gvProducts_RowDeleting(ByVal sender As Object, ByVal e As ASPxDataDeletingEventArgs)
     ' Custom row deleting code here
     Throw New Exception("Data modifications are not allowed.")
     e.Cancel = True
End Sub
javascript
function gvProducts_EndCallback(s, e) {
     if (s.cpShowDeleteConfirmBox)
          pcConfirm.Show();
}
function Yes_Click() {
     pcConfirm.Hide();
     gvProducts.DeleteRow(gvProducts.cpRowIndex);            
}
function No_Click() {
     pcConfirm.Hide()
}

Example: 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.

aspx
<dx:ASPxGridView ID="myGrid" ClientInstanceName="myClientGrid" runat="server"/>
    <!-- ... -->
</dx:ASPxGridView>
csharp
myGrid.JSProperties["cpJSON"] = "{ \"x\": \"Hello, \", \"y\": \"World!\" }";
vb
myGrid.JSProperties("cpJSON") = "{ ""x"": ""Hello, "", ""y"": ""World!"" }"
javascript
function ReadJSON() {
    var obj = JSON.parse(myClientGrid.cpJSON)
    alert(obj.x + obj.y);
}

CustomJSProperties Event

The CustomJSProperties event fires each time a control callback or page postback is sent to the server side. This event enables you to declare temporary client properties via the event parameter’s CustomJSPropertiesEventArgs.Properties property (a collection of property names and values). A property becomes accessible on the client after it is declared.

Note

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

Example

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

List of Controls with JSProperties and CustomJSProperties Members

ControlJSProperties PropertyCustomJSProperties Event
ASPxButtonASPxButton.JSPropertiesASPxButton.CustomJSProperties
ASPxCallbackASPxCallback.JSPropertiesASPxCallback.CustomJSProperties
ASPxCallbackPanelASPxPanelBase.JSPropertiesASPxPanelBase.CustomJSProperties
ASPxCloudControlASPxCloudControl.JSPropertiesASPxCloudControl.CustomJSProperties
ASPxDataViewASPxDataViewBase.JSPropertiesASPxDataViewBase.CustomJSProperties
ASPxDiagramASPxDiagram.JSPropertiesASPxDiagram.CustomJSProperties
ASPxDockPanelASPxPopupControlBase.JSPropertiesASPxPopupControlBase.CustomJSProperties
ASPxFileManagerASPxFileManager.JSPropertiesASPxFileManager.CustomJSProperties
ASPxFilterControlASPxFilterControl.JSPropertiesASPxFilterControlBase.CustomJSProperties
ASPxGanttASPxGantt.JSPropertiesASPxGantt.CustomJSProperties
ASPxGaugeControlASPxGaugeControl.JSPropertiesASPxGaugeControl.CustomJSProperties
ASPxGridViewASPxGridBase.JSPropertiesASPxGridView.CustomJSProperties
ASPxHtmlEditorASPxHtmlEditor.JSPropertiesASPxHtmlEditor.CustomJSProperties
ASPxLoadingPanelASPxLoadingPanel.JSPropertiesASPxLoadingPanel.CustomJSProperties
ASPxMenuASPxMenuBase.JSPropertiesASPxMenuBase.CustomJSProperties
ASPxNavBarASPxNavBar.JSPropertiesASPxNavBar.CustomJSProperties
ASPxNewsControlASPxDataViewBase.JSPropertiesASPxDataViewBase.CustomJSProperties
ASPxPageControlASPxTabControlBase.JSPropertiesASPxTabControlBase.CustomJSProperties
ASPxPanelASPxPanelBase.JSPropertiesASPxPanelBase.CustomJSProperties
ASPxRoundPanelASPxPanelBase.JSPropertiesASPxPanelBase.CustomJSProperties
ASPxPivotGridASPxPivotGrid.JSPropertiesASPxPivotGrid.CustomJsProperties
ASPxPopupControlASPxPopupControlBase.JSPropertiesASPxPopupControlBase.CustomJSProperties
ASPxSchedulerASPxScheduler.JSPropertiesASPxScheduler.CustomJSProperties
ASPxSpellCheckerASPxSpellChecker.JSPropertiesASPxSpellChecker.CustomJSProperties
ASPxTabControlASPxTabControlBase.JSPropertiesASPxTabControlBase.CustomJSProperties
ASPxTimerASPxTimer.JSPropertiesASPxTimer.CustomJSProperties
ASPxTitleIndexASPxTitleIndex.JSPropertiesASPxTitleIndex.CustomJSProperties
ASPxTreeListASPxTreeList.JSPropertiesASPxTreeList.CustomJSProperties
ASPxTreeViewASPxTreeView.JSPropertiesASPxTreeView.CustomJSProperties
ASPxUploadControlASPxUploadControl.JSPropertiesASPxUploadControl.CustomJSProperties
EditorsASPxEditBase.JSPropertiesASPxEditBase.CustomJSProperties