简体   繁体   English

在代码隐藏中绑定GridView不起作用

[英]Binding GridView in Code-Behind Not Working

I have a very annoying problem that I've been struggling with for a few hours now. 我有一个非常烦人的问题,我已经苦苦挣扎了几个小时。 I have multiple asp.net GridView controls on a page. 我在一个页面上有多个asp.net GridView控件。 One of these grids (grid B), is dependent on another grid (grid a) for its data to update correctly. 这些网格之一(网格B)依赖于另一个网格(网格a)来正确更新其数据。

The problem I have is this: 我的问题是这样的:

When I do gridB.Databind() in a if(!IsPostback), then functionality of Grid B works, but the grid doesn't update with newly added records in Grid A. 当我在if(!IsPostback)中执行gridB.Databind()时,网格B的功能正常工作,但网格不会使用网格A中新添加的记录进行更新。

When I do gridB.Databind() anywhere else that doesn't contain a Postback check, then the data in grid B updates correctly with the newly added records in Grid A, but then the functionality of Grid B no longer works. 当我在不包含回发检查的其他任何地方进行gridB.Databind()时,网格B中的数据将使用网格A中新添加的记录正确更新,但是网格B的功能将不再起作用。

Example: 例:

            // Functionality for dropdownlist etc works correctly, but new data from gvA doesn't show in gvB
            if (grvSender.ID == "gvA")
            {
                if (!IsPostBack)
                    gvB.DataBind();
            }

            // Functionality for dropdownlist etc no longer works correctly, but new data from gvA shows correctly in gvB
            if (grvSender.ID == "gvA")
            {
                if (IsPostBack)
                    gvB.DataBind();
            }

I've called the databind method for gvB in every possible place known to man and the same problem persists...Fix problem A gives me problem B and fixing problem B gives me problem A. 我已经在人们可能知道的每个可能的地方调用了gvB的databind方法,并且相同的问题仍然存在...解决问题A可以解决问题B,解决问题B可以解决问题A.

Any ideas would be great. 任何想法都很棒。 I can see this being something ridiculously silly but I've stared at the code almost all day now and I'm out of ideas. 我可以看到这有点可笑,但是我几乎整天都在盯着代码,但我没有想法。

You should use Page_PreRender event for your code and you can write your code as follow with else as well. 您应该对代码使用Page_PreRender事件,并且还可以按照else方式编写代码。

        if (grvSender.ID == "gvA")
        {
            if (!IsPostBack)
                gvB.DataBind();
        }

        else
        {
            if (IsPostBack)
                gvB.DataBind();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM