简体   繁体   中英

Cannot remove the attribute from row cell in gridview | asp.net

I have a gridview in which I want the complete row to be clickable but I just don't want the last column to be clickable. I could assign the clickability to each cell individually but that doesn't look good as when we hover on one column only that column rows are highlighted. So I did this, set the clickablity for the row and remove it from last column, but unable to remove the attribute from the last column. It is still clickable.

Please let me know if I explained the issue well and you get what I want ?

protected void gvTables_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.color='blue';this.style.textDecoration='underline';";
        e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.color='black';";
        e.Row.ToolTip = "Click to Edit";
        e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gvTables, "Select$" + e.Row.RowIndex);

        e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Remove("onmouseover");
        e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Remove("onmouseout");
        e.Row.Cells[e.Row.Cells.Count - 1].ToolTip = "";
        e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Remove("onclick");            
    } 
}

You cannot remove the attributes from the last cell because they do not exist. The attributes are set on row level <tr> and not cell level <td> .

You can set the attributes per cell like this:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.color='blue';this.style.textDecoration='underline';";
    e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.color='black';";

    for (int i = 0; i < e.Row.Cells.Count - 1; i++)
    {
        e.Row.Cells[i].ToolTip = "Click to Edit";
        e.Row.Cells[i].Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gvTables, "Select$" + e.Row.RowIndex);
    }  
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM