我有一个包含GridView的ModalPopUpExtender,我想在按钮单击时进行填充,实现以下功能:
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);
}
}
现在一切都很顺利,我可以看到带有数据的网格。然后我打开了自动分页并创建了 gvRecords_PageIndexChanged 方法调用。我还打开了 EnableSortingAndPagingCallbacks。
protected void gvRecords_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvRecords.PageIndex = e.NewPageIndex;
gvRecords.DataSource = dTable;
gvRecords.DataBind();
}
这个功能非常奇怪。我注意到当我点击页面号码时,表格会变成空白,并显示之前定义的EmptyDataText。但是,当我关闭ModalPopupExtender并再次打开它(再次点击按钮)时,它会向我显示正确的页面和数据!例如,如果我点击了第3页,然后得到一个空白表格,现在重新打开MPE将向我显示第3页内容的网格视图。我想这可能是viewstate存储在某个地方,但为什么网格视图不会立即向我显示页面呢?
我真的卡在这里,无法理解我错过了什么!
非常感谢任何帮助,我已经在网上搜索了很久,但也许这是如此微不足道和明显,以至于没有人需要问吗?!