问题1
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{}上面哪一个才是行单击事件。问题2
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onclick", "alert('" +GridView1.Rows[0].Cells[0].text.ToString()+ "');");
}
为什么这个报索引越界
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{}上面哪一个才是行单击事件。问题2
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onclick", "alert('" +GridView1.Rows[0].Cells[0].text.ToString()+ "');");
}
为什么这个报索引越界
ext.ToString()+ "');");
我估计这就是问题的关键
因为在RowDataBound事件中,是一行一行绑定数据,形成 GridView的数据,
你在还没绑定完数据,就调用GridView1.Rows[0],就会报错的!!
照你的想法,程序应该是这样:
string n = "";
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0)
n = e.Row.Cells[0].Text; e.Row.Attributes.Add("onclick", "alert('" + n + "');"); }
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "A('" +e.Cells[0].text.ToString()+ "');");");
}
}单击行执行A()
列是否存在
本篇文章来源于 http://www.17aspx.com
原文链接:http://www.17aspx.com/html/aspnet/controls/2009/0920/60.html