gridview添加RowDataBound事件,并设置DataKeyName为绑定数据的主键
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{//判断选择的是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
int SId = Convert.ToInt32(this.gridview1.DataKeys[e.Row.RowIndex].Value);//获取DateKeyName的值
e.Row.Attributes.Add("onmouseover", "c=style.backgroundColor;style.backgroundColor='yellow'");
e.Row.Attributes.Add("onmouseout", "style.backgroundColor=c");
//添加双击事件(跳转至新页面)
e.Row.Attributes.Add("ondblclick", "window.open(a.aspx?SId=" + SId + "');");
//添加单击事件
e.Row.Attributes["onclick"] = ClientScript.GetPostBackEventReference(gridview1,"Select$"+e.Row.RowIndex.ToString());
}
双击后a页面接收sid
int sid = Convert.ToInt32(Request.QueryString["SId"]);
单击后获取单击行的DataKey
int value = Convert.ToInt32(this.gridview1.DataKeys[gridview1.SelectedIndex].Value);