Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a gridview row clickable?

I want to create a gridview row where the whole row is clickable and when I click anywhere on the row it open another aspx page with the rows information.

I am using asp.net and C#. Can anyone help me please. Thanks in advance.

like image 465
Shibly Avatar asked Aug 31 '25 18:08

Shibly


1 Answers

fire two events of Gridview

OnRowDataBound and OnSelectedIndexChanged

Then write code in these events

protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
        e.Row.ToolTip = "Click to select this row.";
    }
}

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowIndex == GridView1.SelectedIndex)
        {
            row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
            row.ToolTip = string.Empty;
        }
        else
        {
            row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
            row.ToolTip = "Click to select this row.";
        } 
    }
}

also set property EnableEventValidation = "false" in

like image 149
Sheenu Mehra Avatar answered Sep 02 '25 07:09

Sheenu Mehra