Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.net Gridview - Why Are DataRowBound changes Lost on sort?
    text
    copied!<p>I am making conditional formatting changes to the data in my gridview using a RowDataBound event:</p> <pre><code> void gvReg_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DateTime lastUpdate DateTime.Parse(DataBinder.Eval (e.Row.DataItem, "LAST_UPDATE"); if (lastUpdate &lt; DateTime.Today.AddMonths(-1)) { Hyperlink hypLastUpdate = (Hyperlink)e.Row.FindControl("hypLastUpdate"; hypLastUpdate.CssClass = "Error"; hypLastUpdate.NavigateUrl = "http://www.someExampleErrorPage.com"; } } } </code></pre> <p>This works, and sets the proper CssClass to the hyperlink (which makes it a jarring shade of bold red), but once the gridview is sorted (via the user clicking a column heading) the css class is reset on hypLastUpdate and it loses both it's style and associated NavigateUrl property.</p> <p>The control hypLastUpdate is contained in a template field in a gridview, and it's text value is databound to a field called "LAST_UPDATE".</p> <p>Is this a planned behavior (is sorting supposed to break the conditional formatting done in RowDataBound events?) or is there something I can check to make sure I am not doing something incorrectly?</p> <p>I am not using the DataBind method anywhere in the code behind, and viewstate is turned on for the gridview in question.</p> <p>--EDIT--</p> <p>It ended up being a mistake in event handling.</p> <p>I was doing:</p> <pre><code>gvReg.Sorted += {SomeEventHandler} </code></pre> <p>Inside of the page load event, but only when it wasn't a postback. This function called gvReg.DataBind after the grid view was sorted. I removed the handler wire up and instead added the event handler function to the OnSorted event. I guess assigned delegates to a gridview are not saved in ViewState between callbacks?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload