简体   繁体   English

使单元格在ASP GridView中可单击

[英]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. 我需要在整个单元格上不仅仅是在文本上的此超级链接。 在此处输入图片说明

You can give the cell an onclick attribute in the same way you did with the style. 您可以使用与样式相同的方式为单元格赋予onclick属性。

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) 或者将链接以完整的宽度和高度围绕div包裹(对于SEO更好)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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