用DataList 控制元件开发的一个简单的留言本程序:
2024-07-21 02:16:41
供稿:网友
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.ado" %>
<html>
<body bgcolor="white">
<h3>datalist 控制元件<hr></h3>
<div id=message runat=server></div>
<form runat="server">
<table cellspacing=0 cellpadding=0 width="776" border=0>
<tr>
<td width="2%"><img height=20 src=http://www.163design.net/n/a/"images/up.gif" width=16></td>
<td width="98%" bgcolor=#333333 align="center"><font face=宋体 color=#ffffcc><b>留言本</b></font></td>
</tr>
</table>
<asp:datalist id="datalist1" runat="server"
border="1" bordercolor="black"
cellpadding="2" cellspacing="0"
headerstyle-backcolor="#888888"
itemstyle-backcolor="#eeeeee"
selecteditemstyle-backcolor="#ffffff"
headertemplate-colspan="3"
onitemcommand="datalist_itemcommand"
width="776">
<!--编号模版-->
<template name="headertemplate" >
<asp:linkbutton id="tosay" runat="server" text="我要发言" forecolor="#ffffff"/>
<a href="temp.aspx"><font color="#ffffff">刷新界面</font></a>
<asp:linkbutton id="mange" runat="server" text="管理留言" forecolor="#ffffff"/>
</template>
<!--内容模版-->
<template name="itemtemplate">
<%# container.dataitem("book_id")%>
<%# container.dataitem("content")%>
<asp:linkbutton id="detail" runat="server" text="查看详细内容" forecolor="#333333"/>
</template>
<!--查看留言模版-->
<template name="selecteditemtemplate">
编号: <%# container.dataitem("book_id")%><br>
发言人: <%# container.dataitem("username")%><br>
时间: <%# container.dataitem("post_time")%><br>
内容: <%# container.dataitem("content")%><br>
<div align="right"><asp:linkbutton id="title" runat="server" text="关闭" forecolor="#333333"/></div>
</template>
</asp:datalist>
<table id="foot" cellspacing=0 cellpadding=0 width="776" border=0 runat="server" visible="false">
<tr>
<td bgcolor=#000000>
<table cellspacing=1 cellpadding=3 width="100%" border=0>
<tr bgcolor=#ffffff>
<td>大名:<input type="text" id="username" size=20 runat="server" /><br>
留言:<input type="text" id="content" size=20 runat="server" />
<asp:linkbutton id="save" runat="server" text="保存" onclick="linkbutton1_click" autopostback/><br>
<asp:linkbutton id="close" runat="server" text="关闭" onclick="linkbutton2_click"/></td>
</tr>
</table>
</td>
</tr>
</table>
<table cellspacing=0 cellpadding=0 width="776" border=0>
<tr>
<td width="98%" bgcolor=#333333 align="center"><font face=宋体 color=#ffffcc><b> 留言本</b></font></td>
<td width="2%"><img height=20 src=http://www.163design.net/n/a/"images/dn.gif" width=16></td>
</tr>
</table>
</form>
<hr>
<asp:label id="label1" runat="server" />
</body>
</html>
<script language="vb" runat="server">
sub page_load(sender as object, e as eventargs)
datalist1.datasource = createdataview( "data.mdb", "guestbook" )
datalist1.databind()
end sub
function createdataview( db as string, table as string ) as dataview
dim provider, connstr, sql as string
provider = "microsoft.jet.oledb.4.0;"
connstr = "provider=" & provider & _
"data source=" & server.mappath( db )
dim cmd as adodatasetcommand
cmd = new adodatasetcommand( "select * from " & table &" order by post_time desc", connstr )
dim ds as dataset = new dataset()
cmd.filldataset(ds, table)
createdataview = new dataview(ds.tables(table))
end function
sub datalist_itemcommand(sender as object, e as datalistcommandeventargs)
select case e.commandsource.text
case "我要发言"
foot.visible = true
case "管理留言"
case "查看详细内容"
datalist1.selectedindex = e.item.itemindex '查看留言模版被选中
case "关闭"
datalist1.selectedindex = -1
end select
datalist1.datasource = createdataview( "data.mdb", "guestbook" )
datalist1.databind()
end sub
sub linkbutton2_click(sender as object, e as eventargs)
foot.visible = false
end sub
sub linkbutton1_click(sender as object, e as eventargs)
dim connstr, sql as string
sql = "insert into guestbook (username,post_time,content) values ('"& username.value &"','"& datetime.now &"','"& content.value &"')"
connstr = "provider=microsoft.jet.oledb.4.0;" & _
"data source=" & server.mappath( "data.mdb" )
dim cmd as adocommand = new adocommand( sql, connstr )
cmd.activeconnection.open()
cmd.execute()
message.innerhtml = "<b>留言成功!</b>"
foot.visible = false
page.navigate("temp.aspx")
end sub
</script>
研究了好长时间,可最后那句page.navigate("temp.aspx")还是达不到及时刷新的效果:(,看精华区有篇c#编的是在page_load事件里面接收request.form对象后insert立即更新dateset,不知道我这个怎么弄好...