天天看點

資料源(SqlDataSource 與 GridView)

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="|&amp;lt;" LastPageText="&amp;gt;|" 

Mode="NextPreviousFirstLast" />

<PagerTemplate>

<asp:Button ID="Button1" runat="server" CommandArgument="First" 

CommandName="page" Text="|&lt;" Width="30px" />

&nbsp;<asp:Button ID="button2" runat="server" CommandArgument="prev" 

CommandName="page" Text="&lt;" Width="30px" />

&nbsp;<asp:Button ID="Button3" runat="server" CommandArgument="next" 

CommandName="page" Text="&gt;" Width="30px" />

&nbsp;<asp:Button ID="Button4" runat="server" CommandArgument="Last" 

CommandName="page" Text="&gt;|" 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="|&amp;lt;" LastPageText="&amp;gt;|" 

Mode="NextPreviousFirstLast" Visible="False" />

<PagerTemplate>

<asp:Button ID="Button1" runat="server" CommandArgument="First" 

CommandName="page" Text="|&lt;" Width="30px" />

&nbsp;<asp:Button ID="button2" runat="server" CommandArgument="prev" 

CommandName="page" Text="&lt;" Width="30px" />

&nbsp;<asp:Button ID="Button3" runat="server" CommandArgument="next" 

CommandName="page" Text="&gt;" Width="30px" />

&nbsp;<asp:Button ID="Button4" runat="server" CommandArgument="Last" 

CommandName="page" Text="&gt;|" 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="第一頁" />

&nbsp;<asp:Button ID="btnPrev" runat="server" Xοnclick="btnPrev_Click" Text="上一頁" />

&nbsp;<asp:Button ID="btnNext" runat="server" Xοnclick="btnNext_Click" Text="下一頁" />

&nbsp;<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="|&amp;lt;" LastPageText="&amp;gt;|" 

Mode="NextPreviousFirstLast" />

<PagerTemplate>

<asp:Button ID="Button1" runat="server" CommandArgument="First" 

CommandName="page" Text="|&lt;" Width="30px" />

&nbsp;<asp:Button ID="button2" runat="server" CommandArgument="prev" 

CommandName="page" Text="&lt;" Width="30px" />

&nbsp;<asp:Button ID="Button3" runat="server" CommandArgument="next" 

CommandName="page" Text="&gt;" Width="30px" />

&nbsp;<asp:Button ID="Button4" runat="server" CommandArgument="Last" 

CommandName="page" Text="&gt;|" 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="|&amp;lt;" LastPageText="&amp;gt;|" 

Mode="NextPreviousFirstLast" Visible="False" />

<PagerTemplate>

<asp:Button ID="Button1" runat="server" CommandArgument="First" 

CommandName="page" Text="|&lt;" Width="30px" />

&nbsp;<asp:Button ID="button2" runat="server" CommandArgument="prev" 

CommandName="page" Text="&lt;" Width="30px" />

&nbsp;<asp:Button ID="Button3" runat="server" CommandArgument="next" 

CommandName="page" Text="&gt;" Width="30px" />

&nbsp;<asp:Button ID="Button4" runat="server" CommandArgument="Last" 

CommandName="page" Text="&gt;|" 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="第一頁" />

&nbsp;<asp:Button ID="btnPrev" runat="server" Xοnclick="btnPrev_Click" Text="上一頁" />

&nbsp;<asp:Button ID="btnNext" runat="server" Xοnclick="btnNext_Click" Text="下一頁" />

&nbsp;<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();

}

}

}