A simple message book program developed using DataList control components

Source: Internet
Author: User

<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. ADO" %>
<Html>
<Body BgColor = "White">
<H3> DataList control element <HR> </H3>
<Div id = Message runat = server> </div>
<Form runat = "server">
<Table cellspacing = 0 cellpadding = 0 width = "776" border = 0>
<Tr>
<Td width = "2%"> </td>
<Td width = "98%" bgcolor = #333333 align = "center"> <font face = 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">

<! -- Id 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 the page </font> </a>
<Asp: LinkButton id = "Mange" runat = "server" Text = "manage messages" 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 the message template -->
<Template name = "SelectedItemTemplate">
No.: <% # Container. DataItem ("book_id") %> <BR>
Spokesman: <% # 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 = "Disable" 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 & gt;
<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 = "save" OnClick = "linkbutton#click" AutoPostBack/> <br>
<Asp: LinkButton id = "close" runat = "server" Text = "close" 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> message book </B> </font> </td>
<Td width = "2%"> </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 "I want to speak"
Foot. Visible = true

Case "manage messages"

Case "View Details"
DataList1.SelectedIndex = e. Item. ItemIndex
Case "close"
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 linkbutton#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> Message successful! </B>"
Foot. Visible = false
Page. Navigate ("temp. aspx ")
End Sub
</Script>

After studying for a long time, we can see the last sentence Page. navigate ("temp. aspx ") still cannot achieve the timely refresh effect: (see section C # In the essence area, which receives the REQUEST in the PAGE_LOAD event. after the FORM object is inserted, the DATESET is updated immediately. I don't know how to fix this...


Related Article

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.