控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

用DataList控件开发一个简单的留言本程序

作者:未知 出处:不详 2010年02月01日 阅读:

程序代码如下所示:

<%@ 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.okasp.com/techinfo/"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.okasp.com/techinfo/"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,不知道我这个怎么弄好...
                                                      
                                                            【注:本文由控件中国网转载】

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统