Akshay
Akshay

Reputation: 1472

Make cell clickable in asp gridview

I need the complete cell clickable. I tried few solutions but they couldn't make cell clickable, the text is clickable only.

protected void gvSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow )
    {
        e.Row.Cells[0].Attributes["style"] = "cursor:pointer";
        e.Row.Cells[0].Text = "<a href='/Pages/ProjectEdit.aspx?ID="+ ((Label)e.Row.FindControl("lblID")).Text+"'>" + ((Label)e.Row.FindControl("lblID")).Text + "</a>";
    }
}

..............................

<Columns>
    <asp:TemplateField ItemStyle-CssClass="ItemClass" ItemStyle-Width="8%" HeaderText="Project ID">
        <ItemTemplate>
            <asp:Label style="cursor:pointer;" ID="lblID" runat="server" Text='<%# Eval("id")%>' ></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

I need this hyper link on the complete cell not just on the text. enter image description here

Upvotes: 1

Views: 2794

Answers (1)

VDWWD
VDWWD

Reputation: 35514

You can give the cell an onclick attribute in the same way you did with the style.

e.Row.Cells[0].Attributes["onclick"] = "location.href='/Pages/ProjectEdit.aspx?ID='";

Or wrap the link around a div with full width and height (better for SEO)

e.Row.Cells[0].Text = "<a href=\"/Pages/ProjectEdit.aspx?ID=\"><div style=\"width: 100%; height: 100%;\">CLickMe</div></a>";

Upvotes: 4

Related Questions