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:
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
}
}
JohnB, Your code works very fine, i added just a little hack to avoid alternatingRowStyle ruining after mouseout.
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
Changed to:
e.Row.Attributes.Add("onmouseout", "if(" + e.Row.RowIndex + "% 2 == 0) { this.style.backgroundColor=''; } else { this.style.backgroundColor = '#E8F7EA'; }");
If there's a better way to do it, please let me know, but it's working perfect for me.
Greetings.
I have the solution.
This is what i have done:
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}
I have putted the preceding code in the RowDataBound event.
row click in grid view redirect to other page
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
}
}
works absolutely fine
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
}
}
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:
<asp:GridView ID="gvSearch" runat="server" OnRowDataBound="gvSearch_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="id" Visible="false" />
<asp:BoundField DataField="first_name" HeaderText="First" />
<asp:BoundField DataField="last_name" HeaderText="Last" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:BoundField DataField="state_name" HeaderText="State" />
</Columns>
</asp:GridView>
So change the first column to this instead:
<asp:BoundField DataField="id" ItemStyle-CssClass="hide" />
Add this css to the top of your page:
<head>
<style type="text/css">
.hide{
display:none;
}
</style>
<head>
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;");