GridView
非常有用的一个控件<如果需要分页,就用这个控件,如果不需要可以用Repeater 写代码会更灵活>
SortExpression="id" 排序
GridViewRow grv = GridView1.Rows[i]; //行
string name = grv.Cells[2].Text; //行的值
CommandArgument 属性:
CommandArgument='<%# Eval("id") %>' 绑定字段
CommandArgument='<%# Container.DataItemIndex %>' 绑定行索引
GridView1.DataKeys[i].Value.ToString() 关健字值
以上几个方法和属性就可以操作任何行,列的值
在编写自定义command事件时要指定 CommandArgument 属性
command事件:
1. CommandArgument如果指定的是索引
protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "get2")
{
int i = Convert.ToUInt16(e.CommandArgument);
GridViewRow grv = GridView1.Rows[i]; //行
string name = grv.Cells[2].Text; //行的值
string email = (grv.FindControl("Label1") as Label).Text; //如果采用了模块化,则要查找控件的text属性得到值
showmessagebox(email);
// 还有一种获得关健字的方法:
//GridView1.DataKeys[i].Value.ToString()
// 与 grv.cells[关健字的列].text 功能一样
}
}
2. CommandArgument如果指定的是字段
protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "get2")
{
string name=e.CommandArgument.toString();
showmessagebox(email);
}
}
注意: 如果不采用自已写command事件,也可以在 SqlDataSource 数据源中编写command sql语句 这时就不用指定CommandArgument 属性了
不过,在给CommandName 命名的时候,要采用默认的关健字
浙公网安备 33010602011771号