公用模块编写
1 数据库连接编写
为了方便应用程序移植和版本控制,可以在应用程序的配置文件(也就是Web.Config)中设置数据库连接信息。添加如下语句:
<configuration>
<appSettings>
<add key="strConnection" value="server=ms-e6f404039fa9; uid=sa; pwd=sa;database=ST_ FlowerPrearrange; Connection Reset=FALSE"/>
<add key="Manager" value="admin"/>
</appSettings>
…
</configuration>
%
注意:应当使uid和pwd与机器上SQL Server的登录名和密码相对应。
2 界面主体框架
为了提高主要代码的重复使用性,提高主要代码性能,把用户界面分成两个部分,分别为头部页面ST_Top.aspx和主页面ST_Main.aspx。而主页面ST_Main.aspx包含了ST_Left.aspx页面,此页面主要调用系统的验证用户和注册功能。下面对这几种页面逐一解释。
1.头部页面
头部页面如图4-13所示。
图4-13 头部页面
(1)ST_Top.aspx界面的HTML主要代码如下:
<TABLE id="Table1" height="70" cellSpacing="0" cellPadding="0" width="100%" border="0"
bgColor="#f3e2a6">
<TR>
<TD><FONT>
<TABLE id="Table2" height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD style="WIDTH: 276px"></TD>
<TD align="right" width="460">
<TABLE id="Table3" style="WIDTH: 462px; HEIGHT: 50px" height="50" cellSpacing="0" cellPadding ="0"
width="662" border="0"><TR>
<TD align="center" height="50"></TD>
<TD align="center" height="50">
<asp:HyperLink id="hl_Main" runat="server" NavigateUrl="ST_Main.aspx" Target= "ST_main"> <img src="img/Home.gif" border="0"><br>首页 </asp:HyperLink></TD>
<TD align="center" height="50">
<asp:HyperLink id="hl_Cart" runat="server" NavigateUrl="ST_AddToCart.aspx" Target= "ST_main"><img src="img/basket.gif" border="0"><br>购物车 </asp:HyperLink></TD>
<TD align="center" height="50">
<asp:HyperLink id="hl_Temp" runat="server" NavigateUrl="ST_AddToTempStore.aspx" Target="ST_main"><img src="img/predoc.gif" border="0"><br>收藏</asp:HyperLink><a href="ST_ AddToTempStore.aspx" target="ST_main"></a></TD>
<TD align="center" height="50">
<asp:HyperLink id="hl_Accounts" runat="server" Target="ST_main"></asp: HyperLink> <a href="ManageUser.aspx" target="ST_main"></a></TD>
<TD align="center" height="50">
<asp:HyperLink id="hl_Flower" runat="server" Target="ST_main"></asp:HyperLink>
</TD>
<TD align="center" height="50">
<asp:HyperLink id="hl_Orders" runat="server" Target="ST_main"></asp:HyperLink> </TD>
<TD align="center" height="50">
<asp:HyperLink id="hl_UserOrder" runat="server" NavigateUrl="ST_ShowUserOrders
.aspx" Target="ST_main"><img src="img/UserOrder.gif" border="0"><br>我的定单</asp:HyperLink></TD>
.aspx" Target="ST_main"><img src="img/UserOrder.gif" border="0"><br>我的定单</asp:HyperLink></TD>
<TD align="center" height="50">
<asp:HyperLink id="hl_Search" runat="server" NavigateUrl="ST_Search.aspx" Target = "ST_main" Width="34px"><img src="img/searchico.gif" border="0"><br>查找</asp:HyperLink>
</TD>
<TD align="center" height="50">
</TD>
<TD align="center" height="50">
<asp:HyperLink id="hl_Exit" runat="server" NavigateUrl="ST_Logout.aspx" Target = "ST_main"><img src="Img/aqquit.gif" border="0"><br>退出</asp:HyperLink>
</TD></TR>
</TABLE>
</TD></TR>
</TABLE></FONT>
</TD>
</TR>
</TABLE>
(2)后台事件处理页面ST_Top.aspx.cs主要代码及其解释如下:
if(!object.Equals(Session["UserName"],null))
{
if (ST_GROUP.FlowerPrearrange.ST_User.ST_IsSupervisor(Session["UserName"].ToString()))
{
//链接到鲜花管理界面
hl_Flower.NavigateUrl = "ST_FlowerManage.aspx";
hl_Flower.Text = "<img src=img/forum.gif border=0><br>鲜花管理 ";
hl_Flower.Visible =true;
//链接到用户管理界面
hl_Accounts.NavigateUrl = "ST_ManageUser.aspx";
hl_Accounts.Text = "<img src=img/mans.gif border=0><br>用户管理";
hl_Accounts.Visible =true;
hl_Orders.NavigateUrl = "ST_ManageOrder.aspx";
hl_Orders.Text = "<img src=img/UserOrder.gif border=0><br>订单";
hl_Orders.Visible =true;
this.hl_Cart.Visible = false;
this.hl_UserOrder.Visible = false;
this.hl_Temp.Visible = false;
}
else
{
hl_Flower.Visible =false;
hl_Accounts.Visible =false;
hl_Orders.Visible =false;
}
}
2.主页面
主页面ST_Main.aspx如图4-14所示。
图4-14 主页面
(1)此页面将在登录系统这一节介绍。首先来看一下ST_Main.aspx界面的HTML主要代码。其代码如下:
<TABLE id="Table1" height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD vAlign="top" height="700">
<TABLE id="Table2" height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD width="265">
<iframe id="iFrame1" style="BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-BOTTOM-STYLE: none; BORDER-Left-STYLE: none" tabIndex="0" marginWidth="0" marginHeight ="0" src="ST_Left.aspx" frameBorder="0" width="240" height="100%">
</iframe>
</TD>
<TD vAlign="top" height="255" style="width: 489px">
<TABLE id="Table3" height="100%" cellSpacing="0" cellPadding="0" width="95%" border ="0">
<TR>
<TD vAlign="top">
<TABLE id="Table4" cellSpacing="0" cellPadding="0" width="100%" border="0">
<TD>
<TABLE id="Table6" cellSpacing="0" cellPadding="0" width="100%" border ="0">
<TR>
<TD noWrap width="1%"><IMG alt="" src="img/point.gif"><STRONG> <FONT color="#cd0104">最新鲜花</FONT></STRONG></TD>
<TD background="img/titlemd2.gif"></TD></TR>
</TABLE>
<TR><TD>
<!—DataGrid控件-->
<asp:datagrid id="dg_NewFlower" runat="server" BorderStyle="None" BorderWidth ="0px" ShowHeader="False" AutoGenerateColumns="False" Width="100%">
<!—列-->
<Columns>
<!—链接列-->
<asp:HyperLinkColumn Text="<img src=img/dir_pointer_g.gif border= 0>">
<HeaderStyle Width="1%">
</HeaderStyle>
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
</asp:HyperLinkColumn>
<asp:HyperLinkColumn DataNavigateUrlField="ST_Id" DataNavigateUrl- FormatString ="ST_ShowFlowerByID.aspx?ST_flowerid={0}" DataTextField="ST_Name"></asp:HyperLinkColumn>
</Columns>
</asp:datagrid></TD></TR>
<TD>
<TABLE id="Table7" cellSpacing="0" cellPadding="0" width="100%" border= "0">
<TR>
<TD noWrap width="1%"><IMG alt="" src="img/point.gif"><font color ="#cd0104"><b> </b></font></TD>
<TD background="img/titlemd2.gif">评论</TD></TR>
</TABLE>
<TR>
<TD>
<!—DataGrid控件-->
<asp:datagrid id="dg_CommentFlower" runat="server" BorderStyle ="None" BorderWidth="0px" ShowHeader="False" AutoGenerateColumns="False" Width="100%">
<Columns>
<!—链接列-->
<asp:HyperLinkColumn Text="<img src=img/dir_pointer_g.gif border =0>">
<HeaderStyle Width="1%">
</HeaderStyle>
<ItemStyle HorizontalAlign="Center">
</ItemStyle>
</asp:HyperLinkColumn>
<asp:HyperLinkColumn DataNavigateUrlField="ST_Flowerid" Data- NavigateUrlFormatString="ST_ShowFlowerByID.aspx?ST_flowerid={0}" DataTextField="ST_CommentCaption"> </asp:HyperLinkColumn>
</Columns>
</asp:datagrid></TD>
</TR>
</TABLE>
</TD>
<TABLE id="Table5" cellSpacing="0" cellPadding="0" width="100%" border= "0">
<TR><TD>
<TABLE id="Table8" cellSpacing="0" cellPadding="0" width="100%" border ="0">
<TR>
<TD noWrap width="1%"><IMG alt="" src="img/point.gif"><font color ="#cd0104"><b> 推荐鲜花</b></font></TD>
<TD background="img/titlemd2.gif"></TD> </TR>
</TABLE>
<TR>
<TD vAlign="top"><asp:datalist id="dl_HitFlower" runat="server" Width= "100%">
<ItemTemplate>
<FONT>
<TABLE id="Table11" cellSpacing="0" cellPadding="0" width="100%" border ="0">
<TR><TD>
<TABLE id="Table12" cellSpacing="0" cellPadding="0" width="100%" border ="0">
<TR>
<TD vAlign=middle align=center ><a href='ST_ShowFlowerByID.aspx? ST_Flowerid=<%# DataBinder.Eval(Container.DataItem,"ST_id")%>'><%# DataBinder.Eval (Container.DataItem, "ST_Image")%></a></TD>
<td width="10"></td>
<TD>
<TABLE id="Table13" cellSpacing="0" cellPadding="0" width="100%" border ="0">
<TR>
<TD><a href='ST_ShowFlowerByID.aspx?ST_flowerid=<%# DataBinder.Eval (Container.DataItem,"ST_id")%>'>
<b>
<%# DataBinder.Eval(Container.DataItem,"ST_name")%>
</b></a></TD></TR>
<TD><%# DataBinder.Eval(Container.DataItem,"ST_descr")%></TD></TR>
</TABLE></TD></TR>
</TABLE>
</td></tr>
<TR>
<TD background="img/bg11(1).gif" height="1"></TD></TR>
</TABLE></FONT>
</ItemTemplate>
<AlternatingItemTemplate>
<FONT></FONT>
</AlternatingItemTemplate>
</asp:datalist></TD></TR>
<TR>
<TD vAlign="top" style="height: 18px"></TD>
</TR></TABLE>
</TD></TR></TABLE>
</TD></TR></TABLE>
<TR>
<TD height="20">
<TABLE id="Table9" height="100%" cellSpacing="0" cellPadding="0" width="100%" bgColor ="#6666ff"
border="0">
<TR>
<TD align="center" height="15"></TD></TR>
<TR>
<TD align="center"><b><FONT color="#ffffff">版权所有</FONT></b></TD>
</TR>
<TR>
<TD align="center" height="30"><b><FONT color="#ffffff"></FONT></b></TD>
</TR>
</TABLE>
</TD></TR>
<TR>
<TD>
</TD></TR>
</TABLE>
(2)后台页面ST_Main.aspx.cs。当页面加载时会运行Page_Load中的代码,这里调用了一个自己定义的方法ST_BindData()。该方法主要负责从数据库中读取数据,并绑定到控件dg_CommentFlower,以显示数据。主要代码及其解释如下:
protected void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
ST_BindData();
}
}
private void ST_BindData()
{
DataView ST_myDv;
DataView ST_myDv1;
System.Data.SqlClient.SqlDataReader ST_myDr;
//调用ST_myFlower类的ST_GetNewFlower()方法,获取最新鲜花的信息
ST_myDv=ST_myFlower.ST_GetNewFlower();
//获取数据源DataView的对象
dg_NewFlower.DataSource=ST_myDv;
//将数据绑定到数据集
dg_NewFlower.DataBind();
//调用ST_myFlower类的ST_GetFlowerByHits()方法,获得推荐的鲜花的信息
ST_myDr= ST_myFlower.ST_GetFlowerByHits();
dl_HitFlower.DataSource =ST_myDr;
dl_HitFlower.DataBind ();
//调用ST_myComment类的ST_GetCommentTop10()方法,获得排名前10的评论
ST_myDv1=ST_myComment.ST_GetCommentTop10();
dg_CommentFlower.DataSource =ST_myDv1;
dg_CommentFlower.DataBind ();
}
(3)上面调用的方法ST_GetNewFlower() 、ST_GetFlowerByHits()、ST_GetCommentTop10()来自于ST_Flower类。其主要代码如下。
ST_GetNewFlower()方法用来获取最新10种鲜花的信息。
public DataView ST_GetNewFlower()
{
String ST_strsql;
DataSet ST_myDs;
//创建获得最新10种鲜花信息的sql语句
ST_strsql="select top 10 ST_id,ST_name from ST_Flower order by ST_id desc";
ST_myDs=ST_ExecuteSql4Ds(ST_strsql);
//返回DataView对象
return ST_myDs.Tables[0].DefaultView ;
}
ST_GetFlowerByHits()方法返回一个SqlDataReader,可以通过SqlDataReader来遍历整个数据集。
public SqlDataReader ST_GetFlowerByHits()
{
string ST_strsql;
SqlDataReader ST_result;
try
{
//创建SqlConnection对象
SqlConnection ST_myCn=new SqlConnection(ST_strConn);
//创建查询推荐前10的鲜花名字的sql语句
ST_strsql="select top 10 ST_id,ST_name,ST_descr=' '+SUBSTRING (ST_description, 0,100)+'......',ST_image=case when(not ST_Cover is null) then ' <img src=ST_ReadFlowerCover.aspx? id='+cast(ST_id as varchar(10))+' Border=1 width=70 height=100>' else ' <img src=img/pic.jpg border=1 width = 70 height=100>' end from ST_Flower order by ST_hits DESC";
//创建SqlCommand对象
SqlCommand ST_myCm=new SqlCommand(ST_strsql,ST_myCn);
//打开连接
ST_myCn.Open ();
ST_result=ST_myCm.ExecuteReader(CommandBehavior.CloseConnection);
return ST_result;
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}
}
ST_GetCommentTop10()用来查询评论排名前10的评论标题。
public DataView ST_GetCommentTop10()
{
string ST_strsql;
DataSet ST_myDs;
//创建查询评论排名前10的评论标题的sql语句
ST_strsql="select top 10 ST_Flowerid, ST_CommentCaption=ST_UserName+':'+SUBSTRING(ST_ Caption, 0,50) from ST_Comment ";
ST_myDs=ST_ExecuteSql4Ds(ST_strsql);
return ST_myDs.Tables[0].DefaultView ;
}
3 登录系统和退出系统
1.登录系统
整个登录系统的关键在于ST_Left.aspx页面,此页面用了两个panel控件,用了一个三行两列的表。在表里用了两个文本框,一个用于输入用户名,一个用于输入密码。还用了一个按钮和一个超链接。超链接用来链接注册页面,请读者参考配书光盘里的ST_Register.aspx页面。ST_Left.aspx页面还用两个DataGrid控件,分别用于显示鲜花类型和鲜花销售排名(前10名),希望读者能够自己参考ST_Left.aspx文件。本页面获得的信息通过ST_Left.aspx.cs调用的系统模块进行验证。用户名和密码都正确,就读取用户信息,登录主页面。如果用户密码错误则弹出错误警告。
(1)用户登录页面ST_Left.aspx设计效果如图4-15 所示。
|
|
图4-15 用户登录页面
用户登录页面的HTML主要代码如下:
<TR>
<TD style="height: 144px"><asp:panel id="Panel1" style="POSITION: relative" runat="server"Height= "128px" Width="229px">
<TABLE id="Table3" cellSpacing="0" cellPadding="0" width="204" border="0">
<TR>
<TD colSpan="3">
<asp:label id="lblInfo" runat="server" Width="163px" ForeColor="Blue" Font-Size="X-Small"> </asp:label></TD>
</TR><TR>
<TD style="height: 24px">
<asp:Label id="Label1" runat="server" Width="70px" Font-Size="X-Small">账户:</asp:Label> </TD>
<TD style="height: 24px">
<asp:textbox id="tb_Account" runat="server" Width="135px"></asp:textbox></TD>
<TD style="height: 24px"></TD>
</TR>
<TR>
<TD>
<asp:Label id="Label2" runat="server" Width="71px" Font-Size="X-Small">密码:</asp:Label> </TD>
<TD>
<asp:textbox id="tb_Password" runat="server" Width="133px" TextMode="Password"> </asp:textbox> </TD>
<TD></TD>
</TR>
<TR>
<TD align="center" colSpan="3"> </TD></TR>
<TR>
<TD style="height: 18px">
<asp:HyperLink id="HyperLink1" runat="server" Font-Size="X-Small" NavigateUrl ="ST_ Register.aspx" Target="ST_main">注册</asp:HyperLink></TD>
<TD style="height: 18px">
<asp:Button id="ib_Login" runat="server" Text="登录" οnclick="ib_Login_Click"></asp:Button></TD>
<TD style="height: 18px"></TD></TR>
</TABLE>
</asp:panel></TD></TR>
<tr>
<td height="10"><asp:panel id="Panel2" style="POSITION: relative" runat="server"Height="148" Width="229px" Visible="False">
<TABLE id="Table4"cellSpacing="0" cellPadding="0" width="175" border="0">
<TR>
<TD style="WIDTH: 194px" align="center">
<asp:label id="lblInfo2" runat="server" Width="163px" ForeColor="Blue"></asp:label></TD> </TR>
<TR>
<TD style="WIDTH: 194px">
<asp:Label id="Label3" runat="server" Width="198px">最近访问时间:</asp:Label></TD>
</TR>
<TR>
<TD style="WIDTH: 194px" align="Left">
<asp:Label id="lblDate" runat="server" Width="147px" Height="13px" ForeColor="#FF8080"> </asp:Label></TD>
</TR>
<TR>
<TD style="WIDTH: 194px; HEIGHT: 19px">
<asp:Label id="Label4" runat="server" Width="117px">访问次数:</asp:Label>
<asp:Label id="lblTimes" runat="server" Width="6px" ForeColor="#FF8080"></asp:Label></TD>
</TR>
<TR>
<TD style="WIDTH: 194px; HEIGHT: 19px"></TD>
</TR>
<TR>
<TD style="WIDTH: 194px">
<asp:HyperLink id="HyperLink3" runat="server" Width="195px" NavigateUrl="ST_UpdateUser
.aspx" Target="ST_main">>>修改个人信息</asp:HyperLink></TD>
.aspx" Target="ST_main">>>修改个人信息</asp:HyperLink></TD>
</TR>
<TR>
<TD style="WIDTH: 194px">
<asp:HyperLink id="HyperLink4" runat="server" Width="195px" NavigateUrl="ST_ ChangePassword.aspx" Target="ST_main">>>修改密码</asp:HyperLink></TD>
</TR>
</TABLE>
</asp:panel></td>
</tr>
(2)ST_Left.aspx.cs页面是用户验证模块的核心,包含了用户验证的主要功能,解释如下:
protected void ib_Login_Click(object sender, System.EventArgs e)
{
//创建ST_User类的对象
ST_GROUP.FlowerPrearrange.ST_User user = new ST_GROUP.FlowerPrearrange.ST_User();
//获取用户名
ST_name = this.tb_Account.Text.Trim();
user.ST_Name = ST_name;
//获取密码
user.ST_Password = this.tb_Password.Text.Trim();
//判断用户是否存在
if(user.ST_Login())
{
//显示欢迎信息
this.lblInfo2.Text = "欢迎您,"+user.ST_Name;
try
{
//获得用户信息
user.ST_GetUserInfo();
ST_id = user.ST_ID;
Session["UserName"] = ST_name;
Session["UserID"] = ST_id;
if(Object.Equals(Request.Cookies["ShopFlowerLogin"],null))
{
ST_CreateCookie();
//显示面板信息
ST_ShowInfo();
}
else
{
ST_ShowInfo();
ST_UpdateCookie();
}
Page.RegisterStartupScript("refresh","<script language=javascript>parent.top.history.go(0); </script>");
}
catch(Exception ex)
{
this.lblInfo.Text = "获取用户信息失败!" + ex.Message;
}
}
else
{
this.lblInfo.Text = "登录失败!";
}
}
private void ST_ShowInfo()
{
this.Panel1.Visible = false;
this.Panel2.Visible = true;
string time,times;
//获取客户端时间
ST_GetVisitInfo(out time,out times);
//显示最新访问日期
this.lblDate.Text = time.ToString();
//显示最新访问时间
this.lblTimes.Text = times.ToString();
}
private void ST_GetVisitInfo(out string lastTime,out string times)
{
HttpCookie cookie = Request.Cookies["ShopFlowerLogin"];
int type = int.Parse(cookie.Values["Value"].ToString());
string FlowerShopCookie = ST_Functions.ST_DecryptCookie(cookie.Values["ST_GROUP.FlowerPrearrange"]
.ToString(),type);
.ToString(),type);
string [] ST_myCookie = new String[10];
ST_myCookie = FlowerShopCookie.Split('#');
//从客户端获取日期
lastTime = ST_myCookie[2];
//从客户端获取时间
times = ST_myCookie[3];
}
(3)前面调用了ST_User类的ST_Login()和ST_GetUserInfo()方法,其中ST_Login()方法用来验证用户输入的用户名和密码是否正确,即从数据库中查询有没有与用户输入的数据相同的数据,如果有则返回True,说明用户已存在。
public bool ST_Login()
{
//根据用户名、密码创建查询用户ID的sql语句
ST_strSQL = "Select ST_Id from ST_UserInfo Where ST_Name='"
+ this.ST_Name + "'"
+ " And ST_Password='" + ST_Functions.ST_Encrypt(this.ST_Password,1) +"'";
try
{
ST_ExecuteSql4Value(ST_strSQL);
return true;
}
catch
{
return false;
}
}
ST_GetUserInfo()用来根据用户名创建查询用户信息。
public bool ST_GetUserInfo()
{
//根据用户名创建查询用户信息的sql语句
ST_strSQL = "Select * from ST_UserInfo Where ST_Name='"
+ this.ST_Name + "'";
SqlConnection ST_myCn = new SqlConnection(ST_strConn);
//打开连接
ST_myCn.Open();
SqlCommand ST_myCmd = new SqlCommand(ST_strSQL,ST_myCn);
try
{
ST_myCmd.ExecuteNonQuery();
SqlDataReader reader = ST_myCmd.ExecuteReader();
if(reader.Read())
{
//获取用户ID
this.ST_ID = reader.GetInt32(0);
//获取用户邮箱
this.ST_Mail = reader.GetString(3);
return true;
}
else
{
return false;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
ST_myCmd.Dispose();
ST_myCn.Close();
}
}
上面调用了ST_Functions类的ST_Encrypt()方法,用来将用户的密码进行MD5加密。这部分留给读者自己研究,代码在ST_Functions文件里。
2.注销登录
注销用户登录是由ST_Logout.aspx和ST_Logout.aspx.cs来实现的,由于界面没有任何元素,所以这里只列出ST_Logout.aspx.cs的主要代码以及解释。
protected void Page_Load(object sender, System.EventArgs e)
{
//设置用户名为空
Session["UserName"] = null;
//设置用户ID为空
Session["UserID"] = null;
//定向到ST_Main.aspx页面
Response.Redirect("ST_Main.aspx");
}
联系站长:
lifuyun023@163.com