问题:
以下代码能让用户通过点击LinkButton自由选看任何一个分页!但分页显示后,点击索引会导致
整个datagrid表都消失!已经设定了OnPageIndexChanged
方法但还是这样,应该怎么改才行?
<%@Import namespace="System.Data" %>
<%@Import namespace="System.Data.SqlClient"%>
<html>
<head>
<title>要分页显示</title>
</head>
<body>
<center>
<form id="form1" runat="server" method="post">
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="data1">LinkButton1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="data2">LinkButton2</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="data3">LinkButton3</asp:LinkButton>
<asp:DataGrid AllowPaging="true" ID="DataGrid1" PageSize="5" runat="server" ShowFooter="false" ShowHeader="true" OnPageIndexChanged="onindex" Height="213px" Width="582px">
<pagerstyle Mode="NumericPages" PageButtonCount="5" />
</asp:DataGrid>
</form>
</center>
</body>
</html>
<script language="C#" runat="server">
void onindex(Object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind( );
}
//------------------------------------
void data1(Object sender, EventArgs e)
{
SqlConnection SqlConnection1 = new SqlConnection("Server=Localhost;user id=sa;password=;database=database1;");
SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter("select * from table1", SqlConnection1);
DataSet DataSet1 = new DataSet();
SqlConnection1.Open();
SqlDataAdapter1.Fill(DataSet1);
SqlConnection1.Close();
DataGrid1.DataSource = DataSet1;
DataGrid1.DataBind();
}
void data2(Object sender, EventArgs e)
{
SqlConnection SqlConnection1 = new SqlConnection("Server=Localhost;user id=sa;password=;database=database1;");
SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter("select * from table2", SqlConnection1);
DataSet DataSet1 = new DataSet();
SqlConnection1.Open();
SqlDataAdapter1.Fill(DataSet1);
SqlConnection1.Close();
DataGrid1.DataSource = DataSet1;
DataGrid1.DataBind();
}
void data3(Object sender, EventArgs e)
{
SqlConnection SqlConnection1 = new SqlConnection("Server=Localhost;user id=sa;password=;database=database1;");
SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter("select * from table3", SqlConnection1);
DataSet DataSet1 = new DataSet();
SqlConnection1.Open();
SqlDataAdapter1.Fill(DataSet1);
SqlConnection1.Close();
DataGrid1.DataSource = DataSet1;
DataGrid1.DataBind();
}
</script>