步步為營 .NET三層架構(gòu)解析 八、UI的設(shè)計(jì)(GridView的設(shè)計(jì)及其分頁)
文章錄入:7747.Net 責(zé)任編輯:7747.Net 22
【字體:小 大】
這次我們?cè)O(shè)計(jì)員工操作列表Custom.aspx和部門操作列表Departmant.aspx的設(shè)計(jì).以及如何設(shè)計(jì)GridView的分頁功能.
先調(diào)用BLL設(shè)計(jì)方法獲取數(shù)據(jù)的List列表,轉(zhuǎn)換成PagedDataSource,再設(shè)置PagedDataSource屬性來實(shí)現(xiàn)分頁功能.
先看Custom.aspx的設(shè)計(jì):
view sourceprint?<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('您確認(rèn)刪除該記錄嗎?');" 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="添加用戶" />
當(dāng)前頁碼為:<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>
現(xiàn)來看下Custom.aspx.cs的設(shè)計(jì):
記在先加上:
view sourceprint?using BLL;
using Model;
然后再:
view sourceprint?public partial class Custom : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//驗(yàn)證權(quán)限
IsAdmin();
BinData();
}
}
/// <summary>
/// 綁定數(shù)據(jù)
/// </summary>
private void BinData()
{
customSystem CustomSystem = new customSystem();
List<custom> Customlist = new List<custom>();
//調(diào)用BLL方法獲取列表
Customlist = CustomSystem.GetCustom();
int curpage = Convert.ToInt32(this.labPage.Text);
//設(shè)計(jì)分頁
PagedDataSource ps = new PagedDataSource();
ps.DataSource = Customlist;
ps.AllowPaging = true;
//設(shè)置頁面的條數(shù)
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);
//綁定數(shù)據(jù)源
gvCustom.DataSource = ps;
gvCustom.DataBind();
}
/// <summary>
/// 刪除一條數(shù)據(jù)
/// </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>
/// 跳轉(zhuǎn)到增加頁面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAddUser_Click(object sender, EventArgs e)
{
Response.Redirect("Register.aspx");
}
}
再來看下Departmant.aspx的設(shè)計(jì):
view sourceprint?<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('您確認(rèn)刪除該記錄嗎?');">刪除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnAddDepartment" runat="server" onclick="btnAddDepartment_Click"
Text="添加部門" />
當(dāng)前頁碼為:<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的設(shè)計(jì):
也一樣要先加上:
view sourceprint?using BLL;
using Model;
然后再:
view sourceprint?public partial class departmant : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//驗(yàn)證權(quán)限
IsAdmin();
BinData();
}
}
/// <summary>
/// 綁定數(shù)據(jù)
/// </summary>
private void BinData()
{
departmentSystem DepartmentSystem = new departmentSystem();
List<department> Departmentlist = new List<department>();
//調(diào)用BLL方法獲取列表
Departmentlist = DepartmentSystem.GetDepartment();
int curpage = Convert.ToInt32(this.labPage.Text);
//設(shè)計(jì)分頁
PagedDataSource ps = new PagedDataSource();
ps.DataSource = Departmentlist;
ps.AllowPaging = true;
//設(shè)置頁面的條數(shù)
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);
//綁定數(shù)據(jù)源
GridView1.DataSource = ps;
GridView1.DataBind();
}
/// <summary>
/// 刪除一條數(shù)據(jù)
/// </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>
/// 跳轉(zhuǎn)到增加頁面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAddDepartment_Click(object sender, EventArgs e)
{
Response.Redirect("ADDdepart.aspx");
}
}
到這里我們一個(gè)簡單的三層架構(gòu)的應(yīng)用就設(shè)計(jì)完了,可能權(quán)限的設(shè)計(jì)有點(diǎn)薄弱,改天我再專門講解下權(quán)限的設(shè)計(jì),感謝大家一直以來對(duì)我關(guān)注和支持,同時(shí)也感謝大家的拍磚.
下次我會(huì)做下這個(gè)系列的導(dǎo)航出來,并公布源碼.歡迎繼續(xù)拍磚.
摘自紅色黑客聯(lián)盟(www.7747.net) 原文:http://www.7747.net/kf/201103/86551.html
聯(lián)系客服