This example illustrates how to:
- Define a custom editor (for example, the CheckBox) inside the column's DateItemTemplate (via the GridViewSettings.SetDataItemTemplateContent method);
- Specify the CheckBox.Name property based on the corresponding DataRow's keyValue (this operation requires specifying the GridViewSettings.KeyFieldName property);
- Subscribe the client-side ASPxClientCheckBox.CheckedChanged event;
- Store the CheckBox's state via a custom JavaScript object like a dictionary/key value pairs (the CheckBox.Name - Checked State)
An end-user is able to select/deselect the CheckBoxes displayed within the column.
When the user tries to perform any GridView's callback (sorting, paging, etc.), the stored key value pairs collection is serialized via the "toJSON" plugin (available with the jquery-json library). The serialized object is passed to a Controller as custom argument according to the "Passing Values to Controller Action Through Callbacks" technique.
The passed serialized object is de-serialized on the Controller side and passed to the View object to restore an unbound CheckBox state.
View:
[JScript]<script type="text/javascript">var DXHiddenObject = {};function OnBeginCallback(s, e){ e.customArgs["items"] = $.toJSON(DXHiddenObject);}function OnCheckedChanged(s, e){if(s.GetChecked()) DXHiddenObject[s.name] = true;elsedelete DXHiddenObject[s.name];}</script>
PartialView:
C#:
[C#]@Html.DevExpress().GridView(settings=>{settings.Name="gvTypedListDataBinding";settings.CallbackRouteValues=new{Controller="Home",Action="TypedListDataBindingPartial"};settings.KeyFieldName="ID";...settings.Columns.Add(column=>{column.SetDataItemTemplateContent(c=>{Html.DevExpress().CheckBox(checkboxSettings=>{checkboxSettings.Name="cb_"+c.KeyValue.ToString();checkboxSettings.Properties.ClientSideEvents.CheckedChanged="OnCheckedChanged";if(ViewData["items"]!=null){Dictionary<string,bool>items=(Dictionary<string,bool>)ViewData["items"];checkboxSettings.Checked=items.ContainsKey(checkboxSettings.Name)&&(bool)items[checkboxSettings.Name];}}).Render();});});settings.ClientSideEvents.BeginCallback="OnBeginCallback";}).Bind(Model).GetHtml()
VB.NET:
[VB.NET]@Html.DevExpress().GridView( _Sub(settings) settings.Name = "gvTypedListDataBinding" settings.CallbackRouteValues = NewWith {Key .Controller = "Home", Key .Action = "TypedListDataBindingPartial"} settings.KeyFieldName = "ID" ... settings.Columns.Add( _Sub(column) column.SetDataItemTemplateContent( _Sub(c) Html.DevExpress().CheckBox( _Sub(checkboxSettings) checkboxSettings.Name = "cb_" + c.KeyValue.ToString() checkboxSettings.Properties.ClientSideEvents.CheckedChanged = "OnCheckedChanged"If (ViewData("items") IsNot Nothing) ThenDim items As Dictionary(OfString, Boolean) = CType(ViewData("items"), Dictionary(OfString, Boolean)) checkboxSettings.Checked = items.ContainsKey(checkboxSettings.Name) AndAlsoCBool(items(checkboxSettings.Name))EndIfEndSub).Render()EndSub)EndSub) settings.ClientSideEvents.BeginCallback = "OnBeginCallback"EndSub).Bind(Model).GetHtml()
Controller:
[C#]publicclassHomeController:Controller{publicActionResultIndex(){if(Session["TypedListModel"]==null)Session["TypedListModel"]=InMemoryModel.GetTypedListModel();returnView(Session["TypedListModel"]);}publicActionResultTypedListDataBindingPartial(){ViewData["items"]=GetSerializedObject(Request["items"]);returnPartialView(Session["TypedListModel"]);}privateDictionary<string,bool>GetSerializedObject(stringinputString){if(string.IsNullOrEmpty(inputString))returnnull;Dictionary<string,bool>items=null;try{items=newJavaScriptSerializer().Deserialize<Dictionary<string,bool>>(inputString);}catch{ViewData["ErrorMessage"]="Invalid Input JSON String";}returnitems;}}
VB.NET
[VB.NET]PublicClass HomeControllerInherits ControllerPublicFunction Index() As ActionResultIf Session("TypedListModel") IsNothingThen Session("TypedListModel") = InMemoryModel.GetTypedListModel()EndIfReturn View(Session("TypedListModel"))EndFunctionPublicFunction TypedListDataBindingPartial() As ActionResult ViewData("items") = GetSerializedObject(Request("items"))Return PartialView(Session("TypedListModel"))EndFunctionPrivateFunction GetSerializedObject(ByVal inputString AsString) As Dictionary(OfString, Boolean)IfString.IsNullOrEmpty(inputString) ThenReturnNothingEndIfDim items As Dictionary(OfString, Boolean) = NothingTry items = New JavaScriptSerializer().Deserialize(Of Dictionary(OfString, Boolean))(inputString)Catch ViewData("ErrorMessage") = "Invalid Input JSON String"EndTryReturn itemsEndFunctionEndClass
Question Comments
Added By: Leonardo Fucci at: 1/14/2013 5:26:07 AM
Ok, according to this example, I can select and memorize what i ́ve selected on the checkboxes... but how can i send selected items to controller when a Submit button in the form is pressed.
I ́m trying to do this without success cause saved items are in client side and, for some reasson or another, it is impossible to send them to server side, also when you change page numbers, it looses checked information.
Can you explain how to submit checked information to controller?
Thanks.
Hi Can you please suggect how can i use the same approach for a coloredit column in a an mvc gridview.
Added By: Himanshu Agarwal 2 at: 4/12/2015 12:10:03 AMEven my question is same as of Leonardo that how can we send the selected values to the controller side on submit button click.