ASP.NET中GridView上的整行选择

本文关键字:行选 选择 NET GridView ASP | 更新日期: 2023-09-27 18:20:21

我有一个带有一些列的网格视图。第一列是模板字段:

...
<asp:TemplateField HeaderText="id">
      <ItemTemplate>
           <asp:HyperLink ID="HyperLink1" runat="server" 
                        NavigateUrl='<%# "~/mail/showMail.aspx?q="+Eval("id") %>'>Select</asp:HyperLink>
           </ItemTemplate>
</asp:TemplateField>
...

我想使用整行选择,而不是上面的TemplateField。我使用这个代码:

protected void grdList_RowCreated(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.ToolTip = "Click to select row";
            e.Row.Attributes["onclick"] =
                this.Page.ClientScript.
               GetPostBackClientHyperlink(this.grdList, "Select$" + e.Row.RowIndex);
        }
}

但不起作用。

如何使用超链接而不是"选择$",如下图所示:

ClientScript.
               GetPostBackClientHyperlink(this.grdList, "~/mail/showMail.aspx?q=Eval('"id'")" );

ASP.NET中GridView上的整行选择

找到了一个解决方案:

我使用boundField而不是像这样的模板字段:

<asp:BoundField DataField="id" HeaderText="id" />

并使用此事件:

protected void grdList_SelectedIndexChanged(object sender, EventArgs e)
{
        GridViewRow SelectedRow = grdList.SelectedRow;
        string id = SelectedRow.Cells[0].Text;
        Response.Redirect("~/Mail/ShowMail.aspx?q=" + id);
}
protected void grdList_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
        e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
        e.Row.ToolTip = "Click to select row";
        e.Row.Attributes["onclick"] =
            this.Page.ClientScript.
           GetPostBackClientHyperlink(this.grdList, "Select$" + e.Row.RowIndex);
    }
}