在ASP.NET中单击GridView行事件我希望获得id并在单击行时将其发送到另一个页面

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (160)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = "location.href='MailsByOne.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
        e.Row.Attributes["style"] = "cursor:pointer";
    }
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    contacts connn = new contacts();
    int index = GridView1.SelectedRow.RowIndex;
    int ID = Convert.ToInt32(GridView1.SelectedRow.Cells[0].Text);
    string message = "Row Index: " + index + "\\ContactID: " + connn.ContactID;
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}

在ASP.NET中单击GridView Row事件我希望获得id并在单击行时将其发送到另一个页面。

提问于
用户回答回答于

试试这个:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] =
                this.Page.ClientScript.
               GetPostBackClientHyperlink(this.grdList, "Select$" + e.Row.RowIndex);
        }
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
        GridViewRow SelectedRow = grdList.SelectedRow;
        string id = SelectedRow.Cells[0].Text;
        Response.Redirect("~/Mail/ShowMail.aspx?q="+id);
}
用户回答回答于
void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{    
    // Get the currently selected row using the SelectedRow property.
    GridViewRow row = gridview1.SelectedRow;

    //now get the labels if you use Template Fields 
    Label _LabelId = row.FindControl("LabelId") as Label;


//if You use Bound Fields then
int ID = Convert.ToInt32(GridView1.SelectedRow.Cells[0].Text);
    Response.Redirect("~/Mail/ShowMail.aspx?q="+id);

}

扫码关注云+社区

领取腾讯云代金券