ASP.NET gridview中的数据刷新问题 [英] Data refresh issue in ASP.NET gridview

查看:120
本文介绍了ASP.NET gridview中的数据刷新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在asp.net中遇到数据刷新问题。

在主页上点击链接时,会加载另一个页面,其中有一个gridview控件。这个gridview显示来自xml文件的数据,并且其中包含编辑/更新选项。

当编辑和更新任何单元格的值时,新值将显示在gridview中,并且还成功写回到xml中但是问题是,如果再次点击链接,网格视图会显示旧数据而不是更新数据。



有人可以帮我解决。



我的尝试:



1.我试过在aspx文件中使用EnableRowsCache =false,它没有帮助。

2.Tried下面的代码,但是当点击gridview中的编辑按钮时它会导致异常

Hi I am facing a data refresh problem in asp.net.
On the home page when a link is clicked another page is loaded which has a gridview control.This gridview displays data from an xml file and it has edit/update options in it.
When the value of any cell is edited and updated the new value is displayed in the gridview and also successfully written back to the xml in the back end.But the problem is that if the linked is clicked again the grid view shows the old data rather than the updated data.

Can someone please help me out.

What I have tried:

1.I have tried using EnableRowsCache="false"in the aspx file,it didn't help.
2.Tried the below code ,but it causes exception when the edit button in the gridview is clicked

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        MyGridView.DataBind();
    }
}

推荐答案

应该不是IsPostback

检查以下内容 -

It should be not IsPostback
Check following-
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack) // note the not operator (!)
   {
      MyGridView.DataBind();
   }
} 





希望,它有帮助:)



Hope, it helps :)


您需要在调用DataBind()之前设置DataSource以反映更改。



You need to set the DataSource before calling DataBind() to reflect the changes.

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack) 
   {
      MyGridView.DataSource = ???; //set the data source here
      MyGridView.DataBind();
   }
} 


这确实是一个缓存问题。



调用
It was a cache problem indeed.

Calling
Response.Cache.SetCacheability(HttpCacheability.NoCache) in Page_Load() fixed the refresh issue.


这篇关于ASP.NET gridview中的数据刷新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆