Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updatepanel only updates first time then wont update again

I am using a GridView within an updatepanel. I have gridview generated edit and cancel buttons.

The first time I click edit, the gridview edit template displays fine. Then if I click cancel, or edit on another row, nothing happens. It seems that the update panel has ceased to work.

If I do the same thing without the update panel the postback works fine and the gridview does what it should (although it's quite cumbersome as it refreshes the whole page, which is why I want to use the updatepanel!)

        <asp:UpdatePanel ID="upSentOrders" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:GridView ID="gvSentOrders" runat="server"
                        autogeneratecolumns="False"
                        allowpaging="false" 
                        DataKeyNames="titxn_id" 
                        AlternatingRowStyle-CssClass="gvAlternate"                  
                        CssClass="gvTable" 
                        OnRowDataBound="addSentTotals" 
                        OnRowEditing="editOrder" 
                        OnRowCancelingEdit="cancelEdit"     
                        OnDataBound="showSentTotals"                    
                        AutoGenerateEditButton="true" 
                        AutoGenerateDeleteButton="false" 
                        ShowFooter="true" 
                        ShowHeader="true">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <table class="basketHeader">
                                <tr>
                                    <td class="basketTitle"><asp:Label ID="lblTitle" runat="server" Text="Order Date" /></td>
                                    <td class="basketPX"><asp:Label ID="lblPXOffer" runat="server" Text="Part Ex" /></td>
                                    <td class="basketCash"><asp:Label ID="lblCashOffer" runat="server" Text="Cash" /></td>
                                    <td class="basketDelete">&nbsp;</td>
                                </tr>                                
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table class="basket">
                                <tr>
                                    <td class="basketTitle"><asp:Label ID="lblTitle" runat="server" Text='<%# String.Format("{0:dd MMMM yyyy}", Eval("titxn_date")) %>' /></td>
                                    <td class="basketPX"><asp:Label ID="lblPXOfferItem" runat="server" Text='<%# "£" + Eval("titxn_totalpxatsend") %>' /></td>
                                    <td class="basketCash"><asp:Label ID="lblCashOfferItem" runat="server" Text='<%# "£" + Eval("titxn_totalcashatsend") %>' /></td>
                                    <td class="basketDelete"><asp:Button ID="btnEdit" Text="" CommandArgument="1" runat="server" CssClass="editButton" ToolTip="View Order" OnClick="viewOrder" /></td>
                                </tr>                                
                            </table>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <table class="basket">
                                <tr>
                                    <td class="basketTitle"><asp:Label ID="lblTitle" runat="server" Text='<%# String.Format("{0:dd MMMM yyyy}", Eval("titxn_date")) %>' /></td>
                                    <td class="basketPX"><asp:Label ID="lblPXOfferItem" runat="server" Text='<%# "£" + Eval("titxn_totalpxatsend") %>' /></td>
                                    <td class="basketCash"><asp:Label ID="lblCashOfferItem" runat="server" Text='<%# "£" + Eval("titxn_totalcashatsend") %>' /></td>
                                    <td class="basketDelete"><asp:Button ID="btnStopEdit" Text="" CommandArgument="-1" runat="server" CssClass="stopEditButton" ToolTip="View Order" OnClick="viewOrder" /></td>
                                </tr>                                
                                <tr>
                                    <td colspan="4">
                                        Items GridView here...
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="4">
                                        <asp:Label ID="lblAddress" runat="server" Text='<%# Eval("titxn_name") + ", " + Eval("titxn_addressnumber") + " " + Eval("titxn_addressone") + ", " + Eval("titxn_addresstown") + ", " + Eval("titxn_addresscounty") + ", " + Eval("titxn_addresscountry") + ", " + Eval("titxn_addresspostcode") + "." %>' />   
                                    </td>
                                </tr>
                            </table>                        
                        </EditItemTemplate>
                        <FooterTemplate>
                            <table class="basketTotals">
                                <tr>
                                    <td class="basketTitle">Total Still To Arrive</td>
                                    <td class="basketPX"><asp:Label ID="lblPxTotal" runat="server" Text="" /></td>
                                    <td class="basketCash"><asp:Label ID="lblCashTotal" runat="server" Text="" /></td>
                                    <td class="basketDelete">&nbsp;</td>
                                </tr>                                
                            </table>                  
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    <p>There is no orders still being processed.  Look like we've already completed all the orders you've sent us!</p>
                </EmptyDataTemplate>
            </asp:GridView>    
        </ContentTemplate>
    </asp:UpdatePanel>

Code behind....

protected void editOrder(object sender, GridViewEditEventArgs e)
{
    gvSentOrders.EditIndex = e.NewEditIndex;
    bindSentGridView();
}

protected void cancelEdit(object sender, GridViewCancelEditEventArgs e)
{
    gvSentOrders.EditIndex = -1;
    bindSentGridView();
}

Any thoughts would be a great help. Thanks.

like image 701
Ben Drury Avatar asked Jan 24 '12 14:01

Ben Drury


2 Answers

It was a JS error from another bit of code that stopped the AJAX updating the panel.

like image 147
Ben Drury Avatar answered Oct 17 '22 14:10

Ben Drury


Since the UpdatePanel has a property UpdateMode="Conditional", you need to update it from the codebehind on every postback:

protected void editOrder(object sender, GridViewEditEventArgs e)
{
    gvSentOrders.EditIndex = e.NewEditIndex;
    bindSentGridView();
    upSentOrders.Update();
}

protected void cancelEdit(object sender, GridViewCancelEditEventArgs e)
{
    gvSentOrders.EditIndex = -1;
    bindSentGridView();
    upSentOrders.Update();
}

Hope this helps.

like image 23
aleafonso Avatar answered Oct 17 '22 14:10

aleafonso