asp.net的更新问题
问题:各位大侠,我是个asp.net的初学都,做了个页面,更新后有记录已经更新成功了,但有时刷新显示,有时不显示,查了很多,也加了 if (!IsPostBack),但还是有这个问题,代码如下,
if (!IsPostBack)
{
if (Request.QueryString["id"] != null & Request.QueryString["type"] == "up")
{
string sql1 = "select * from product where ArticleID=" + Request.QueryString["id"] + "";
DataSet ds1 = function.sqlds(sql1, "tmp1");
string CurrentId = ds1.Tables["tmp1"].Rows[0]["ArticleID"].ToString();
string CurrentPx = ds1.Tables["tmp1"].Rows[0]["addtime"].ToString();
//
string sql2 = "select top 1 * from product where addtime>#" + CurrentPx + "# order by addtime asc";
DataSet ds2 = function.sqlds(sql2, "tmp2");
string UpId = ds2.Tables["tmp2"].Rows[0]["ArticleID"].ToString();
string UpPx = ds2.Tables["tmp2"].Rows[0]["addtime"].ToString();
//
string sqls1 = "UpDate Product set addtime= '" + UpPx + "' where ArticleID= " + CurrentId;
//
string sqls2 = "UpDate Product set addtime= '" + CurrentPx + "' where ArticleID= " + UpId;
function.SqlExe(sqls1);
function.SqlExe(sqls2);
}
最佳答案: 估计是你的页面控件的绑定有问题!
你在数据更新后,再重新绑定一次!
回答2: IsPostBack好处很大,但是你这种做法也不好,
我建议你把IsPostBack里的所有代码拷到一个专门的方法里边
第一次加载之后。。
一旦有了更新,就调用这个方法,这样不仅可以保证能够执行到,而及时更新之外还能确保服务重新加载。
回答3: 你贴上来的代码看不出问题。
1.你用的数据绑定控件是在哪里绑定的?也就是在哪里执行.Bind()方法?
2.你每次进行更新操作是否重新获取了数据源,重新进行了绑定?
回答4: 估计你更新过后没有再次绑定数据
http://www.you68.cn
在更新完数据之后还要执行一次
string sqla2 = "select * from Product order by addtime desc";
GridView1.DataSource = function.SqlTable(sqla2).DefaultView;
GridView1.DataBind();
才会显示你的更新内容
回答5: 发现问题:
string sql1 = "select * from product where ArticleID=" + Request.QueryString["id"] + "";
ArticleID=" 的引号前加一个单引号
你把if (!IsPostBack)
{
这去了呢
补存
string sqla2 = "select * from Product order by addtime desc";
GridView1.DataSoureID=null;
GridView1.DataSource = function.SqlTable(sqla2).DefaultView;
GridView1.DataBind();
把你的上面的那段补存代码改成这个看看行不行?
回答6: 看看是不是浏览器没有真正刷新,试加:
this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
另外有你这一句
select * from product where ArticleID=" + Request.QueryString["id"] + ""
网站往外一方,很快就死掉