Use DataList control to develop a simple message this program

Source: Internet
Author: User
Tags insert
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.ADO"%>
<Html>
<body bgcolor= "White" >
<div Id=message runat=server></div>
<form runat= "Server" >
<table cellspacing=0 cellpadding=0 width= "776" border=0>
<tr>
&LT;TD width= "2%" ></td>
&LT;TD width= "98%" bgcolor= #333333 align= "center" ><font face= song body color= #ffffcc ><b> message book </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" >

<!--numbering template-->
<template name= "HeaderTemplate" >
<asp:linkbutton id= "Tosay" runat= "Server" text= "I want to Speak" forecolor= "#ffffff"/>
<a href= "temp.aspx" ><font color= "#ffffff" > Refresh interface </font></a>
<asp:linkbutton id= "mange" runat= "server" text= "Management Message" forecolor= "#ffffff"/>
</template>


<!--content Template-->
<template name= "ItemTemplate" >
<%# Container.DataItem ("book_id")%>
<%# Container.DataItem ("content")%>
<asp:linkbutton id= "Detail" runat= "Server" text= "View Details" forecolor= "#333333"/>
</template>

<!--view message template-->
<template name= "SelectedItemTemplate" >
Item No: <%# Container.DataItem ("book_id")%><br>
Speaker: <%# Container.DataItem ("username")%><br>
Time: <%# Container.DataItem ("Post_time")%><br>
Content: <%# Container.DataItem ("content")%><br>
<div align= "right" ><asp:linkbutton id= "Title" runat= "Server" text= "Close" forecolor= "#333333"/></div >
</template>

</asp:DataList>
<table id= "Foot" cellspacing=0 cellpadding=0 width= "776" border=0 runat= "Server" visible= "false" >
<tr>
&LT;TD bgcolor= #000000 >
<table cellspacing=1 cellpadding=3 width= "100%" border=0>
<tr bgcolor= #ffffff >
<td> Name: <input type= "Text" id= "Username" size=20 runat= "Server"/><br>
Message: <input type= "Text" id= "Content" size=20 runat= "Server"/>
<asp:linkbutton id= "Save" runat= "Server" text= "saved" autopostback/><br>
<asp:linkbutton id= "Close" runat= "Server" text= "Off"/></td>
</tr>
</table>
</td>
</tr>
</table>

<table cellspacing=0 cellpadding=0 width= "776" border=0>
<tr>
&LT;TD width= "98%" bgcolor= #333333 align= "center" ><font face= song body color= #ffffcc ><b> message book </b></ Font></td>
&LT;TD width= "2%" ></td>
</tr>
</table>




</Form>

<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 "I want to speak"
Foot. Visible = True

Case "Management Message"

Case "View Details"
Datalist1.selectedindex = E.item.itemindex ' View message template is selected
Case "Off"
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 &A MP; "', '" & 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> message success!" </B> "
Foot. Visible = False
Page.navigate ("temp.aspx")
End Sub
</script>

Study for a long time, but the last sentence page.navigate ("temp.aspx") or to achieve the effect of timely refresh: (, see the Essence of a C # is compiled in the Page_ The Load event receives the Request.Form object after the insert is immediately updated dateset, do not know how to fix this ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.