代码如下: protected void btdel_Click(object sender, EventArgs e)
{
try
{
int intCount = this.GridView1.Rows.Count;
for (int i = 0; i < intCount; i++)
{
CheckBox CheckSingle = this.GridView1.Rows[i].Cells[6].FindControl("chk2") as CheckBox;
if (CheckSingle.Checked)
{
int id = Convert.ToInt32(GridView1.Rows[i].Cells[0].Text.ToString());
conn = new OleDbConnection(AccessString + System.Web.HttpContext.Current.Server.MapPath(DataBase));
conn.Open();
string sqlStr = "delete from UserInfo where id=" + id;
OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
cmd.ExecuteNonQuery();
//this.lbmessage.Text = "删除成功";
Response.Write("删除成功");
}
}
this.DBUserInfo();
}
catch
{
this.lbmessage.Text = "服务器忙,请一会再试";
}
finally
{
conn.Close();
}
}
private void DBUserInfo()
{
int countpage = Convert.ToInt32(this.lblcurrpage.Text);//当前页
conn = new OleDbConnection(AccessString + System.Web.HttpContext.Current.Server.MapPath(DataBase));
conn.Open();
string sqlText = "select * from UserInfo order by id asc";
OleDbDataAdapter da = new OleDbDataAdapter(sqlText, conn);
da.Fill(ds, "text");
System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["text"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 3;
ps.CurrentPageIndex = countpage - 1;
this.lbpagecount.Text = Convert.ToString(ps.PageCount);
if (countpage == 1)
{
this.firstpage.Enabled = false;
this.prepage.Enabled = false;
}
else
{
this.firstpage.Enabled = true;
this.prepage.Enabled = true;
}
if (countpage == ps.PageCount)
{
this.lastpage.Enabled = false;
this.nextpage.Enabled = false;
}
else
{
this.lastpage.Enabled = true;
this.nextpage.Enabled = true;
}
this.GridView1.DataSource = ps;
this.GridView1.DataBind();
if (this.GridView1.Rows.Count == 0)
{
this.panel1.Visible = false;
this.lbmessage.Text = "没有用户申请注册!";
}
else
{
this.panel1.Visible = true;
}
pagecount = Convert.ToString(ps.PageCount);
conn.Close();
}可以成功删除,数据库也更新了,可是GridView中没有马上显示出来,要再刷新一次页面才能更新GridView!怎么回事呀!
删除操作里面,我有调用DBUserInfo()来更新数据的呀!
{
try
{
int intCount = this.GridView1.Rows.Count;
for (int i = 0; i < intCount; i++)
{
CheckBox CheckSingle = this.GridView1.Rows[i].Cells[6].FindControl("chk2") as CheckBox;
if (CheckSingle.Checked)
{
int id = Convert.ToInt32(GridView1.Rows[i].Cells[0].Text.ToString());
conn = new OleDbConnection(AccessString + System.Web.HttpContext.Current.Server.MapPath(DataBase));
conn.Open();
string sqlStr = "delete from UserInfo where id=" + id;
OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
cmd.ExecuteNonQuery();
//this.lbmessage.Text = "删除成功";
Response.Write("删除成功");
}
}
this.DBUserInfo();
}
catch
{
this.lbmessage.Text = "服务器忙,请一会再试";
}
finally
{
conn.Close();
}
}
private void DBUserInfo()
{
int countpage = Convert.ToInt32(this.lblcurrpage.Text);//当前页
conn = new OleDbConnection(AccessString + System.Web.HttpContext.Current.Server.MapPath(DataBase));
conn.Open();
string sqlText = "select * from UserInfo order by id asc";
OleDbDataAdapter da = new OleDbDataAdapter(sqlText, conn);
da.Fill(ds, "text");
System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["text"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 3;
ps.CurrentPageIndex = countpage - 1;
this.lbpagecount.Text = Convert.ToString(ps.PageCount);
if (countpage == 1)
{
this.firstpage.Enabled = false;
this.prepage.Enabled = false;
}
else
{
this.firstpage.Enabled = true;
this.prepage.Enabled = true;
}
if (countpage == ps.PageCount)
{
this.lastpage.Enabled = false;
this.nextpage.Enabled = false;
}
else
{
this.lastpage.Enabled = true;
this.nextpage.Enabled = true;
}
this.GridView1.DataSource = ps;
this.GridView1.DataBind();
if (this.GridView1.Rows.Count == 0)
{
this.panel1.Visible = false;
this.lbmessage.Text = "没有用户申请注册!";
}
else
{
this.panel1.Visible = true;
}
pagecount = Convert.ToString(ps.PageCount);
conn.Close();
}可以成功删除,数据库也更新了,可是GridView中没有马上显示出来,要再刷新一次页面才能更新GridView!怎么回事呀!
删除操作里面,我有调用DBUserInfo()来更新数据的呀!
cmd.ExecuteNonQuery();
之后加上:
databind();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}void BindData() //绑定数据的方法
{}
你只绑定了gridview1,没绑定ps。