GridView1_SelectedIndexChanging 是翻页的操作跟row的事件无关
GridView1_RowDataBound 是行绑定事件
1.
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "alert('a');");//alert('a');可以换成自己的方法
}
(这个是照你的逻辑写下来的,但是不推荐)
2.你可以在gv中增加一个按钮列,给这个列添加一个commond 命令行事件中添加 把当前行数据同步到textbox中的操作(个人感觉这种办法更简单,明了)
完整例子如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.BindList();
}
}
//这个函数是为GridView绑定数据的,具体数据,你自己从数据库中读
private void BindList()
{
DataTable table = new DataTable();
table.Columns.Add("userName", Type.GetType("System.String"));
table.Columns.Add("userSex", Type.GetType("System.String"));
for (int i = 0; i < 10; i++)
{
DataRow row = table.NewRow();
row[0] = "用户" + i.ToString();
row[1] = i % 2 == 0 ? "男" : "女";
table.Rows.Add(row);
}
this.GridView1.DataSource = table;
this.GridView1.DataBind();
}
//这个函数是关键,当数据行绑定后发生
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//下面三句代码可以实现你的效果,如果不合适,自己可以比照着修改一下。
string cellvalue = e.Row.Cells[0].Text;
string js = string.Format("alert(\"{0}\");", cellvalue);
e.Row.Attributes.Add("onclick", js);
//下面两句代码是添加鼠标效果,可以省略,当鼠标移动到行上时,变颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccddff',this.style.fontWeight='Bold';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
}
}
用JS操作啊,多简单啊。。