在ASP.NET中的GridView上全行选择?

时间:2014-09-02 04:16:47

标签: c# asp.net gridview

我有一个带有某些列的gridview。第一列是模板字段:

...
<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\")" );

1 个答案:

答案 0 :(得分:0)

找到解决方案:

我使用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);
    }
}