数据源(SqlDataSource 与 GridView)
来源:互联网 发布:linux for 循环判断 编辑:程序博客网 时间:2024/06/06 12:54
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" Xonclick="btnFirst_Click" Text="第一页" />
<asp:Button ID="btnPrev" runat="server" Xonclick="btnPrev_Click" Text="上一页" />
<asp:Button ID="btnNext" runat="server" Xonclick="btnNext_Click" Text="下一页" />
<asp:Button ID="btnLast" runat="server" Xonclick="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" Xonclick="btnFirst_Click" Text="第一页" />
<asp:Button ID="btnPrev" runat="server" Xonclick="btnPrev_Click" Text="上一页" />
<asp:Button ID="btnNext" runat="server" Xonclick="btnNext_Click" Text="下一页" />
<asp:Button ID="btnLast" runat="server" Xonclick="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();
}
}
}
- 数据源(SqlDataSource 与 GridView)
- 控件(七)——GridView控件以SqlDataSource控件为数据源显示数据
- 控件(五)——Gridview控件以SqlDataSource控件为数据源实现换肤功能
- GridView与SQLDataSource简单数据绑定
- SqlDataSource 数据源控件
- 数据源控件 SqlDataSource
- DevExpress数据源SqlDataSource使用
- GridView和DetailView实现增,删,改,查(数据源为后台绑定,非SqlDataSource)
- GridView和DetailView实现增,删,改,查(数据源为后台绑定,非SqlDataSource)
- Gridview+sqldatasource使用注意事项
- GridView + SqlDataSource + 分页+ 搜索
- SqlDataSource GridView 显示分类
- .NET学习(四)数据源控件SqlDataSource的使用
- SqlDataSource数据源控件参数问题
- 关于DropDownList绑定SqlDataSource数据源
- gridView和sqlDataSource的使用
- 使用sqldatasource动态绑定gridview
- sqldatasource bind gridview (date between)
- js控制图片滚动效果
- C#运行外部程序
- 10.9 DotNetBar for Windows Forms 破解
- Hdu 1114 Piggy-Bank
- 淘宝开放平台与拍拍开放平台
- 数据源(SqlDataSource 与 GridView)
- 64位 TortoiseSVN 连接 32位 SVN服务器报错问题
- Array.Copy(array1, array2, 2);
- js中继承的几种用法apply,call,prototype
- awk 实例练习(一)
- [sicily online]1048. Inverso
- awk 实例练习(二)
- 算法题1:二元查找树转换成有序双向链表
- windows7上帝模式