aspnet-11816-common-concepts-client-side-functionality-passing-values-between-client-and-server-sides.md
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
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:
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.
<dx:ASPxMemo ID="mShortDesc" ClientInstanceName="mShortDescClient" runat="server">
// Specify a new property on the server side.
mShortDesc.JSProperties["cpMyAttribute"] = "1"
' Specify a new property on the server side.
mShortDesc.JSProperties("cpMyAttribute") = "1"
// After a property has been specified, you can access its value on the client side.
var i = mShortDescrClient.cpMyAttribute;
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>
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;
}
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
function gvProducts_EndCallback(s, e) {
if (s.cpShowDeleteConfirmBox)
pcConfirm.Show();
}
function Yes_Click() {
pcConfirm.Hide();
gvProducts.DeleteRow(gvProducts.cpRowIndex);
}
function No_Click() {
pcConfirm.Hide()
}
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.
<dx:ASPxGridView ID="myGrid" ClientInstanceName="myClientGrid" runat="server"/>
<!-- ... -->
</dx:ASPxGridView>
myGrid.JSProperties["cpJSON"] = "{ \"x\": \"Hello, \", \"y\": \"World!\" }";
myGrid.JSProperties("cpJSON") = "{ ""x"": ""Hello, "", ""y"": ""World!"" }"
function ReadJSON() {
var obj = JSON.parse(myClientGrid.cpJSON)
alert(obj.x + obj.y);
}
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.
<dx:ASPxGridView ID="grid" OnCustomJSProperties="GridView_CustomJSProperties" runat="server"/>
<!-- ... -->
</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;
}