简体   繁体   中英

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. There is a nested gridview inside the other. I also have two update panels. The child gridview is inside parent updatepanel. at the end of parent updatepanel, child update panel starts, that has a button.

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. I can also find the child updatepanel and I am using the update method. 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:

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:

<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. There are no layout issues.

When you use the UpdateMode="Conditional" then you have to set the ChildrenAsTriggers="False" .

And manually trigger your button(s) or other controls events as AsyncPostBackTrigger like:

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

and manually update your updatepanel as 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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