I have a user control which contains a WHDG. This grid has a few basic properties set in the .aspx file. All of the columns, behaviors, editorproviders, etc... are added in the code behind.
Things work great the first time the page loads, however on a postback, such as a Save button click, the grid gets reloaded but all of the editors seem to get out of whack.
One post recommends applying the behaviors to the Grid.GridView, however this doesn't seem to work. When I try this, none of the EditingBehaviors appear to be getting created, even on the first page load.
Another post suggests that Editors/Behaviors added at runtime need to be reinstaniated on each postback. Following this approach doesn't seem to make any difference.
When I view source on the first grid and compare against the same grid after postback, everything seems to be there. However there is additional HTML that appears to be setting my editors to hidden. Which means something must be triggering the HierarchicalGridRender to emit this additional HTML. Either I am missing a property or something is null causing the HTML to get triggered during rendering.
...
I've stripped this example down in hopes an answer might be easier to come by.
BoundDataField boundDataField = new BoundDataField(true); boundDataField.DataFieldName = "UnitId"; boundDataField.Key = "UnitId"; boundDataField.Header.Text = ""; boundDataField.DataType = ColumnDataType.Int; boundDataField.Width = new Unit(150); boundDataField.Hidden = false; boundDataField.DataFormatString = "";
if (MyGrid.GridView.Columns[boundDataField.Key] == null) { MyGrid.GridView.Columns.Add(boundDataField); }
BoundDataField boundDataField2 = new BoundDataField(true); boundDataField2.DataFieldName = "Status"; boundDataField2.Key = "Status"; boundDataField2.Header.Text = "Status"; boundDataField2.DataType = ColumnDataType.String; boundDataField2.Width = new Unit(150); boundDataField2.Hidden = false; boundDataField2.DataFormatString = "";
if (MyGrid.GridView.Columns[boundDataField2.Key] == null) { MyGrid.GridView.Columns.Add(boundDataField2); }
//ACTIVATION MyGrid.Behaviors.CreateBehavior<Infragistics.Web.UI.GridControls.Activation>(); MyGrid.Behaviors.Activation.Enabled = true;
//EDITING CORE MyGrid.Behaviors.CreateBehavior<Infragistics.Web.UI.GridControls.EditingCore>(); MyGrid.Behaviors.EditingCore.Enabled = true; MyGrid.Behaviors.EditingCore.AutoCRUD = false; MyGrid.Behaviors.EditingCore.BatchUpdating = true; MyGrid.Behaviors.EditingCore.EnableInheritance = true;
//CELL EDITING MyGrid.Behaviors.EditingCore.Behaviors.CreateBehavior<Infragistics.Web.UI.GridControls.CellEditing>(); MyGrid.Behaviors.EditingCore.Behaviors.CellEditing.Enabled = true; MyGrid.Behaviors.EditingCore.Behaviors.CellEditing.EditModeActions.EnableOnActive = true; MyGrid.Behaviors.EditingCore.Behaviors.CellEditing.EditModeActions.EnableOnKeyPress = true; MyGrid.Behaviors.EditingCore.Behaviors.CellEditing.EditModeActions.MouseClick = EditMouseClickAction.Single;
//DEFINE DROPDOWN ITEMS List<DIYDropDownItem> items = new List<DIYDropDownItem>(); items.Add(new DIYDropDownItem("Status1", "A", false)); items.Add(new DIYDropDownItem("Status2", "M", false)); items.Add(new DIYDropDownItem("Status3", "D", false)); items.Add(new DIYDropDownItem("Status4", "I", false));
List<DropDownItem> ddi = new List<DropDownItem>();
foreach (DIYDropDownItem item in items) { ddi.Add(CreateDropDownItem(item.Text, item.Value, item.Selected)); }
//DEFINE DROPDOWN PROVIDER DropDownProvider dropDown = new DropDownProvider(); dropDown.ID = "DropDownProvider1"; dropDown.EditorControl.DisplayMode = Infragistics.Web.UI.ListControls.DropDownDisplayMode.DropDownList; dropDown.EditorControl.TextField = "Status"; dropDown.EditorControl.ViewStateMode = System.Web.UI.ViewStateMode.Enabled; dropDown.EditorControl.EnableViewState = true;
foreach (DropDownItem ddi2 in ddi) { dropDown.EditorControl.Items.Add(ddi2); }
//ASSIGN DROPDOWN PROVIDER TO GRID'S EDITORPROVIDERS COLLECTION MyGrid.EditorProviders.Add(dropDown);
//LINK EDITOR TO A COULMN VIA COLUMN SETTING EditingColumnSetting setting = new EditingColumnSetting(); setting.ColumnKey="Status"; setting.EditorID = "DropDownProvider1"; //ADD SETTING TO GRID MyGrid.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(setting);
//UPDATE BEHAVIORS TO ENSURE THEY ARE REFLECTED IN UI MyGrid.RefreshBehaviors();
From what I have read in other posts, dynamically added behaviors/editors have to be instantiated on every postback.
With this in mind, I have tried various combinations of injecting this code into the different places in the page lifecycle. Currently this code is residing in the Page_Init() method.
I am working on a project where we are encountering similar challenges with editor providers losing state during AJAX postback scenarios. When I attempt to restore behaviors, editor providers, etc on subsequent postbacks I have run into a variety of errors that I suspect relate to the points in the Page Life cycle where I attempt to synch up these details. At times I lose viewstate of the original data in the grid, at other I get errors that items are already defined.
Has anyone found any posted examples for recommended practices how to implement this correctly?
Hello,
I have the same problem, after a postback i lost my ProviderEditor, checking the client object i found that the ColumnSettings of my grid disappear. Any one have the solution for this? are there any way to create a ColumnSettings on client side?
I went around and around with the dev support team on this, finally ended up with an approach that seems to work.
My approach was all handled on the server side though, not on the client side. I would recommend doing it in the code behind versus on the client side via javascript, although I am sure it can be done in a similar fashion.
The key comes down to defining the editorproviders at the right point in the page life cycle. And then on every subsequent postback, re-initiliazing the editor for each editor provider, i.e.
Setup columns, behaviors, and databind your grid in the PreInit Method of the page
And then on Page_Load
For each editor provider
Get editor (editorprovider.getEditor())
Cast the editor to the appropriate editor type (WebTextEdit, WebDropDown, etc....)
For WebDropDowns be sure to rebind the datasource
That should get you started, becareful it seems very fragile, it is easy to get the viewstate out of whack if you are doing things at the wrong spot in the page lifecycle.
I'll try to attach a sample code.
Hi
Is this issue fixed for you? can you help me to retain the editor control values during postback?
Attached source code doesn't have the DropdownEditor provider code.
Thanks
Sam
Here is a sample, this particular sample has a user control, but you can remove that and just do everything directly in page as needed. Also note the grid.databind() that occurs between creating the columns and creating the behaviors. I never had a chance to trace through the code to see why this step needed to be there, but if it isn't things start to go bad quickly.
Good Luck!