GridView 行单击事件 ASP.Net 我想获取 id 并在单击行时将其发送到另一个页面

本文关键字:单击 另一个 Net ASP 事件 GridView id 获取 | 更新日期: 2023-09-27 18:22:20

{
    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);
}

GridView 行单击事件 ASP.Net 我想获取 id 并在单击行时将其发送到另一个页面。

GridView 行单击事件 ASP.Net 我想获取 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);
}

可以在网格视图中使用 Datakeynames 属性

<asp:GridView DataKeyNames="your Id you want to send">

和 在您的.cs代码将是

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
 string id= gvCustomReports.DataKeys[int.Parse(e.CommandArgument.ToString())].Values[0].ToString();
 Response.Redirect("MailsByOne.aspx?contactid="+id);
}

试试这个:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink((Control)sender, "Select$" + e.Row.RowIndex);
    }
}  
protected void gridInterventions_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
   GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
   int ID = Convert.ToInt32(row .Cells[0].Text);
   //Send your id here 
   //You can use session to save the id on one page and read in the target page
   Session["id"]=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);
}