【问题标题】:How do I update multiple Gridviews across multiple User Controls while the Gridviews and User Controls are inside Update Panels?当 Gridview 和用户控件位于更新面板中时,如何跨多个用户控件更新多个 Gridview?
【发布时间】:2012-03-29 15:35:25
【问题描述】:

我有一个页面,其中包含一个包含在更新面板中的 ASP 占位符,我加载了一个用户控件和另一个包含在更新面板中的占位符。然后,我使用多个用户控件加载这个最新的占位符,每个用户控件都包含一个包装在更新面板中的 Gridview。

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

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

如果我设法以任何人都能理解的方式表达我的问题,我将不胜感激,如果您觉得我描述的内容不够清晰,请提出问题。

这里是三个级别和页面/用户控件/用户控件:

页面:

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

谢谢,科尔夫

【问题讨论】:

  • 请发布一些示例代码。

标签: asp.net gridview user-controls updatepanel


【解决方案1】:

如果您需要在单独的更新面板中更新某个控件,请将 updatepanel 的更新模式更改为“有条件”,然后当您需要更新面板时,您可以调用更新面板的 Update 方法。

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

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

一旦 plhProductionProcess 中的控件更新调用:

updPPI.Update()

【讨论】:

  • 我现在尝试在填充每个占位符后调用更新,但单击保存按钮后我仍然看不到更新的 Gridview。
  • 您能否发布代码以显示您如何绑定数据网格和更新面板?
  • 是的,我会在星期一这样做,如果你能回来看看那会很酷
【解决方案2】:

我知道为什么我的更新面板似乎没有更新。

我对页面生命周期做出了错误的假设,并假设按钮点击触发的事件发生在页面加载之前。

我现在已将 Gridview 填充代码移至 Page_PreRender,一切都按我的预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2021-05-14
    • 1970-01-01
    相关资源
    最近更新 更多