UpdatePanel中的GridView更改onrowdatabound中的单元格颜色会导致viewstate错误:无法加载viewstate

时间:2013-11-01 00:24:19

标签: c# asp.net gridview viewstate

一直在寻找天,但只是缩小了原因。我已经阅读了足够的内容,并且已经看到了建议,但似乎没有一个能解决这个问题。

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

一切正常,直到我根据其中一个Column值更改单元格的背景颜色。如果我删除下面的代码,那么我不会收到viewstate错误。

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();

但错误仍然存​​在。

任何想法/帮助?

任何回复非常感谢

1 个答案:

答案 0 :(得分:0)

protected void PCM_Grid_RowDataBound(object sender,GridViewRowEventArgs e)     {

if(e.Row.RowType == DataControlRowType.DataRow)         {

//尝试获取gridview值,如下所示

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

如果(DisableUpdateCCCat == “Y”)

{

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

}

}

}