c# gridview row click

后端 未结 9 1922
野的像风
野的像风 2020-12-01 09:34

When I click on a row in my GridView, I want to go to a other page with the ID I get from the database.

In my RowCreated event I have the following line:

         


        
9条回答
  •  遥遥无期
    2020-12-01 10:14

    Martijn,

    Here's another example with some nifty row highlighting and a href style cursor:

    protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
        e.Row.Attributes.Add("style", "cursor:pointer;");
        e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
      }
    }

    The code above works in .NET 3.5. However, you can't set your id column to Visible="false" because you'll get a blank query string value for your id key:

    
      
        
        
        
        
        
      
    

    So change the first column to this instead:

    Add this css to the top of your page:

    
      
    

    But to hide the first cell of your header row, add this to your gvSearch_RowDataBound() in code-behind:

    if (e.Row.RowType == DataControlRowType.Header)
    {
      e.Row.Cells[0].CssClass = "hide";
    }

    Obviously, you could have hidden the id column in code-behind too, but this will result in more text in your markup than a css class:

    e.Row.Cells[0].Attributes.Add("style", "display:none;");
    e.Row.Attributes.Add("style", "cursor:pointer;");

提交回复
热议问题