2013-11-01 30 views
1

一直在寻找天,但不能缩小原因。我对此已有足够的了解,并已经看到了一些建议,但他们似乎都没有解决这个问题。UpdatePanel中的GridView在onrowdatabound中更改单元格颜色会导致视图状态错误:未能加载视图状态

我在一个更新面板中有一个带有Gridview控件的asp.net页面。没有什么特别的Gridview,几个绑定的文本框字段。 Gridview由后台代码中的数据表加载。

所有工作都很好,直到我根据其中一个Column值更改单元格的背景颜色。如果我删除下面的代码,那么我不会看到状态错误。

OnRowDataBound代码:

protected void PCM_Grid_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      //check if we are allowed to edit 
      if (DataBinder.Eval(e.Row.DataItem, "DisableUpdateCCCat").ToString() == "Y") 
      { 
       e.Row.Cells[8].BackColor = System.Drawing.Color.Silver; //Silver out Cat column 
      } 
     } 
    } 

当我将格式应用在上面,然后说单击保存按钮,我得到的错误:

0x800a139e - JavaScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: 
Failed to load viewstate. The control tree into which viewstate is being loaded must match the 
control tree that was used to save viewstate during the previous request. For example, when 
adding controls dynamically, the controls added during a post-back must match the type and 
position of the controls added during the initial request. 

带我读过你需要更新UpdatePanel中的视图状态

UpdatePanel.Update(); 

但是错误仍然存​​在。

任何想法/帮助?

任何答复赞赏

+0

您好!根据你的描述从头开始创建项目 - 没有问题。你可以发布页面和GridView的ViewState的一些标记和设置吗? – IDeveloper

回答

0

保护无效PCM_Grid_RowDataBound(对象发件人,GridViewRowEventArgs E) {

如果(e.Row.RowType == DataControlRowType.DataRow) {

//尝试获取gridview值如下

string DisableUpdateCCCat = e.Row.Cells [6] .Text;

如果(DisableUpdateCCCat == “Y”)

{

e.Row.Cells [6] .BackColor = System.Drawing.Color.Silver;

}

}

}