简体   繁体   English

aspx Updatepanel不更新-包含Gridview

[英]aspx Updatepanel not updating - contains Gridview

I am trying to write some data to a database and then update the gridview with the updated data. 我试图将一些数据写入数据库,然后使用更新后的数据更新gridview。 There is a nested gridview inside the other. 另一个内部有一个嵌套的gridview。 I also have two update panels. 我也有两个更新面板。 The child gridview is inside parent updatepanel. 子gridview在父updatepanel中。 at the end of parent updatepanel, child update panel starts, that has a button. 在父级updatepanel的末尾,将启动子级更新面板,该面板有一个按钮。

On click I have managed to write to the DB, using find control, I can find the dynamic gridview, re-bind it to the data source. 单击后,我已经成功使用find控件写入数据库,可以找到动态gridview,然后将其重新绑定到数据源。 I can also find the child updatepanel and I am using the update method. 我还可以找到子级updatepanel,并且正在使用update方法。 but it is not updating. 但它没有更新。 I am getting no errors. 我没有任何错误。

<ItemTemplate>
  <img alt="" runat="server" style="cursor: pointer" src="images/plus.png" />
  <asp:UpdatePanel ID="pnlOrders" runat="server" Style="display: none"  UpdateMode="Conditional">
    <ContentTemplate>
      <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
        <Columns>
          <asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="Contact Name" />
          <asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="City" />
        </Columns> 
      </asp:GridView>
      <asp:UpdatePanel id="pnChild" runat="server" CssClass="ChildGrid" UpdateMode="Conditional">
        <ContentTemplate>
          <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
          <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("CustomerID") %>'></asp:TextBox>
          <asp:Button ID="Button1" runat="server" Text="Button" OnClick="MyButtonClick" />
        </ContentTemplate>
    </asp:UpdatePanel>
    </ContentTemplate>
  </asp:UpdatePanel>
</ItemTemplate>

C Sharp code: C Sharp代码:

GridView gvOrder = gvr.FindControl("gvOrders") as GridView;
gvOrder.DataSource = GetData(string.Format("select top 10 * from Orders where CustomerId='{0}'", cID));
gvOrder.DataBind();
UpdatePanel uPanel = gvr.FindControl("pnlOrders") as UpdatePanel;
uPanel.Update();

Html output: HTML输出:

<table class="Grid" cellspacing="0" rules="all" border="1" id="gvCustomers" style="border-collapse:collapse;">
    <tr>
        <th scope="col">&nbsp;</th><th scope="col">Contact Name</th><th scope="col">Contact Name</th><th scope="col">City</th>
    </tr><tr>
        <td>

                    <img src="images/plus.png" alt="" style="cursor: pointer" />
                     <div id="gvCustomers_pnlOrders_0">

                         <div>
                <table class="ChildGrid" cellspacing="0" rules="all" border="1" id="gvCustomers_gvOrders_0" style="border-collapse:collapse;">
                    <tr>
                        <th scope="col">Contact Name</th><th scope="col">City</th>
                    </tr><tr>
                        <td style="width:150px;">1</td><td style="width:150px;">19-05-18</td>
                    </tr><tr>
                        <td style="width:150px;">12</td><td style="width:150px;">25-05-18</td>

                    </tr>
                </table>
            </div>
           <div id="gvCustomers_pnChild_0" CssClass="ChildGrid">

           <input name="gvCustomers$ctl02$TextBox1" type="text" id="gvCustomers_TextBox1_0" />
           <input name="gvCustomers$ctl02$TextBox2" type="text" value="1" id="gvCustomers_TextBox2_0" />
           <input type="submit" name="gvCustomers$ctl02$Button1" value="Button" id="gvCustomers_Button1_0" />

            </div>

The HTML layout looks wrong, but that's because I have only posted output of one row. HTML布局看起来不对,但这是因为我只发布了一行的输出。 There are no layout issues. 没有布局问题。

When you use the UpdateMode="Conditional" then you have to set the ChildrenAsTriggers="False" . 使用UpdateMode="Conditional" ,必须设置ChildrenAsTriggers="False"

And manually trigger your button(s) or other controls events as AsyncPostBackTrigger like: 并以AsyncPostBackTrigger 手动触发按钮或其他控件事件,例如:

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>

and manually update your updatepanel as yourupdatepanel.Update() : 并手动将您的updatepanel更新为yourupdatepanel.Update()

UpdatePanel uPanel = gvr.FindControl("pnlOrders") as UpdatePanel;
uPanel.Update();

The issue seemed to be with nesting of the panels. 问题似乎在于面板的嵌套。 I changed the child updatepanel to just a panel and all seems to be working without needing a trigger declaration. 我将子级updatepanel更改为仅一个面板,并且似乎所有功能都可以正常运行,而无需触发声明。

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

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