这次我们设计员工操作列表Custom.aspx和部门操作列表Departmant.aspx的设计.以及如何设计GridView的分页功能.
先调用BLL设计方法获取数据的List列表,转换成PagedDataSource,再设置PagedDataSource属性来实现分页功能.
先看Custom.aspx的设计:
<div style="text-align: center"> <asp:GridView ID="gvCustom" DataKeyNames="ID" OnRowDeleting="GridView_RowDeleting" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" HorizontalAlign="Center" Width="98%" CellSpacing="1" AutoGenerateColumns="False"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" /> <EditRowStyle BackColor="#2461BF" Font-Italic="True" HorizontalAlign="Center" VerticalAlign="Middle" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="White" Font-Size="12px" HorizontalAlign="Center" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="id" HeaderText="ID" /> <asp:BoundField DataField="ename" HeaderText="帐户名" /> <asp:BoundField DataField="cname" HeaderText="姓名" /> <asp:BoundField DataField="age" HeaderText="年龄" /> <asp:BoundField DataField="departname" HeaderText="部门" /> <asp:HyperLinkField DataTextField="id" DataTextFormatString="修改" HeaderText="修改" DataNavigateUrlFields="id" DataNavigateUrlFormatString="Updatecustom.aspx?id={0}" /> <asp:TemplateField HeaderText="删除"> <ItemTemplate> <asp:LinkButton ID="LinkButton2" OnClientClick="return confirm('您确认删除该记录吗?');" runat="server" CommandName="Delete" Font-Names="宋体" Font-Strikeout="False" Font-Underline="False" ForeColor="Black">删除</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID="btnAddUser" runat="server" onclick="btnAddUser_Click" Text="添加用户" /> 当前页码为:<asp:Label ID="labPage" runat="server" Text="1"></asp:Label> 总页码为:<asp:Label ID="labBackPage" runat="server"></asp:Label> <asp:LinkButton ID="lnkbtnOne" runat="server" OnClick="lnkbtnOne_Click">第一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnUp" runat="server" OnClick="lnkbtnUp_Click">上一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnBack" runat="server" OnClick="lnkbtnBack_Click">最后一页</asp:LinkButton> </div>
现来看下Custom.aspx.cs的设计:
记在先加上:
using BLL; using Model;
然后再:
public partial class Custom : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //验证权限 IsAdmin(); BinData(); } } /// <summary> /// 绑定数据 /// </summary> private void BinData() { customSystem CustomSystem = new customSystem(); List<custom> Customlist = new List<custom>(); //调用BLL方法获取列表 Customlist = CustomSystem.GetCustom(); int curpage = Convert.ToInt32(this.labPage.Text); //设计分页 PagedDataSource ps = new PagedDataSource(); ps.DataSource = Customlist; ps.AllowPaging = true; //设置页面的条数 ps.PageSize = 16; ps.CurrentPageIndex = curpage - 1; this.lnkbtnUp.Enabled = true; this.lnkbtnNext.Enabled = true; this.lnkbtnBack.Enabled = true; this.lnkbtnOne.Enabled = true; if (curpage == 1) { this.lnkbtnOne.Enabled = false; this.lnkbtnUp.Enabled = false; } if (curpage == ps.PageCount) { this.lnkbtnNext.Enabled = false; this.lnkbtnBack.Enabled = false; } this.labBackPage.Text = Convert.ToString(ps.PageCount); //绑定数据源 gvCustom.DataSource = ps; gvCustom.DataBind(); } /// <summary> /// 删除一条数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { int nid = Int32.Parse(gvCustom.DataKeys[e.RowIndex].Value.ToString()); customSystem CustomSystem = new customSystem(); CustomSystem.Deletecustom(nid); BinData(); } /// <summary> /// 第一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lnkbtnOne_Click(object sender, EventArgs e) { this.labPage.Text = "1"; BinData(); } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lnkbtnUp_Click(object sender, EventArgs e) { this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) - 1); BinData(); } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lnkbtnNext_Click(object sender, EventArgs e) { this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) + 1); BinData(); } /// <summary> /// 最后一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lnkbtnBack_Click(object sender, EventArgs e) { this.labPage.Text = this.labBackPage.Text; BinData(); } /// <summary> /// 跳转到增加页面 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnAddUser_Click(object sender, EventArgs e) { Response.Redirect("Register.aspx"); } }
<div style="text-align: center"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" CellSpacing="1" DataKeyNames="ID" ForeColor="#333333" GridLines="None" HorizontalAlign="Center" OnRowDeleting="GridView_RowDeleting" Width="98%"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" /> <EditRowStyle BackColor="#2461BF" Font-Italic="True" HorizontalAlign="Center" VerticalAlign="Middle" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="Silver" Font-Bold="True" Font-Size="12px" ForeColor="White" HorizontalAlign="Center" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="id" HeaderText="ID" /> <asp:BoundField DataField="departname" HeaderText="部门名称" /> <asp:BoundField DataField="description" HeaderText="部门描述" /> <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="Updatedepart.aspx?id={0}" DataTextField="id" DataTextFormatString="修改" HeaderText="修改" /> <asp:TemplateField HeaderText="删除"> <ItemTemplate> <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Font-Names="宋体" Font-Strikeout="False" Font-Underline="False" ForeColor="Black" OnClientClick="return confirm('您确认删除该记录吗?');">删除</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID="btnAddDepartment" runat="server" onclick="btnAddDepartment_Click" Text="添加部门" /> 当前页码为:<asp:Label ID="labPage" runat="server" Text="1"></asp:Label> 总页码为:<asp:Label ID="labBackPage" runat="server"></asp:Label> <asp:LinkButton ID="lnkbtnOne" runat="server" OnClick="lnkbtnOne_Click">第一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnUp" runat="server" OnClick="lnkbtnUp_Click">上一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnBack" runat="server" OnClick="lnkbtnBack_Click">最后一页</asp:LinkButton> </div>
然后再看Departmant.aspx.cs的设计:
也一样要先加上:
using BLL; using Model;
然后再:
public partial class departmant : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //验证权限 IsAdmin(); BinData(); } } /// <summary> /// 绑定数据 /// </summary> private void BinData() { departmentSystem DepartmentSystem = new departmentSystem(); List<department> Departmentlist = new List<department>(); //调用BLL方法获取列表 Departmentlist = DepartmentSystem.GetDepartment(); int curpage = Convert.ToInt32(this.labPage.Text); //设计分页 PagedDataSource ps = new PagedDataSource(); ps.DataSource = Departmentlist; ps.AllowPaging = true; //设置页面的条数 ps.PageSize = 16; ps.CurrentPageIndex = curpage - 1; this.lnkbtnUp.Enabled = true; this.lnkbtnNext.Enabled = true; this.lnkbtnBack.Enabled = true; this.lnkbtnOne.Enabled = true; if (curpage == 1) { this.lnkbtnOne.Enabled = false; this.lnkbtnUp.Enabled = false; } if (curpage == ps.PageCount) { this.lnkbtnNext.Enabled = false; this.lnkbtnBack.Enabled = false; } this.labBackPage.Text = Convert.ToString(ps.PageCount); //绑定数据源 GridView1.DataSource = ps; GridView1.DataBind(); } /// <summary> /// 删除一条数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { int nid = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); departmentSystem DepartmentSystem = new departmentSystem(); DepartmentSystem.Deletedepart(nid); BinData(); } /// <summary> /// 第一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lnkbtnOne_Click(object sender, EventArgs e) { this.labPage.Text = "1"; BinData(); } /// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lnkbtnUp_Click(object sender, EventArgs e) { this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) - 1); BinData(); } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lnkbtnNext_Click(object sender, EventArgs e) { this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) + 1); BinData(); } /// <summary> /// 最后一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lnkbtnBack_Click(object sender, EventArgs e) { this.labPage.Text = this.labBackPage.Text; BinData(); } /// <summary> /// 跳转到增加页面 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnAddDepartment_Click(object sender, EventArgs e) { Response.Redirect("ADDdepart.aspx"); } }
下次我会做下这个系列的导航出来,并公布源码.欢迎继续拍砖.