当Gridviews和用户控件位于更新面板内时,如何在多个用户控件中更新多个Gridview?

时间:2012-03-29 15:35:25

标签: asp.net gridview user-controls updatepanel

我有一个包含在其中的更新面板中的ASP占位符的页面,我加载了一个用户控件,其中另一个占位符包含在更新面板中。然后,我将这个最新的Place Holder加载了多个User Control,每个User Controls都包含一个包含在Update Panel中的Gridview。

每个Gridview中的数据都基于“之前”Gridviews中的数据,因此当用户编辑一行时,更改会沿着Gridviews向下级联,这就是我的问题所在。单击保存按钮后,没有任何明显的事情发生(数据库中的所有行都正确更新),直到我在页面的任何位置单击另一个按钮,此时所有更改都在Gridviews中可见地更新。

我已经尝试了我能想到的一切来解决这个问题,没有结束使用更新面板选项和位置,通过JavaScript触发额外的按钮点击以及我能找到的任何其他似乎相关的解决方案,尽管没有一个我离任何我想去的地方都很近。

如果我设法以一种任何人都能理解的方式传达我的问题,我会感激你的洞察力,如果你觉得我没有理解这些问题,请提出问题。

以下是三个级别和页面/用户控件/ usercontrol:

页:

<div id="divCustomerProductInput">
    <asp:UpdatePanel ID="udpSalesOrders" runat="server" UpdateMode="Always">
        <ContentTemplate>
            <asp:PlaceHolder ID="plhCsCustomerproductInput" runat="server"></asp:PlaceHolder>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

第一个用户控件加载到上面的占位符中:

<asp:UpdatePanel ID="udpSalesOrders" runat="server" UpdateMode="Always" >
    <ContentTemplate>
        <asp:PlaceHolder runat="server" ID="plhProductionProcess"></asp:PlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>

第二次用户控件多次加载到占位符中:

<asp:UpdatePanel ID="udpPPI" runat="server" UpdateMode="Always">
        <ContentTemplate>
            <%--  MANUFACTURE--%>
            <asp:GridView ID="gdvProductionProcessIngredients" AutoGenerateColumns="false" runat="server">
                <Columns>
                    <asp:TemplateField Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label></ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Ingredient Description" ItemStyle-Width="21%" ControlStyle-Width="95%">
                        <ItemTemplate>
                            <asp:Label ID="txtIngredientDescription" runat="server" Text='<%# Eval("IngredientDescription") %>'></asp:Label></ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Ingredient Code" ItemStyle-Width="14%" ControlStyle-Width="95%">
                        <ItemTemplate>
                            <asp:Label ID="txtIngredientCode" runat="server" Text='<%# Eval("RmId") %>'></asp:Label></ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="From" ItemStyle-Width="10%" ControlStyle-Width="95%">
                        <ItemTemplate>
                            <asp:Label ID="txtParentDepartment" runat="server" Text='<%# Eval("ParentDepartment") %>'></asp:Label></ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Quantity (%)" ItemStyle-Width="10%" ControlStyle-Width="95%">
                        <ItemTemplate>
                            <asp:TextBox ID="txtQuantityKg" runat="server" Text='<%# Eval("OldestAncestorQuantityPercent") %>'
                                ForeColor="Black"></asp:TextBox></ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Input Cost / Kg (£)" ItemStyle-Width="13%" ControlStyle-Width="95%">
                        <ItemTemplate>
                            <asp:Label ID="lblCost" runat="server" Text='<%# Eval("InputCostKg") %>'></asp:Label></ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Yield (%)" ItemStyle-Width="10%" ControlStyle-Width="95%">
                        <ItemTemplate>
                            <asp:TextBox ID="txtYield" runat="server" Text='<%# Eval("Yield") %>' ForeColor="Black"></asp:TextBox></ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Output Cost / Kg (£)" ItemStyle-Width="14%" ControlStyle-Width="95%">
                        <ItemTemplate>
                            <asp:Label ID="lblCostPerKg" runat="server" Text='<%# Eval("OutputCostKg") %>'></asp:Label></ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-Width="6%" ControlStyle-Width="95%" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Button ID="btnDeleteIngredient" CommandName="REMOVE" CommandArgument='<%# Eval("Id") %>'
                                CssClass="btn danger hover" runat="server" Text="Remove" /></ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

好的,这是绑定和更新代码:

  Dim myCsProductProductionProcessIngredients As New v2.Model.CsProductProductionProcessIngredientCollection
    myCsProductProductionProcessIngredients.LoadByPPPId(_ProductProductionProcessId)

    Me.gdvProductionProcessIngredients.DataSource = myCsProductProductionProcessIngredients
    Me.gdvProductionProcessIngredients.DataBind()

    Me.udpPPI.Update()

谢谢,Korv

2 个答案:

答案 0 :(得分:1)

如果需要在单独的更新面板中更新控件,请将updatepanel updatemode更改为“conditional”,然后在需要更新面板时,可以调用更新面板的Update方法。

例如,如果udpPPI是plhProductionProcess更新时需要更新的面板。

<asp:UpdatePanel ID="udpPPI" runat="server" UpdateMode="Conditional">

更新plhProductionProcess中的控件后,调用:

updPPI.Update()

答案 1 :(得分:0)

我弄清楚为什么我的更新面板似乎没有更新。

我对页面生命周期做了一个不正确的假设,并假设按钮点击触发的事件在页面加载之前出现。

我现在已经将Gridview填充代码移动到Page_PreRender,一切都按照我的意图运行。