1 实现将student中的所有信息在一个模板页上显示,格式参考课件中的图片。并实现编辑功能。
前台代码:
<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="_12_6.Page1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
InsertCommand="INSERT INTO [student] ([sname], [classid], [sex], [age], [isking], [photo]) VALUES (@sname, @classid, @sex, @age, @isking, @photo)"
SelectCommand="SELECT * FROM [student]"
UpdateCommand="UPDATE [student] SET [sname] = @sname, [classid] = @classid, [sex] = @sex, [age] = @age, [isking] = @isking, [photo] = @photo WHERE [sid] = @sid">
<DeleteParameters>
<asp:Parameter Name="sid" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
<asp:Parameter Name="sid" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
PageSize="3" Width="394px"
Xonpageindexchanging="GridView1_PageIndexChanging"
Xonrowdatabound="GridView1_RowDataBound" ShowFooter="True">
<PagerSettings FirstPageText="|&lt;" LastPageText="&gt;|"
Mode="NextPreviousFirstLast" />
<PagerTemplate>
<asp:Button ID="Button1" runat="server" CommandArgument="First"
CommandName="page" Text="|<" Width="30px" />
<asp:Button ID="button2" runat="server" CommandArgument="prev"
CommandName="page" Text="<" Width="30px" />
<asp:Button ID="Button3" runat="server" CommandArgument="next"
CommandName="page" Text=">" Width="30px" />
<asp:Button ID="Button4" runat="server" CommandArgument="Last"
CommandName="page" Text=">|" Width="30px" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</PagerTemplate>
</asp:GridView>
<br />
<br />
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
PageSize="3" Width="394px"
Xonpageindexchanging="GridView1_PageIndexChanging"
Xonrowdatabound="GridView1_RowDataBound" ShowFooter="True">
<PagerSettings FirstPageText="|&lt;" LastPageText="&gt;|"
Mode="NextPreviousFirstLast" Visible="False" />
<PagerTemplate>
<asp:Button ID="Button1" runat="server" CommandArgument="First"
CommandName="page" Text="|<" Width="30px" />
<asp:Button ID="button2" runat="server" CommandArgument="prev"
CommandName="page" Text="<" Width="30px" />
<asp:Button ID="Button3" runat="server" CommandArgument="next"
CommandName="page" Text=">" Width="30px" />
<asp:Button ID="Button4" runat="server" CommandArgument="Last"
CommandName="page" Text=">|" Width="30px" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</PagerTemplate>
</asp:GridView>
<br />
<asp:Button ID="btnFirst" runat="server" Xοnclick="btnFirst_Click" Text="第一页" />
<asp:Button ID="btnPrev" runat="server" Xοnclick="btnPrev_Click" Text="上一页" />
<asp:Button ID="btnNext" runat="server" Xοnclick="btnNext_Click" Text="下一页" />
<asp:Button ID="btnLast" runat="server" Xοnclick="btnLast_Click" Text="末一页" />
</div>
</form>
</body>
</html>
2 将模版列功能改造成博客内容列表, 并使完成分页 (使用分页模板完成,显示上一页,下一页,第一页,最后一页,和当前第x页,共y页 。)
前台代码:
<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="_12_6.Page1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
InsertCommand="INSERT INTO [student] ([sname], [classid], [sex], [age], [isking], [photo]) VALUES (@sname, @classid, @sex, @age, @isking, @photo)"
SelectCommand="SELECT * FROM [student]"
UpdateCommand="UPDATE [student] SET [sname] = @sname, [classid] = @classid, [sex] = @sex, [age] = @age, [isking] = @isking, [photo] = @photo WHERE [sid] = @sid">
<DeleteParameters>
<asp:Parameter Name="sid" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
<asp:Parameter Name="sid" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
PageSize="3" Width="394px"
Xonpageindexchanging="GridView1_PageIndexChanging"
Xonrowdatabound="GridView1_RowDataBound" ShowFooter="True">
<PagerSettings FirstPageText="|&lt;" LastPageText="&gt;|"
Mode="NextPreviousFirstLast" />
<PagerTemplate>
<asp:Button ID="Button1" runat="server" CommandArgument="First"
CommandName="page" Text="|<" Width="30px" />
<asp:Button ID="button2" runat="server" CommandArgument="prev"
CommandName="page" Text="<" Width="30px" />
<asp:Button ID="Button3" runat="server" CommandArgument="next"
CommandName="page" Text=">" Width="30px" />
<asp:Button ID="Button4" runat="server" CommandArgument="Last"
CommandName="page" Text=">|" Width="30px" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</PagerTemplate>
</asp:GridView>
<br />
<br />
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
PageSize="3" Width="394px"
Xonpageindexchanging="GridView1_PageIndexChanging"
Xonrowdatabound="GridView1_RowDataBound" ShowFooter="True">
<PagerSettings FirstPageText="|&lt;" LastPageText="&gt;|"
Mode="NextPreviousFirstLast" Visible="False" />
<PagerTemplate>
<asp:Button ID="Button1" runat="server" CommandArgument="First"
CommandName="page" Text="|<" Width="30px" />
<asp:Button ID="button2" runat="server" CommandArgument="prev"
CommandName="page" Text="<" Width="30px" />
<asp:Button ID="Button3" runat="server" CommandArgument="next"
CommandName="page" Text=">" Width="30px" />
<asp:Button ID="Button4" runat="server" CommandArgument="Last"
CommandName="page" Text=">|" Width="30px" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</PagerTemplate>
</asp:GridView>
<br />
<asp:Button ID="btnFirst" runat="server" Xοnclick="btnFirst_Click" Text="第一页" />
<asp:Button ID="btnPrev" runat="server" Xοnclick="btnPrev_Click" Text="上一页" />
<asp:Button ID="btnNext" runat="server" Xοnclick="btnNext_Click" Text="下一页" />
<asp:Button ID="btnLast" runat="server" Xοnclick="btnLast_Click" Text="末一页" />
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace _12_6
{
public partial class Page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindStudent();
}
}
private void BindStudent()
{
string sql = "select * from student";
DataTable dt = sqlHealpler.SqlHealper.ExecuteDataTable(sql);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
this.GridView2.DataSource = dt;
this.GridView2.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ( e.Row.RowType==DataControlRowType.Pager)
{
(e.Row.Cells[0].FindControl("Label1") as Label).Text = "第"+(GridView1.PageIndex+1).ToString()+"页,共"+GridView1.PageCount+"页";
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (e.NewPageIndex>=0)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.BindStudent();
}
}
protected void btnFirst_Click(object sender, EventArgs e)
{
this.GridView2.PageIndex = 0;
this.BindStudent();
}
protected void btnLast_Click(object sender, EventArgs e)
{
this.GridView2.PageIndex = this.GridView2.PageCount - 1;
this.BindStudent();
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index=this.GridView2.PageIndex;
if (index>=1)
{
index--;
}
this.GridView2.PageIndex = index;
this.BindStudent();
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index = this.GridView2.PageIndex;
if (index < this.GridView2.PageCount+1)
{
index++;
}
this.GridView2.PageIndex = index;
this.BindStudent();
}
}
}