asp.net|编码|设计
ASP.net文章管理系统:系统设计与编码。
相关文章:ASP.net文章管理系统:系统设计与编码(上)
14.3.4 ArticlesList.aspx的设计与编码
ArticlesList.aspx提供了两个功能:
(1)根据传入参数列出该类别的文章列表;
(2)根据传入参赛检索出符合条件的文章列表。
这两个功能的实现是有后台逻辑程序根据传入的参数类型判断,然后为用户提供不同的服务。
ArticlesList.aspx的HTML代码如下:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ArticlesList.aspx.vb" Inherits="TheArticleSYS.ArticlesList"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta name="vs_snapToGrid" content="False">
<title>ArticlesList</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<center>
<TABLE id="Table1" style="BORDER-RIGHT: #009999 1px solid; BORDER-TOP: #009999 1px solid; BORDER-LEFT: #009999 1px solid; BORDER-BOTTOM: #009999 1px solid"
cellSpacing="0" cellPadding="0" width="760" border="1">
<TR>
<TD><IMG src="http://www.webjx.com/htmldata/2006-03-13/images/bg12.jpg"></TD>
</TR>
<TR>
<TD><FONT face="宋体"></FONT></TD>
</TR>
<TR>
<TD>
<TABLE id="Table2" style="BORDER-RIGHT: #006600 1pt solid; BORDER-TOP: #006600 1pt solid; BORDER-LEFT: #006600 1pt solid; BORDER-BOTTOM: #006600 1pt solid"
cellSpacing="0" cellPadding="0" width="762" border="0">
<asp:datalist id="DataList1" runat="server" BorderColor="#009999" BorderWidth="1pt">
<AlternatingItemStyle BackColor="Azure"></AlternatingItemStyle>
<HeaderStyle Font-Size="Larger" Font-Bold="True" ForeColor="Azure" BackColor="#009999"></HeaderStyle>
<ItemTemplate>
<tr>
<TD align="left" style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; BACKGROUND-COLOR: darkgray">标题:
<a href='<%# "TheArticle.aspx?aid="&databinder.eval(container.dataitem,"id") %>' target=_blank >
<%# databinder.eval(container.dataitem,"title") %>
</a>
</TD>
</tr>
<tr>
<TD align="left" style="FONT-SIZE: 11pt; BACKGROUND-COLOR: lightgrey">
id:<%# databinder.eval(container.dataitem,"id") %>
作者:<%# databinder.eval(container.dataitem,"writer") %>
---发布日期:<%# databinder.eval(container.dataitem,"sdate") %>
---浏览次数:<%# databinder.eval(container.dataitem,"views") %>
</TD>
</tr>
<tr>
<TD align="left" style="FONT-SIZE: 11pt; BACKGROUND-COLOR: lightgrey">关键字[<%# databinder.eval(container.dataitem,"keyword") %>]</TD>
</tr>
<tr>
<TD align="left" style="TEXT-INDENT: 10pt; BACKGROUND-COLOR: seashell">
内容摘要:<br>
...<%# mid(databinder.eval(container.dataitem,"content"),50,200) %>...<br>
</TD>
</tr>
</ItemTemplate>
</asp:datalist></TABLE>
</TD>
</TR>
<TR>
<TD><FONT face="宋体"><asp:label id="Label1" runat="server"></asp:label><asp:linkbutton id="LinkButton4" runat="server">第一页</asp:linkbutton>
<asp:linkbutton id="LinkButton1" runat="server">上一页</asp:linkbutton><asp:linkbutton id="LinkButton2" runat="server">下一页</asp:linkbutton>
<asp:linkbutton id="LinkButton3" runat="server">末一页</asp:linkbutton></FONT></TD>
</TR>
</TABLE>
</center>
</form>
</body>
</HTML>
ArticlesList.aspx.vb的后台逻辑代码如下:
'---code begin---
Imports System.Web
Imports System.Data
Public Class ArticlesList
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'此处省略了设计器生成的无关代码,以节省篇幅
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not Page.IsPostBack Then
viewstate("pagenow") = 0
Dim akinds As String = Request.Params("akind")
Dim seekid As String = Request.Params("seekid")
Dim infor As String = Request.Params("infor")
Dim moreid As String = Request.Params("moreid")
If moreid = "" Then
If akinds = "" Or seekid = "" Or infor = "" Then
Response.Redirect("default.aspx")
Else
If akinds = "-1" Then
mysql = " where " + seekid + " like '%" + infor + "%'"
Else
mysql = " where akinds=" + akinds + " and " + seekid + " like '%" + infor + "%' "
End If
End If
Else
mysql = " where akinds=" + Request.Params("moreid")
End If
getdatainfor()
getdata()
End If
End Sub
'获取符合条件的记录条数
Sub getdatainfor()
mycon.Open()
Dim mycmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("select count(*) from articles " & mysql, mycon)
viewstate("pagecount") = mycmd.ExecuteScalar
mycon.Close()
If viewstate("pagecount") = 0 Then
Response.Write("<script>alert('\n对不起,没有查找到符合条件的文章\n');</script>")
Exit Sub
End If
End Sub
'获取记录信息
Sub getdata()
Dim mycmd As OleDb.OleDbDataAdapter
Dim dt As DataSet = New DataSet
mycmd = New OleDb.OleDbDataAdapter(" select * from articles" & mysql & " order by id desc ", mycon)
'每页显示6篇文章记录
mycmd.Fill(dt, viewstate("pagenow"), 6, "plist")
DataList1.DataSource = dt.Tables("plist").DefaultView
DataList1.DataBind()
Label1.Text = "共有[<font color=red> " & viewstate("pagecount") & "</font>]篇文章"
Label1.Text += ",当前为第[<font color=red>" & (viewstate("pagenow") + 1)
Label1.Text += "</font>]页/共[<font color=red>" & (viewstate("pagecount") \ 6 + 1) & "</font>]页"
mycon.Close()
End Sub
'上一页, 即:页码减1
Private Sub LinkButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
If viewstate("pagenow") > 0 Then
viewstate("pagenow") -= 1
getdatainfor()
getdata()
End If
End Sub
'下一页, 即:页码加1
Private Sub LinkButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
If viewstate("pagenow") < viewstate("pagecount") \ 6 Then
viewstate("pagenow") += 1
getdatainfor()
getdata()
End If
End Sub
'第一页,即:页码为0
Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton4.Click
If viewstate("pagenow") <> 0 Then
viewstate("pagenow") = 0
getdatainfor()
getdata()
End If
End Sub
'最后一页
Private Sub LinkButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton3.Click
If viewstate("pagenow") < viewstate("pagecount") \ 6 Then
viewstate("pagenow") = viewstate("pagecount") \ 6
getdatainfor()
getdata()
End If
End Sub
End Class
'---code end ----
编译运行后,结果如图14.12所示
图14.12ArticlesList.aspx运行结果
14.3.5 TheArticle.aspx和Tealker.ascx的设计与编码
TheArticle.aspx是显示文章具体内容的页面,根据传入的文章ID,从数据库中检索出文章,并把内容显示出来。并利用Tealker.ascx把有关该文章的评论显示到页面中,同时为浏览者提供一个发表评论的入口。
TheArticle.aspx的HTML代码如下:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="TheArticle.aspx.vb" Inherits="TheArticleSYS.TheArticle"%>
<%@ Register TagPrefix="uc1" TagName="tealker" Src="tealker.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title runat="server" id="Mytitle"></title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<center>
<TABLE id="Table1" style="BORDER-RIGHT: #739cd6 1pt solid; BORDER-TOP: #739cd6 1pt solid; BORDER-LEFT: #739cd6 1pt solid; BORDER-BOTTOM: #739cd6 1pt solid"
cellSpacing="0" cellPadding="0" width="762" background="images/bg10.gif" border="0">
<TR>
<TD></TD>
<TD align="right">
<TABLE id="Table2" style="BORDER-RIGHT: #739cd6 1pt solid; BORDER-TOP: #739cd6 1pt solid; BORDER-LEFT: #739cd6 1pt solid; WIDTH: 680px; BORDER-BOTTOM: #739cd6 1pt solid; HEIGHT: 50px"
cellSpacing="0" cellPadding="0" width="680" border="0">
<TR>
<TD style="FONT-WEIGHT: bold; FONT-SIZE: 14pt; COLOR: white; HEIGHT: 50px; BACKGROUND-COLOR: #739cd6"
align="center"><asp:label id="Label1" runat="server">Label</asp:label></TD>
</TR>
<TR>
<TD style="BORDER-RIGHT: #739cd6 1pt solid; BORDER-TOP: #739cd6 1pt solid; FONT-SIZE: 10pt; BORDER-LEFT: #739cd6 1pt solid; BORDER-BOTTOM: #739cd6 1pt solid; HEIGHT: 30px; BACKGROUND-COLOR: azure"
align="center"><asp:label id="Label2" runat="server">Label</asp:label></TD>
</TR>
<TR>
<TD style="BORDER-RIGHT: #739cd6 1pt solid; BORDER-TOP: #739cd6 1pt solid; FONT-SIZE: 10pt; BORDER-LEFT: #739cd6 1pt solid; BORDER-BOTTOM: #739cd6 1pt solid; HEIGHT: 30px; BACKGROUND-COLOR: azure"
align="center"><asp:label id="Label4" runat="server">Label</asp:label></TD>
</TR>
<TR>
<TD style="FONT-SIZE: 11pt; LINE-HEIGHT: 20pt"><asp:label id="Label3" runat="server">Label</asp:label></TD>
</TR>
<TR>
<TD style="BORDER-BOTTOM: #009999 thin solid"><br>
</TD>
</TR>
<TR>
<TD><br>
</TD>
</TR>
<TR>
<TD align="center"><uc1:tealker id=Tealker1 runat="server" aid='<%# Request.Params("aid")%>'></uc1:tealker></TD>
</TR>
</TABLE>
<P></P>
</TD>
</TR>
</TABLE>
</center>
</form>
</body>
</HTML>
TheArticle.aspx.vb的逻辑代码如下:
'---code begin---
Public Class TheArticle
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'此处省略了设计器生成的无关代码,以节省篇幅
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Request.Params("aid") <> "" Then
mysql = "select * from articles where id=" + Request.Params("aid")
mycon.Open()
Dim mycmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(mysql, mycon)
Dim myreader As OleDb.OleDbDataReader
myreader = mycmd.ExecuteReader
Do While myreader.Read
'显示文章标题
Label1.Text = myreader("title")
'显示文章属性
Label2.Text = "-----作者:" & myreader("writer") & " 文章出自:" & myreader("fw") & " 发布时间:" & myreader("sdate")
Label2.Text += " 查看次数:" & myreader("views")
'显示关键字
Label4.Text = "关键字 [" & myreader("keyword") & "]"
'显示文章内容
Label3.Text = myreader("content")
'动态网页标题
Mytitle.InnerText = myreader("title") & "-----叮咚文章管理系统"
Loop
myreader.Close()
updateviews(Request.Params("aid"))
End If
End Sub
'更新文章浏览次数
Sub updateviews(ByVal aid As String)
mysql = "update articles set views=views+1 where id=" + aid
Dim mycmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(mysql, mycon)
mycmd.ExecuteNonQuery()
mycon.Close()
End Sub
End Class
'--code end--
Tealker.ascx的HTML代码如下:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="Tealker.ascx.vb" Inherits="TheArticleSYS.tealker" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<P>
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="679" border="0" style="BORDER-RIGHT: #009999 1px solid; BORDER-TOP: #009999 1px solid; BORDER-LEFT: #009999 1px solid; BORDER-BOTTOM: #009999 1px solid; HEIGHT: 91px">
<TR>
<TD align="center" colspan="3" style="FONT-WEIGHT: bold; FONT-SIZE: 14pt; COLOR: white; BACKGROUND-COLOR: #009999">该文章的评论
</TD>
</TR>
<asp:repeater id="Repeater1" runat="server">
<ItemTemplate>
<TR style="BACKGROUND-COLOR: #f0ffff">
<TD style="BORDER-RIGHT: #009900 1px solid; BORDER-TOP: #009900 1px solid; FONT-SIZE: 10pt; BORDER-LEFT: #009900 1px solid; WIDTH: 150px; BORDER-BOTTOM: #009900 1px solid">
评论人:<%# databinder.eval(container.dataitem,"talker") %>
</TD>
<TD style="BORDER-RIGHT: #009900 1px solid; BORDER-TOP: #009900 1px solid; FONT-SIZE: 10pt; BORDER-LEFT: #009900 1px solid; WIDTH: 200px; BORDER-BOTTOM: #009900 1px solid">
评论日期<%# databinder.eval(container.dataitem,"tdate") %>
</TD>
<TD style="BORDER-RIGHT: #009900 1px solid; BORDER-TOP: #009900 1px solid; FONT-SIZE: 10pt; BORDER-LEFT: #009900 1px solid; BORDER-BOTTOM: #009900 1px solid">
电邮<%# databinder.eval(container.dataitem,"email") %>
</TD>
</TR>
<TR>
<TD colspan="3" style="BORDER-RIGHT: #009999 1pt solid; BORDER-TOP: #009999 1pt solid; FONT-SIZE: 10pt; BORDER-LEFT: #009999 1pt solid; BORDER-BOTTOM: #009999 1pt solid">
<%# databinder.eval(container.dataitem,"content") %>
</TD>
</TR>
</ItemTemplate>
<SeparatorTemplate>
<TR>
<TD style="BORDER-BOTTOM: lightsalmon 1px solid">
</TD>
</TR>
</SeparatorTemplate>
</asp:repeater>
<TR>
<TD colspan="3">
</TD>
</TR>
</TABLE>
</P>
<TABLE id="Table2" style="BORDER-RIGHT: #009999 1pt solid; BORDER-TOP: #009999 1pt solid; BORDER-LEFT: #009999 1pt solid; BORDER-BOTTOM: #009999 1pt solid; HEIGHT: 242px"
cellSpacing="0" cellPadding="0" width="679" border="0">
<TR>
<TD align="center" style="FONT-WEIGHT: bold; FONT-SIZE: 14pt; COLOR: white; BACKGROUND-COLOR: #009999">
发表我对该文的评论
</TD>
</TR>
<TR>
<TD style="FONT-SIZE: 11pt; BACKGROUND-COLOR: azure">我的昵称
<asp:textbox id="TextBox1" runat="server" BorderStyle="Groove" BorderColor="#E0E0E0"></asp:textbox>电邮
<asp:textbox id="TextBox2" runat="server" BorderStyle="Groove" BorderColor="#E0E0E0"></asp:textbox><asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Font-Size="X-Small"
ErrorMessage="昵称不能为空!"></asp:requiredfieldvalidator><asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" Font-Size="X-Small"
ErrorMessage="电邮不能为空!"></asp:requiredfieldvalidator></TD>
</TR>
<TR>
<TD style="HEIGHT: 131px">内容
<asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox3" Font-Size="X-Small"
ErrorMessage="评论内容不能为空!"></asp:requiredfieldvalidator><br>
<asp:textbox id="TextBox3" runat="server" Height="114px" Width="624px" TextMode="MultiLine"></asp:textbox></TD>
</TR>
<TR>
<TD><asp:button id="Button1" runat="server" Text="提交"></asp:button></TD>
</TR>
</TABLE>
Tealker.ascx.vb的逻辑代码如下:
'---code begin--
Imports System.Web.Configuration
Public Class tealker
Inherits System.Web.UI.UserControl
#Region " Web 窗体设计器生成的代码 "
'此处省略了设计器生成的无关代码,以节省篇幅
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
aid = Request.Params("aid")
If aid <> "" Then
getdata()
End If
End Sub
' 显示文章评论信息
Sub getdata()
Dim constr As String = ConfigurationSettings.AppSettings("constr") & Server.MapPath(".") & ConfigurationSettings.AppSettings("dbs")
Dim mycon As OleDb.OleDbConnection = New OleDb.OleDbConnection(constr)
If Not Page.IsPostBack Then
mysql = "select * from talks where articleid=" + Request.Params("aid") + " order by tdate asc"
Dim mycmd As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(mysql, mycon)
Dim dt As DataSet = New DataSet
mycmd.Fill(dt)
Repeater1.DataSource = dt.Tables(0)
Repeater1.DataBind()
End If
End Sub
'提交文章评论内容
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim constr As String = ConfigurationSettings.AppSettings("constr") & Server.MapPath(".") & ConfigurationSettings.AppSettings("dbs")
Dim mycon As OleDb.OleDbConnection = New OleDb.OleDbConnection(constr)
If Page.IsValid Then
mysql = "insert into talks(articleid,talker,email,content) values(" + Request.Params("aid") + ",'" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text.Replace(Chr(10), "<br>") + "')"
mycon.Open()
Dim mycmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(mysql, mycon)
mycmd.ExecuteNonQuery()
Response.Write("<script>alert('恭喜您,评论成功!');</script>")
mycon.Close()
Response.Redirect("Thearticle.aspx?aid=" & Request.Params("aid"))
getdata()
End If
End Sub
End Class
'--code end--
保存编译后,运行结果如图14.13、14.14所示
图14.13 文章详细信息
图14.14 文章相关评论
14.4 本章小结
这个实例功能相对简单,结构也很清晰,目的是对前面所学知识的总结和练习。主要是练习了服务器端控件以及数据库技术方面的知识,希望读者用心体会其中的用法。
为了是读者能够关注与文章管理系统本身的结构和各个功能模块之间的联系,本实例在编写的过程中省略了一些细节问题,比如页面间传递参数,如果在实际应用中不加验证,就会有被攻击的危险。但是这完全不影响作为分析学习本系统的目的,如果读者要将本系统改造成实际应用系统的时候一样要注意。
在实例系统,我们没有考虑到对普通用户的管理和多管理员情况的管理,但是在实际引用中往往会提供用户注册,然后注册会员会享有特别的服务。这里,我们把这个功能的实现作为一个作业,留给读者来思考怎么做,然后编码实现它。
通过本章的学习,读者已经可以从整体上把握一个管理信息系统的设计。本章实例严格按照管理系统的设计步骤, 从分析到实施,如果读者能从这个方面来学习这个实例,对以后编写大型的管理系统很有帮助。