GridViewRow可以任意位置单击引发事件的方法

来源:互联网 发布:js动态修改class样式 编辑:程序博客网 时间:2024/06/05 16:19
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
GridView 是 ASP.NET 2.0 中应用最为广泛一个控件,几乎所有的数据操作都需要它,正如我们平常所应用的,可以编辑、删除、选择等等,但如果客户有需要通过单击行而引发超链接或者进入行编辑状态时,我们该如何实现,这里介绍了一种方法来实现此功能。它将允许你通过点击行的任何一个位置而引发你所需要的事件。

首先为 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);

}

}
好了,很简单的方法,希望对你有用!

http://www.cnblogs.com/xh3/archive/2006/07/28/462084.html

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击