protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
//只有数据行才有绑定数据
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnDel = e.Row.Cells[1].Controls[0] as ImageButton;
btnDel.Attributes.Add("onclick", "javascrip:return confirm('确定删除订阅?')");
}
}
Cells是单元格,Controls是控件,Controls[0]代表第一个控件,这里as的意思我还不是很清楚,不过看起来好像是新建了一个指定控件的引用,然后通过这个引用对这个控件添加了属性。
{
//只有数据行才有绑定数据
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnDel = e.Row.Cells[1].Controls[0] as ImageButton;
btnDel.Attributes.Add("onclick", "javascrip:return confirm('确定删除订阅?')");
}
}
同时,对GridView还有一些其它问题。比如读取的数据是1和0,想要转换为男和女。以前还以为要在SQL语句中通过判断实现,或者在业务层中通过读取DataTable时转换,现在才发现,在这个RowDataBound方法中就可以轻松实现。
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
//只有数据行才有绑定数据
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.Cells[0].Text.Trim() == "1")
{
e.Row.Cells[0].Text = "男";
}
else
{
e.Row.Cells[0].Text = "女";
}
}
}
{
//只有数据行才有绑定数据
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.Cells[0].Text.Trim() == "1")
{
e.Row.Cells[0].Text = "男";
}
else
{
e.Row.Cells[0].Text = "女";
}
}
}