Hello,
I am trying to set css to change the styling of a row that will be triggered upon a postback. For this I am handling the Initialize Row event. The problem is I am losing my css styling. How do I change the styling of a row (e.g. row color) upon an ajax post-back?
I had the same issue and I didn't want extra postback + my "colouring" was depending on server-side logic, which couldn't be migrated to JS.
Perhaps my solution will help someone as yours helped me. In short, I used CustomAJAXResponse.AdditionalProperties.
So let's assume we have a bunch of cells which style we want to change.
1) We form by some logic on the server side the array of these cells. For example, GridRecordItem[] cells.
2) We add "addresses" of these cells to customAjaxResponse. There are different ways to do that - using indexes (which I wouldn't recommend because if something in your code sorts the values it can cause problems) or idpairs for rows, I just used row.DataKey in the following code:
int i = 0; foreach (GridRecordItem cell in cells) { this.WebDataGrid1.CustomAJAXResponse.AdditionalProperties.Add("rowKey" + i.ToString(), cell.Row.DataKey); this.WebDataGrid1.CustomAJAXResponse.AdditionalProperties.Add("columnKey" + i.ToString(), cell.Column.Key); i++; } this.WebDataGrid1.CustomAJAXResponse.AdditionalProperties.Add("Count", i);
Please note, that for this you must have columnKeys assigned.
3) In AJAX postback handler on the client side we set our styles.
function WebDataGrid1_Grid_AJAXResponse(sender, e) { var grid = $find('<%= this.WebDataGrid1.ClientID %>'); var properties = e.get_customResponseObject().AdditionalProperties; for (var i = 0; i < properties.Count; i++) { var rowKey = properties["rowKey" + i]; var columnKey = properties["columnKey" + i]; var row = grid.get_rows().get_rowFromKey(rowKey); if (row != null) { var cell = row.get_cellByColumnKey(columnKey); if (cell != null) { cell.get_element().className = "YourCSSName"; }
} } }