Gridview中经常用到的事件
protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCategoryList.PageIndex = e.NewPageIndex;
gvBind();
}
2.gridview中的删除行事件:OnRowDeleting="Gridview1_RowDeleting"
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Value);//获得删除的ID值
string strsql = "delete from LeaveWord where ID=" + ID;
database data = new database();//实例化一个database类
data.executesql(strsql); //执行命令
gvBind();
}
3.当选中某行时促发的事件:OnRowDataBound="gvCategoryList_RowDataBound"
protected void gvCategoryList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[1].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除ID:\"" + e.Row.Cells[2].Text + "\"吗?')");
}
}
}
4.gridview数据源绑定
public void gvBind()
{
string strcommand = "SELECT ID,Man,Sub,Content,Time,IP FROM LeaveWord ORDER BY ID DESC"; //command命令
database data = new database();//实例化一个database类
DataSet ds = new DataSet();
ds = data.gettable(strcommand);//获得dataset
gvCategoryList.DataSource = ds.Tables["LeaveWord"].DefaultView;//数据源
gvCategoryList.DataKeyNames = new string[] { "ID" };
gvCategoryList.DataBind();//数据绑定
}
浙公网安备 33010602011771号