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?
This is what I have that enables click to select row:
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
List<int> notClickable = new List<int>();
{
notClickable.Add(0);
}
for (int i = 0; i < 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";
}
}
Revised code that solves the problem:
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<int> notClickable = new List<int>();
{
notClickable.Add(0);
}
for (int i = 0; i < 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";
}
}
}