title: Why adding/removing css classes like this won't work?
body: <p>Ok so I have a <strong>GridView</strong> like this:</p>
  
  <pre><code>&lt;asp:GridView ID="gv" runat="server" ... &gt;
  ...
  &lt;HeaderStyle CssClass="header" /&gt;
  &lt;RowStyle CssClass="datarow" /&gt;
  &lt;AlternatingRowStyle CssClass="datarow alt" /&gt;
  &lt;Columns&gt;
      &lt;asp:TemplateField&gt;
          &lt;ItemTemplate&gt;
              &lt;asp:CheckBox runat="server" ID="gvchkDelete" /&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  &lt;/Columns&gt;
  ...
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Notice the custom <strong>RowStyle</strong> and <strong>AlternatingRowStyle</strong> css classes.
  What I want is to change the row's style whenever the <strong>gvchkDelete</strong> CheckBox is selected, and remove it when it's not. So I came up with something like this:</p>
  
  <pre><code>protected void gv_PreRender(object sender, EventArgs e)
  {
      DUtil.GridViewRowHighlight(gv, "gvchkDelete");
  }
  
  //External Class
  public class DUtil
  {
  public static void GridViewRowHighlight(GridView gv, string CheckBoxControlID)
  {
      foreach (GridViewRow gvr in gv.Rows) {
          GridViewRowHighlight(gvr, CheckBoxControlID);
      }
  }
  
  public static void GridViewRowHighlight(GridViewRow gvr, string CheckBoxControlID)
  {
      string scriptFormat = "if(this.checked){{document.getElementById('{0}').className+=' {1}';}}else{{document.getElementById('{0}').className=document.getElementById('{0}').className.replace('{1}','');}};";
      string script = String.Format(scriptFormat, gvr.ClientID, DEstilos.HighlightStyle);
      CheckBox chk = (CheckBox)gvr.FindControl(CheckBoxControlID);
      if (chk.Checked) AddCssClass(gvr, DEstilos.HighlightStyle);
      else RemoveCssClass(gvr, DEstilos.HighlightStyle);
      chk.Attributes.Add("onclick", script);
  }
  
  public static void AddCssClass(WebControl control, string cssClass)
  {
      List&lt;string&gt; classes = new List&lt;string&gt;(control.CssClass.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
      classes.Add(cssClass);
      control.CssClass = ToDelimitedString(classes, " ");
  }
  
  public static void RemoveCssClass(WebControl control, string cssClass)
  {
      List&lt;string&gt; classes = new List&lt;string&gt;(control.CssClass.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
      classes.Remove(cssClass);
      control.CssClass = ToDelimitedString(classes, " ");
  }
  
  public static string ToDelimitedString(IEnumerable&lt;string&gt; list, string delimiter)
  {
      StringBuilder sb = new StringBuilder();
      foreach (string item in list) {
          if (sb.Length &gt; 0)
              sb.Append(delimiter);
  
          sb.Append(item);
      }
      return sb.ToString();
  }
  //End class
  }
  
  //External Class
  public class DEstilos
  {
  public const string HighlightStyle = "highlight";
  //End Class
  }
  </code></pre>
  
  <p>What all of that does is: for every checkbox in the gridview.Rows collection add a JavaScript function to the checkbox's onclick attribute so that when it's clicked the checkbox's parent row it's added/removed the Highlight style. And also if the checkbox is checked/unchecked by default then add/remove the highlight style.</p>
  
  <p>All of this just to ensure that whenever a <strong>postback</strong> is made the rows with a selected checkbox conserved the Highlight style (witch doesn't happen by just enabling viewstate of course). The problem is that the <strong>RowStyle</strong> and <strong>AlternatingRowStyle</strong> css classes defined in the gridview's markup seems to be getting "<strong>ignored</strong>" or "<strong>removed</strong>", Why?</p>
  
  <p><strong>UPDATE</strong>: Solutions without using jQuery or Prototype would be better, except if that's the only way of doing this of course.</p>
  
  <p><strong>UPDATE 2, 3</strong>: If you remove the lines:</p>
  
  <pre><code>if (chk.Checked) AddCssClass(gvr, DEstilos.HighlightStyle);
  else RemoveCssClass(gvr, DEstilos.HighlightStyle);
  </code></pre>
  
  <p>from the GridViewRowHighlight method, then the RowStyle and AlternatingRowStyle css classes <strong>DO</strong> get added to the HTML markup and I get the desired effect, but on every postback the highlight effect is removed, while the checkbox maintains it's previous state.</p>
  
  <p>Thanks for all your help!</p>