ModalPopupExtender中的Gridview分页奇怪的行为

时间:2013-08-16 22:44:14

标签: c# asp.net ajax modalpopupextender

我有一个包含网格视图的modalpopypextender,我想在按钮点击时填充它,这样做:

protected void btnViewRecipients_Click(object sender, EventArgs e)
{
    ModalPopupExtender1.Show();
    BindData();
}

这是直截了当的。 BindData这样做:

protected void BindData()
{
    try
    {
        SqlCommand sqlCommand = new SqlCommand();

        string connectionString = "Data Source=SERVER\\DB1;Initial Catalog=Survey;User ID=abcde;Password=12345;";

        using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        {
            sqlCommand = sqlConnection.CreateCommand();

            sqlCommand.CommandText = "Select * From [Survey].[dbo].[data]";

            SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connectionString);

            SqlCommandBuilder scb = new SqlCommandBuilder(sda);

            //Create a DataTable to hold the query results.

            //Fill the DataTable.
            sda.Fill(dTable);

            //Set the DataGridView DataSource.
            gvRecords.DataSource = dTable;
            gvRecords.DataBind();

            sqlConnection.Close();
        }
    }
    catch (SqlException ex)
    {
        //Console.WriteLine(ex.StackTrace);
    }
}

现在一切正常,我可以看到带有数据的网格。然后我打开了autopaging并继续创建调用gvRecords_PageIndexChanged。我还打开了EnableSortingAndPagingCallbacks。

protected void gvRecords_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvRecords.PageIndex = e.NewPageIndex;
    gvRecords.DataSource = dTable;
    gvRecords.DataBind();
}

这种做法非常奇怪。我注意到当我点击页码时,表格变成空白,并显示我之前定义的EmptyDataText。但当我关闭ModalPopupExtender并再次打开它(再次单击该按钮)时,它会向我显示正确的页面和数据!例如如果我点击第3页,然后得到一张空白表,现在重新打开MPE将在gridview中显示第3页的内容。我想这是存储在某处的视图状态,但为什么gridview不会立即向我显示该页面?

我真的被困在这里而且没理解我错过了什么!

任何帮助百万次的帮助,我已经在网上搜索和搜索了这个,但也许它是如此微不足道,显而易见,没有人需要问过!?

2 个答案:

答案 0 :(得分:0)

已编辑我多年来一直在使用Modals,UpdatePanels和ListViews,我们将解决这个问题,但看到整个标记会很好。

根据你的意见我建议;

  1. 将整个模态标记放在UpdatePanel中。确保将ID和UpdateMode设置为条件;

    <asp:UpdatePanel ID="upModal" runat="server" UpdateMode="Conditional">
       <ContentTemplate>      
       </ContentTemplate>
    </asp:UpdatePanel>
    
  2. 我通常在更新面板中使用ASP:Panel作为我的DIV;

    <asp:Panel ID="pnlPopup" runat="server" CssClass="ModalPanel">
    
  3. 然后将GridView(或我的案例ListView)放在面板中

  4. 在调用gvRecords.Databind()之后的代码中,调用upModal.Update()

    protected void gvRecords_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       gvRecords.PageIndex = e.NewPageIndex;
       gvRecords.DataSource = dTable;
       gvRecords.DataBind();
       upModal.Update();
    }
    

答案 1 :(得分:0)

我最终得到了它,我不得不将PopupControlID设置为upModal,即updatepanel的ID,而不是内部面板。 targetcontrolID也必须指向一个隐藏按钮,正如许多人在使用MPE时发现的那样......

无论如何,这里是:

    <asp:Button ID="hiddenButton" runat="server" Text="" style="display:none;" />
    <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" Enabled="True" TargetControlID="hiddenButton" PopupControlID="upModal" BehaviorID="modalbehavior" BackgroundCssClass="modalBackground"  OnCancelScript="cancelClick();" CancelControlID="closePopup">
    </ajaxToolkit:ModalPopupExtender>

    <asp:UpdatePanel runat="server" ID="upModal" UpdateMode="Conditional">
        <ContentTemplate>

            <asp:Panel id="pnlPopup" runat="server" class="ModalPanel" >

                <table cellpadding="5" cellspacing="5" class="topBanner" style="width:100%;">
                    <tr>
                        <td width="50">
                            <asp:LinkButton ID="closePopup" runat="server" onclick="LinkButton1_Click" CssClass="ClosePopupCls">Close 
                            [x]</asp:LinkButton>
                        </td>
                        <td align="center">
                            <asp:Label ID="lbl" runat="server" Text="Status"></asp:Label>
                        </td>
                        <td width="25">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <asp:GridView ID="gvRecords" runat="server" AllowPaging="True" 
                                BackColor="White" EmptyDataText="No Record Found" 
                                EnableSortingAndPagingCallbacks="True" ForeColor="GrayText" Height="600" 
                                onpageindexchanging="gvRecords_PageIndexChanging" Width="800">
                            </asp:GridView>
                        </td>
                    </tr>
                </table>
            </asp:Panel>

        </ContentTemplate>
    </asp:UpdatePanel>