首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ASP.Net中的GridView行单击事件我要获取id,并在单击行时将其发送到另一个页面

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

Stack Overflow用户
提问于 2014-09-18 13:05:25
回答 4查看 18.3K关注 0票数 1
{
    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行单击事件我想获取id,并在单击行时将其发送到另一个页面。

EN

回答 4

Stack Overflow用户

发布于 2014-09-18 13:13:58

试试这个:

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);
}
票数 1
EN

Stack Overflow用户

发布于 2014-09-18 13:17:57

您可以在网格视图中使用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);
}
票数 1
EN

Stack Overflow用户

发布于 2014-09-18 13:17:29

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

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25904509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档