我有一个网格视图,设置为进行分页,但它无法正常工作。
只有第一页的控件是可见的 - 其他页面有呈现的框,但其中没有控件。
有谁知道为什么会这样?我检查过我有不止一页的数据。
谢谢,
奥利弗
我附上了一张屏幕截图,希望能说明我的问题。
https://i.sstatic.net/NOFnB.jpg
编辑:gridview 的来源
<asp:GridView ID="GridView1" runat="server" OnPageIndexChanging="GridView1_PageIndexChanging"
CssClass="GridView1" OnSelectedIndexChanged="GridView_SelectedIndexChanged"
AllowPaging="True" PageSize="20">
<selectedrowstyle backcolor="LightCyan" forecolor="DarkBlue" font-bold="true" />
</asp:GridView>
它是使用 c# 中生成的数据集填充的
编辑:c# 代码隐藏
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView();
}
public void bindGridView()
{
//declare the connection string to use
string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
//create sql connection
SqlConnection mySQLconnection = new SqlConnection(connectionString);
//open connection
mySQLconnection.Open();
//define command using text string
SqlCommand mySqlCommand = new SqlCommand(sqlTester, mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
//create dataset to fill gridview with
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
//fill gridview
GridView1.DataSource = myDataSet;
GridView1.DataBind();
//close the sql connection
mySQLconnection.Close();
}
我认为根据你的
aspx
和你的.cs
。您的 css 文件存在一些问题“GridView1
”。尝试删除 css 类并告诉我们。问题是否还存在。
注:
这与你的问题无关。但我认为你应该分层分离你的代码,而不是将所有代码写在你的页面代码后面。
阅读:
您正在每个页面索引更改事件上进行连接和数据库旅行。它会导致大量的开销。试试这个。
public void bindGridView()
{
//declare the connection string to use
string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
//create sql connection
SqlConnection mySQLconnection = new SqlConnection(connectionString);
//open connection
mySQLconnection.Open();
//define command using text string
SqlCommand mySqlCommand = new SqlCommand(sqlTester, mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
//create dataset to fill gridview with
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
//fill gridview
GridView1.DataSource = myDataSet;
GridView1.DataBind();
viewstate.add("myDataSet",myDataSet);
//close the sql connection
mySQLconnection.Close();
}
在页面索引更改事件中,执行以下操作... protected void GridView1_PageIndexChanging(对象发送者,GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView.datasource=(DataTable)ViewState["myDataSet"); GridView.DataBind(); }
如果视图状态给出一些有关序列化的错误,请使用会话。这将使您的代码更加高效和优化。