放在網頁
<asp:GridView ID="GridView1" runat="server" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="學號">
<ItemTemplate>
<%#Eval("Student_ID", "{0}")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="學生姓名">
<ItemTemplate>
<%#Eval("Student_Name", "{0}")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
程式碼
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
show_data();
}
protected void show_data()
{
string strSql = "";
strSql += " SELECT [Student_ID],[Student_Name] FROM [Student]";
string strConnString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Student_ConnectionString"].ToString();
SqlConnection connect = new SqlConnection(strConnString);
connect.Open();
SqlDataAdapter da = new SqlDataAdapter(strSql, strConnString);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
connect.Close();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
show_data();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
留言列表