GridView无法在UpdatePanel

时间:2019-10-11 09:55:51

标签: asp.net gridview webforms updatepanel

我在尝试在UpdatePanel中显示GridView时遇到问题。 首先,在其中使用GridView时不显示初始数据,而当我删除UpdatePanel时,将呈现并显示数据。

第二件事是我尝试通过Ajax插入记录后刷新datagrid。 我正在尝试使用点击JavaScript来做到这一点,但到目前为止没有结果。 这是我的aspx页面:

<asp:Panel ID='pSearchParams' runat="server">
                        <asp:ObjectDataSource ID="historyDataSource" runat="server"
                          TypeName="DataSourceAdapters.DataSourceAdapter"
                          SelectMethod="GetData">
                        </asp:ObjectDataSource>
                        <form runat="server" id="x">
                        <asp:ScriptManager
                          ID="ToolkitScriptManager1" runat="server" >
                        </asp:ScriptManager>
                        <ajax:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" OnLoad="UpdatePanel2_Load">
                          <ContentTemplate>
                              <div id="triggerDiv" style="display:none;">
                                <asp:Button ID="triggerButton" CausesValidation="false" runat="server" Text="" />
                              <div>
                            <asp:GridView ID="GridViewHistory" CssClass="GridStyle"
                                runat="server" AllowPaging="True" AllowSorting="True"
                                AutoGenerateColumns="False" CellPadding="10"
                                DataSourceID="historyDataSource"
                                ForeColor="#333333">
                                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                <Columns>
                                    <asp:BoundField DataField="CalculationDate" HeaderText="Date" ReadOnly="True" SortExpression="CalculationDate" />
                                    <asp:BoundField DataField="RoughId" HeaderText="Rough ID" ReadOnly="True" SortExpression="RoughId" />
                                </Columns>
                                <RowStyle BackColor="#EFF3FB" />
                                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                <AlternatingRowStyle BackColor="White" />
                            </asp:GridView>
                         </ContentTemplate>
                         <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="triggerButton" EventName="Click" />
                            <asp:PostBackTrigger ControlID="GridViewHistory" />
                        </Triggers>
                        </ajax:UpdatePanel>
                        </form>
                    </asp:Panel>

以及背后的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(triggerButton);
    }


    [WebMethod]
    public static void InsertHistoryItem()
    {
            DataSourceAdapters.DataSourceAdapter adapter = new DataSourceAdapters.DataSourceAdapter();
            adapter.Insert();

        }


    }

    protected void UpdatePanel2_Load(object sender, EventArgs e)
    {
        stepcutCalculationHistoryDataSource.Select();
        GridViewStepcutCalculationHistory.DataBind();
        UpdatePanel2.Update();
    }

奇怪的是,我在DevTools的网络标签中看到对UpdatePanel的请求已完成,并且响应在那里,但从未以html呈现。

3 个答案:

答案 0 :(得分:0)

ScriptManager控件应位于所有控件的顶部。
                        

答案 1 :(得分:0)

I have tried it is working fine. This is the sample code.


<form id="form1" runat="server">
     <asp:ScriptManager ID="scp1" runat="server">
     </asp:ScriptManager>
        <asp:UpdatePanel ID="upMain" runat="server">
            <ContentTemplate>
                <asp:Panel id="pnl1" runat="server">
                    <asp:GridView ID="GridViewHistory" CssClass="GridStyle"
                                runat="server" AllowPaging="True" AllowSorting="True"
                                AutoGenerateColumns="False" CellPadding="10"
                                DataSourceID="historyDataSource"
                                ForeColor="#333333">
                                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                <Columns>
                                    <asp:BoundField DataField="CalculationDate" HeaderText="Date" ReadOnly="True" SortExpression="CalculationDate" />
                                    <asp:BoundField DataField="RoughId" HeaderText="Rough ID" ReadOnly="True" SortExpression="RoughId" />
                                </Columns>
                                <RowStyle BackColor="#EFF3FB" />
                                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                <AlternatingRowStyle BackColor="White" />
                            </asp:GridView>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>

答案 2 :(得分:0)

我修复了它。最后,这是一个导致问题的不正确封闭的div元素。 我为此失去了理智。