Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)delvpublished in(发表于) 2014/1/8 7:02:56 GridViewRow可以任意位置单击引发事件的方法_[Asp.Net教程]
首先为 GridView 填充数据
private void BindData() { SqlConnection myConnection = new SqlConnection(ConnectionString); SqlCommand myCommand = new SqlCommand("SELECT * FROM Users", myConnection); SqlDataAdapter ad = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); ad.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); }
接下来我们在 GridView_RowDataBound 事件中为 GridViewRow 赋予单击属性
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { string alertBox = "alert('"; if (e.Row.RowType == DataControlRowType.DataRow) { alertBox += e.Row.RowIndex; alertBox += "')"; e.Row.Attributes.Add("onclick", alertBox); } }
好了,很简单的方法,希望对你有用!
来源:网络
赞