操作后为什么要刷新才能更新数据

a377493206 2010-05-09 02:09:55
代码如下:

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()来更新数据的呀!
...全文
114 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
a377493206 2010-05-09
  • 打赏
  • 举报
回复
只有一个GridView,System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();看ps定义。[Quote=引用 5 楼 littlecc2009 的回复:]
引用 3 楼 a377493206 的回复:
我有重新绑定的。

引用 1 楼 zx75991 的回复:
数据发生变更,是需要重新绑一下的。

我看你的代码里有两个gridview吧:ps和gridview1
你只绑定了gridview1,没绑定ps。
[/Quote]
a377493206 2010-05-09
  • 打赏
  • 举报
回复
这个我有写啊,没有贴出来,[Quote=引用 4 楼 zx75991 的回复:]
绑定方法是不是这样用的。

[code=C#]
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}

void BindData() ……
[/Quote]
littlecc2009 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 a377493206 的回复:]
我有重新绑定的。

引用 1 楼 zx75991 的回复:
数据发生变更,是需要重新绑一下的。
[/Quote]
我看你的代码里有两个gridview吧:ps和gridview1
你只绑定了gridview1,没绑定ps。
天下在我心 2010-05-09
  • 打赏
  • 举报
回复
绑定方法是不是这样用的。


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}

void BindData() //绑定数据的方法
{

}

a377493206 2010-05-09
  • 打赏
  • 举报
回复
我有重新绑定的。[Quote=引用 1 楼 zx75991 的回复:]
数据发生变更,是需要重新绑一下的。
[/Quote]
littlecc2009 2010-05-09
  • 打赏
  • 举报
回复
OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
cmd.ExecuteNonQuery();
之后加上:
databind();
天下在我心 2010-05-09
  • 打赏
  • 举报
回复
数据发生变更,是需要重新绑一下的。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧