dt As DataSet = New DataSet
mycmd.Fill(dt, Session("startpage") - 1, Session("pagesize"), "guestbook")
Repeater1.DataSource = dt.Tables("guestbook").DefaultView
Repeater1.DataBind()
'显示留言版状态信息
pagesinfor.Text = "当前第" & CStr(Session("startpage")) & "页/ 共" & Session("pagecount") & "页"
'更新下拉列表
pageslist.SelectedIndex = Session("startpage") - 1
End Sub
'统计页码数
Sub getpagecount()
Dim mycon As OleDb.OleDbConnection = New OleDb.OleDbConnection(viewstate("constr"))
mycon.Open()
Dim mycmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("Select count(*) from GuestMessage", mycon)
Session("pagecount") = mycmd.ExecuteScalar \ Session("pagesize") + 1
mycon.Close()
Dim i As Int16
'清楚下拉列表中的项
'加载新的项
pageslist.Items.Clear()
For i = 1 To Session("pagecount")
pageslist.Items.Add(i.ToString)
Next
pageslist.SelectedIndex = Session("startpage") - 1
End Sub
'数据绑定过程结束
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
'动态加载头像图片
Image1.ImageUrl = DropDownList1.SelectedValue
End Sub
'Button2的Click过程就是增加留言过程
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim mycon As OleDb.OleDbConnection = New OleDb.OleDbConnection(viewstate("constr"))
Dim mysql As String
Dim uname As String
Dim uemail As String
Dim usex As String
Dim uface As String
Dim msg As String
If name.Text <> "" Then
'替换掉SQL语句中的关键字符,如果不替换会引起SQL语句执行错误
uname = name.Text.Replace("'", "")
Else
'弹出对话框提示信息!
Response.Write("<script>alert(对不起!\n您的昵称忘记填写了!');</script>")
'退出该事件的执行
Exit Sub
End If
If email.Text <> "" Then
uemail = email.Text.Replace("'", "")
Else
'弹出对话框提示信息!
Response.Write("<script>alert(对不起!\n您的邮件地址忘记填写了!');</script>")
End If
usex = DropDownList1.SelectedItem.Text.Substring(0, 2)
msg = message.Text.Replace("'", """).Replace("<", "<").Replace(">", "&rt;").Replace("&", "&")
msg = msg.Replace(vbCrLf, "<br> ")
'把VB.NET中的换行转化为Html里的"<br>"标记
mysql = "INSERT INTO GuestMessage(name,sex,email,face,ip,senddate,content) VALUES("
mysql += "'" + uname + "',"
mysql += "'" + DropDownList1.SelectedItem.Text.Substring(0, 2) + "',"
mysql += "'" + uemail + "',"
mysql += "'" + DropDownList1.SelectedValue.ToString + "',"
mysql += "'" + Request.UserHostAddress.ToString() + "', "
mysql += "#" + CStr(DateTime.Now) + "#,"
mysql += "'" + msg + "')"
Try
mycon.Open()
Dim mycmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(mysql, mycon)
mycmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write("程序出错,信息描述如下:<br>" & ex.Message)
Finally
mycon.Close()
'弹出对话框提示信息!
Response.Write("<script>alert('恭喜您\n 留言成功!');</script>")
'新增记录,需要更新留言版状态信息
getpagecount()
'刷新数据
getdata()
write.Visible = False
'关闭留言版面
End Try
End Sub
'管理员登陆事件过程
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If adminpwd.Text = "12345" Then
'默认管理员密码为adminpwd
Session("admin") = "admin"
'弹出对话框提示信息!
Response.Write("<script>alert('恭喜您!\n成功登录! ');</script>")
login.Visible = False
Else
'弹出对话框提示信息!
Response.Write("<script>alert('对不起!\n密码错误,请重试! ');</script>")
Exit Sub
End If
getdata()
End Sub
' 管理员回复事件过程
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim mycon As OleDb.OleDbConnection = New OleDb.OleDbConnection(viewstate("constr"))
Dim reply_msg As String
reply_msg = replymsg.Text.Replace("'", " "").Replace("<", "<").Replace(">", "&rt;").Replace("&", "&")
reply_msg = reply_msg.Replace(vbCrLf, "<br> ")
reply_msg = " <hr width=90% bgcolor=red><br> <font color=red> 管理员于" & CStr(DateTime.Now) & "的回复:</font> <br>" + reply_msg + "<br>"
Dim mysql As String
mysql = "UPDATE GuestMessage SET content=content + '" + reply_msg + "' where id=" & viewstate("replyid")
Try
mycon.Open()
Dim mycmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(mysql, mycon)
mycmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write("程序出错,信息描述如下:<br>" & ex.Message)
Finally
mycon.Close()
'弹出对话框提示信息!
Response.Write("<script>alert('恭喜您\n 回复成功!');</script>")
getdata()
reply.Visible = False
'关闭回复面板
End Try
End Sub
' 显示留言面板
Private Sub say_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles say.Click
write.Visible = True
End Sub
' 管理员实施管理功能
Private Sub Repeater1_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
If Session("admin") <> "admin" Then
'检查权限
login.Visible = True
Exit Sub
End If
If e.CommandName = "del" Then
'如果是删除,则调用删除过程,
'传递的参数是由LinkButton控件CommandArgument值,即记录ID
delmsg(CInt(e.CommandArgument))
End If
If e.CommandName = "reply" Then
'如果是回复,显示回复面板,使用Viewstate保存记录ID
reply.Visible = True
viewstate("replyid") = CInt(e.CommandArgument)
End If
End Sub
'以下是删除留言的通用过程,参数是留言的ID编号
Sub delmsg(ByVal id As Integer)
Dim mycon As OleDb.OleDbConnection = New OleDb.OleDbConnection(viewstate("constr"))
Dim mycmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("delete from GuestMessage where id=" & id, mycon)
mycon.Open()
mycmd.ExecuteNonQuery()
mycon.Close()
Response.Write("<script>alert('恭喜您\n 删除成功!');</script>")
getdata()
viewstate.Remove("replyid")
End Sub
'删除留言过程结束!
'pageslist_SelectedIndexChanged过程即是跳转页面过程
Private Sub pageslist_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pageslist.SelectedIndexChanged
Session("startpage") = pageslist.SelectedValue
getdata()
End Sub
Private Sub prevpage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles prevpage.Click
'添加判断,预防页码越界
If Session("startpage") > 1 Then
Session("startpage") -= 1
getdata()
End If
End Sub
Private Sub nextpage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextpage.Click
'添加判断,预防页码越界
If Session("startpage") < Session("pagecount") Then
Session("startpage") += 1
getdata()
End If
End Sub
End Class
保存编译后,Repeater_Sample.aspx各功能模块运行效果如图9.17~9.19所示。
![ASP.NET:Repeater控件的详解](http://www.webshu.net/jiaocheng/UploadFiles/200606/2006621195457313.jpg)
图9.17 Repeater_Sample.aspx的留言浏览功能
![ASP.NET:Repeater控件的详解](http://www.webshu.net/jiaocheng/UploadFiles/200606/2006621195458270.jpg)
图9.18 Repeater_Sample.aspx的留言签写功能
![ASP.NET:Repeater控件的详解](http://www.webshu.net/jiaocheng/UploadFiles/200606/2006621195458907.jpg)
图9.19 Repeater_Sample.aspx的留言回复功能
9.6 本章小结
本章主要介绍了ASP.NET编程中的数据绑定控件――DataGrid控件,DataList控件和Repeater控件。
在这三个控件中,DataGrid控件是功能最为强大的控件,也是我们常用的控件,利用它的属性设置就可以轻而易举的实现数据的分页功能,在线编辑功能等等。其次是DataList控件,该控件的最大特点是可以分列显示数据,并且对数据模板设置的支持也很完善,可以控制输出用户感兴趣的数据。最后一个轻量级的数据绑定控件Repeater,它在输出数据时候不会添加任何控制标记,也因此需要我们手工编写数据控制代码,该控件适合用于数据结构简单,要求功能简单的数据环境中。
在下面一章中,我们开始学习ASP.NET技术中的文件操作技术。