title: Add and remove CssClass ASP.NET
body: <p>I have a weird behavior with my ASP.NET application. I want to change the color of the current selected row in a Grid View.</p>
  
  <p>My GridView is defined like this :</p>
  
  <pre><code>&lt;asp:GridView [..] OnSelectedIndexChanged="SUPResultList_SelectedIndexChanged"&gt;
      [..]
      &lt;RowStyle CssClass="DataGridItem" /&gt;
      &lt;AlternatingRowStyle CssClass="DataGridAlternateItem" /&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>In my code-behind, I have :</p>
  
  <pre><code>protected void SUPResultList_SelectedIndexChanged(object sender, EventArgs e)
  {
      GridView grid = sender as GridView;
  
      // I remove the class "selected" from the older row
      foreach (GridViewRow row in grid.Rows)
      {
          row.CssClass = row.CssClass.Replace("AdminRowSelected", String.Empty);
      }
  
      grid.SelectedRow.CssClass = String.Join(" ", grid.SelectedRow.CssClass, "AdminRowSelected");
  }
  </code></pre>
  
  <p>May be there is a better way to do what I want ?</p>
  
  <p>Anyway, when my gridview is first rendered, my rows have the good classes. But when I select a row and enter in SUPResultList_SelectedIndexChanged, row.CssClass is empty (so the result of row.CssClass.Replace("AdminRowSelected", String.Empty) is "" and String.Join(" ", grid.SelectedRow.CssClass, "AdminRowSelected"); is "AdminRowSelected"). After my processing I lose my original class and the rows become white (except the selected row).</p>
  
  <p>I also tried this instead of my loop but it failed too :</p>
  
  <pre><code>grid.RowStyle.CssClass = "DataGridItem";
  grid.AlternatingRowStyle.CssClass = "DataGridAlternateItem";
  </code></pre>
  
  <p>Do you have an idea of what is happening (and how to correct it ^^) ?</p>
  
  <p>Thanks in advance !</p>