简体   繁体   中英

ASP.NET GridView inside UpdatePanel doesn't update until second click

I've looked for answers online but I have not found any answer. I have a GridView inside an UpdatePanel and one Linkbutton column to show some detail in a Bootstrap modal. I had a problem that when the user clicks on the LinkButton the event OnClick doesn't fire, so, to solve that I made a fuction in Javascript that only causes a click in another LinkButton to fire the OnClick event.

Now it works fine, but nothing changes until the second click.

ASP.NET code

<asp:UpdatePanel ID="upContent" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <table align="center">
            <tr>
                <td>
                    <asp:GridView ID="grvOrderTracing" runat="server" EmptyDataText="There are no pending orders for the selected date" AutoGenerateColumns="False" DataSourceID="SqlDTSgetOrdersByDate" CssClass="table table-striped table-hover table-responsive" GridLines="None" AllowPaging="True" PageSize="8" >
                        <PagerStyle CssClass="pagination-ys" />
                        <Columns>
                            <asp:BoundField DataField="Order ID" HeaderText="Order ID" SortExpression="Order ID" >
                                <ControlStyle CssClass="hide" />
                                <FooterStyle CssClass="hide" />
                                <HeaderStyle CssClass="hide" />
                                <ItemStyle CssClass="hide" />
                            </asp:BoundField>
                            <asp:TemplateField HeaderText="Order Number">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lbtShowMoreInfo" runat="server" data-toggle="modal" data-target="#showLog" CommandName="OrderID" Text='<%# Bind("[Order Number]") %>' style="cursor: pointer; font-weight: bold;" OnClientClick="ActivityLog(this);">
                                    </asp:LinkButton>
                                    <asp:LinkButton ID="lbtActivityLog" runat="server" CommandName="OrderID" CssClass="hide" OnClick="ActivityLog_Click" Text='<%# Bind("[Order ID]") %>' ></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDTSgetOrdersByDate" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="sp_getOrdersByDate" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False">
                        <SelectParameters>
                            <asp:Parameter DefaultValue="0" Name="filter" Type="Int32" />
                            <asp:Parameter Name="startDate" Type="String" />
                            <asp:Parameter Name="endDate" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </td>
            </tr>
        </table>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="upLog" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <!-- Modal -->
        <div id="showLog" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog modal-lg" >
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                         <h4 class="modal-title" id="myModalLabel">Orden Log</h4>
                    </div>
                    <div class="modal-body">
                        <asp:GridView ID="grvLog" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDTSgetTracing" CssClass="table table-striped table-hover table-responsive" GridLines="None" >
                            <Columns>
                                <asp:BoundField DataField="Movement Date" HeaderText="Movement Date" SortExpression="Movement Date" ReadOnly="true" DataFormatString="{0:dd/MM/yyyy HH:mm}" />
                                <asp:BoundField DataField="User" HeaderText="User" SortExpression="User" ReadOnly="true" />
                                <asp:BoundField DataField="Action" HeaderText="Action" SortExpression="Action" ReadOnly="true" />
                                <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments"/>
                             </Columns>
                         </asp:GridView>
                         <asp:SqlDataSource ID="SqlDTSgetTracing" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="sp_getTracing" SelectCommandType="StoredProcedure">
                             <SelectParameters>
                                 <asp:Parameter  DefaultValue="0" Name="ord_id" Type="Int32"/>
                             </SelectParameters>
                         </asp:SqlDataSource>
                     </div>
                 </div>
             </div>
         </div>
     </ContentTemplate>
</asp:UpdatePanel>

Javascript code

function ActivityLog(lbt) {
    lbt.nextSibling.nextSibling.click(); //Find the other LinkButton and click
}

C# Code

protected void ActivityLog_Click(object sender, EventArgs e)
{
    SqlDTSgetTracing.SelectParameters["ord_id"].DefaultValue = ((LinkButton)sender).Text;
    SqlDTSgetTracing.Select(DataSourceSelectArguments.Empty);
    grvLog.DataBind();
    upLog.Update();
}

When clicking the first time

When clicnking the seccond time

After clicking twice now the data is displayed in the GridView.

I want that from the first click the data being displayed in the GridView.

I think you might want to append return false; to the end of OnClientClick to prevent the rest of that control's click event (coded by ASP.NET for you) from running. That will ensure only the LinkButton you care about will postback.

OnClientClick="ActivityLog(this);return false;"

Not sure if that will fix the need for clicking twice or not, but maybe!

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