title: Click to select row in gridview causing postback in edit mode
body: <p>Currently, when a user clicks on anywhere on a row, it selects that row. Unfortunately, when in edit mode (after clicking on "edit'), the form does a postback every time a user clicks anywhere on the form. How do I disable this and let the user edit the row in peace?</p>
  
  <p>This is what I have that enables click to select row:</p>
  
  <pre><code>protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              List&lt;int&gt; notClickable = new List&lt;int&gt;();
              {
                  notClickable.Add(0);
              }
  
              for (int i = 0; i &lt; e.Row.Cells.Count; i++)
              {
                  if (!notClickable.Contains(i))
                  {
                      e.Row.Cells[i].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
                      e.Row.Cells[i].Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
                  }
              }
  
              e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#ceedfc'");
              e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
              e.Row.Attributes.Add("style", "cursor:pointer;");
              e.Row.ToolTip = "Click to select row";
          }
      }
  </code></pre>
  
  <p>Revised code that solves the problem:</p>
  
  <pre><code>protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              if (GridView1.EditIndex != -1)
              {
                  e.Row.Attributes.Remove("onmouseover");
              }
              else
              {
                  List&lt;int&gt; notClickable = new List&lt;int&gt;();
                  {
                      notClickable.Add(0);
                  }
  
                  for (int i = 0; i &lt; e.Row.Cells.Count; i++)
                  {
                      if (!notClickable.Contains(i))
                      {
                          e.Row.Cells[i].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
                          e.Row.Cells[i].Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
                      }
                  }
  
                  e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#ceedfc'");
                  e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
                  e.Row.Attributes.Add("style", "cursor:pointer;");
                  e.Row.ToolTip = "Click to select row";
              }
          }
      }
  </code></pre>