Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / Hungry Minds - ASP.NET Bible VB.NET & C#

.pdf
Скачиваний:
128
Добавлен:
12.02.2016
Размер:
7.64 Mб
Скачать

The Threads page is used to display the list of threads within the selected topic. But to do that, it must know which topic was chosen. I pass the topic ID and the topic name as arguments on the URL line. This makes them available to be accessed by name in the Threads page by using Request.QueryString.

Picking a Thread

Once the user has picked a topic that they're interested in, the Threads page enables them to see all the threads that have been created in that topic. It also provides a link that allows the user to go to a page and create a whole new thread, as show in here:

<%@ Page Explicit="True" Language="VB"

Debug="True" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.OleDb" %>

<Script Runat="Server">

Sub Page_Load( s As Object, e As EventArgs )

If Not isPostBack Then

Dim ThreadConnection As OleDbConnection

Dim ThreadCommand As OleDbCommand

ThreadConnection = New OleDbConnection( _

"Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=" & _

"c:\inetpub\wwwroot\Discuss\DiscussDB.mdb")

ThreadCommand = New OleDbCommand( _

"Select ThreadID, Name from Threads " & _

"Where Topic=" & _

Request.QueryString("TopicID"), _

ThreadConnection )

ThreadConnection.Open()

ThreadDataList.DataSource = _

ThreadCommand.ExecuteReader()

ThreadDataList.DataBind()

ThreadConnection.Close()

End If

End Sub

Sub SelectThread( s As Object, _

e As DataListCommandEventArgs )

Dim ThreadID, ThreadName As String

ThreadDataList.SelectedIndex = e.Item.ItemIndex

ThreadName = e.CommandArgument

ThreadID = _

ThreadDataList.DataKeys.Item( _

e.Item.ItemIndex).toString() Response.Redirect("Messages.aspx?TopicID=" & _

Request.QueryString("TopicID") & _

"&TopicName=" & Request.QueryString("TopicName") & _

"&ThreadID=" & ThreadID & _

"&ThreadName=" & ThreadName)

End Sub

</Script>

<html>

<head><title>Chatty Threads</title></head> <body>

<font face="arial"> <h1><font color="DarkRed">

Chatty Discussion Forum</font></h1> <font size="5"><u>

<%=Request.QueryString("TopicName") %> </u> Threads</font>

<form Runat="Server">

<asp:DataList

id="ThreadDataList" cellpadding=5 cellspacing=0 gridlines="both" Width="100%" DataKeyField="ThreadID" OnItemCommand="SelectThread" Runat="Server">

<ItemTemplate> <font size="2"> <asp:LinkButton id="ThreadLink"

Text='<%# Container.DataItem("Name") %>'

CommandArgument='<%# Container.DataItem("Name") %>'

Runat="Server"/>

</font>

</Itemtemplate>

<FooterTemplate> <font size="2">

<a href="NewThread.aspx?TopicID= <%=Request.QueryString("TopicID") %>

&TopicName=

<%=Request.QueryString("TopicName")%>">

<i>[ Create a New Thread ]</i></a>

</font>

</FooterTemplate>

</asp:DataList>

<br/>

<a href="topics.aspx">

Return to list of <b>Topics</b></a>

</form>

</font>

</body>

</html>

This page is actually very similar to the Topics page. It retrieves a list of threads and displays them in a DataList as LinkButtons, and the user may click any of the thread names to go to the Messages page.

Using TopicID as selection criteria

There are a few differences between this page and the Topics page. This page uses the TopicID in the query to get only the threads associated with the topic the user requested:

ThreadCommand = New OleDbCommand( _

"Select ThreadID, Name from Threads " & _

"Where Topic=" & _

Request.QueryString("TopicID"), _

ThreadConnection )

I do this simply by concatenating the information from the QueryString into my Select statement. I could have, instead, used a parameter and filled in the parame ter with the TopicID. I decided not to in this case because it would have made the code more complicated and wouldn't really add any value. I do use parameters in both the Messages and the NewThread pages, and I'll explain why in a section called "Using parameters" later in this chapter.

The Thread DataList

Another difference from the Topics page is the DataList:

<asp:DataList

id="ThreadDataList"

cellpadding=5 cellspacing=0

gridlines="both" Width="100%"

DataKeyField="ThreadID"

OnItemCommand="SelectTopic"

Runat="Server">

This list isn't three columns. Instead, it's a single column, which means that each row stretches across the page, allowing for long thread names.

More importantly, there's a new tag inside the DataList after the ItemTemplate tag:

FooterTemplate.

<FooterTemplate>

<font size="2">

<a href="NewThread.aspx?TopicID=

<%=Request.QueryString("TopicID") %>

&TopicName=

<%=Request.QueryString("TopicName")%>">

<i>[ Create a New Thread ]</i></a>

</font>

</FooterTemplate>

The footer template appears as the last item in the DataList. This is the perfect spot to provide a link to a page that will allow the user to add their own thread. I pass the TopicID and TopicName to the NewThread page.

Selecting a thread

When the user clicks one of the threads in the DataList, the SelectThread subroutine is called. Again, this works much the same as the SelectTopic subroutine does in the Topics page. The only real difference is that it passes the thread ID and name to the Messages page as well as the topic ID and name as you can see here:

Response.Redirect("Messages.aspx?TopicID=" & _

Request.QueryString("TopicID") & _

"&TopicName=" & Request.QueryString("TopicName") & _

"&ThreadID=" & ThreadID & _

"&ThreadName=" & ThreadName)

Browsing Messages

The Messages page displays all the messages posted for a particular topic and thread:

<%@ Page Explicit="True" Language="VB" Debug="True" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.OleDb" %>

<Script Runat="Server">

Sub Page_Load( s As Object, e As EventArgs )

If Not isPostBack Then

BindData

End If

End Sub

Sub BindData

Dim MessageConnection As OleDbConnection

Dim MessageCommand As OleDbCommand

MessageConnection = New OleDbConnection( _

"Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=c:\inetpub\wwwroot\Discuss\DiscussDB.mdb")

MessageConnection.Open()

MessageCommand = New OleDbCommand( _

"Select MessageID, Subject, Message, " & _

"Author, Posted From Messages Where " & _

"Thread=@ThreadID Order By Posted", _

MessageConnection )

MessageCommand.Parameters.Add( _

New OleDbParameter("@ThreadID", OleDbType.Integer))

MessageCommand.Parameters("@ThreadID").Value = _

CInt(Request.QueryString("ThreadID"))

MessageRepeater.DataSource = _

MessageCommand.ExecuteReader()

MessageRepeater.DataBind()

MessageConnection.Close()

End Sub

Sub PostClick( obj As Object, e As EventArgs )

Dim MessageConnection As OleDbConnection

Dim MessageCommand As OleDbCommand

Dim InsertString as string

Dim Message As String

MessageConnection = _

New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=c:\inetpub\wwwroot\Discuss\DiscussDB.mdb")

InsertString = "Insert Into Messages (Subject, Message, " & _ "Author, Posted, Thread) VALUES (@Subject, @Message, " & _ "@Author, @Posted, @ThreadID)"

MessageCommand = _

New OleDbCommand(InsertString, MessageConnection)

MessageCommand.Parameters.Add( _

New OleDbParameter("@Subject", OleDbType.Varchar,50))

MessageCommand.Parameters.Add( _

New OleDbParameter("@Message", OleDbType.LongVarchar))

MessageCommand.Parameters.Add( _

New OleDbParameter("@Author", OleDbType.Varchar,50))

MessageCommand.Parameters.Add( _

New OleDbParameter("@Posted", OleDbType.Varchar,50))

MessageCommand.Parameters.Add( _

New OleDbParameter("@ThreadID", OleDbType.Varchar,50))

Message = MessageText.Text

Message = Replace(Message, CStr(chr(13)), "<br>")

MessageCommand.Parameters("@Subject").Value = _

SubjectText.Text

MessageCommand.Parameters("@Message").Value = Message

MessageCommand.Parameters("@Author").Value = AuthorText.Text

MessageCommand.Parameters("@Posted").Value = CStr(Now)

MessageCommand.Parameters("@ThreadID").Value = _

CInt(Request.QueryString("ThreadID"))

MessageConnection.Open()

MessageCommand.ExecuteNonQuery

MessageConnection.Close()

SubjectText.Text = ""

MessageText.Text = ""

AuthorText.Text = ""

BindData

End Sub

</Script>

<html>

<head><title>Chatty Messages</title></head> <body>

<font face="arial"> <h1><font color="DarkRed">

Chatty Discussion Forum</font></h1> <a href="threads.aspx?TopicID= <%=Request.QueryString("TopicID")%>

&TopicName=<%=Request.QueryString("TopicName")%>"> <font size="5"><u> <%=Request.QueryString("TopicName") & _

"</a></u> : <u>" & _ Request.QueryString("ThreadName") %>

</font></u>

<form Runat="Server">

<asp:Repeater

id="MessageRepeater"

Runat="Server">

<ItemTemplate>

<table width="100%" border="1" cellspacing="0" cellpadding="3" bordercolor="DarkBlue">

<tr bgcolor="DarkBlue"><td>

<font color="White" face="arial"><b>

<%# Container.DataItem("Subject") %></b></font> </td>

<td align="right" >

<font color="White" face="arial" size="1">

<%# Container.DataItem("Posted") %></font><br/> </td></tr><tr><td colspan="2">

<font face="arial" size="2">

<%# Container.DataItem("Message") %><br /> <i><%# Container.DataItem("Author") %></i></font>

</td></tr>

</table><br>

</ItemTemplate>

</asp:Repeater>

<table width="100%" border="1" cellspacing="0" cellpadding="3" bordercolor="DarkBlue"> <tr><td bgcolor="LightGrey">

<p>To post a new message to this thread, type your message below and click Post.</p>

<p><font size="2">Subject:</font><br/> <asp:textbox id="SubjectText" runat="server" columns="50"/></p>

<p><font size="2">Message:</font><br /> <asp:textbox id="MessageText" runat="server" textmode="multiline"

columns="50" rows="3"/></p>

<p><font size="2">Your Email Address:</font><br /> <asp:textbox id="AuthorText" runat="server" columns="50"/></p>

<center>

<asp:button runat="server" text=" Post " onclick="PostClick" /><br />

</center>

</td></tr></table> <br />

<a href="threads.aspx?TopicID= <%=Request.QueryString("TopicID")%> &TopicName=<%=Request.QueryString("TopicName")%>">

Return to <b><%=Request.QueryString("TopicName") %>

</b> Threads</a><br>

<a href="topics.aspx">

Return to list of <b>Topics</b></a>

</form>

</font>

</body>

</html>

This page retrieves the messages and displays them. The user is free to browse through and read them. At the bottom of the page is a form to fill out, if the user wishes to post his or her own messages.

Retrieving the messages

All the messages for all the topics and threads are stored in the same table. So to retrieve only the appropriate messages for this page, I concatenate the ThreadID sent as a QueryString to this page:

MessageCommand = New OleDbCommand( _

"Select MessageID, Subject, Message, " & _

"Author, Posted From Messages Where " & _

"Thread=" & Request.QueryString("ThreadID") & _

" Order By Posted", MessageConnection )

MessageRepeater.DataSource = _

MessageCommand.ExecuteReader()

MessageRepeater.DataBind()

MessageConnection.Close()

The results are associated with the MessageRepeater.

The header

At the top of the page, after the standard Chatty header, I placed the topic name and the thread name, separated by a colon. This tells the user exactly what messages they are looking at, as you can see here:

<body>

<font face="arial">

<h1><font color="DarkRed">

Chatty Discussion Forum</font></h1>

<a href="threads.aspx?TopicID=

<%=Request.QueryString("TopicID")%>

&TopicName=<%=Request.QueryString("TopicName")%>">

<font size="5"><u>

<%=Request.QueryString("TopicName") & _

"</a></u> : <u>" & _

Request.QueryString("ThreadName") %>

</font></u>

In addition, I made the topic name a link. When you click it, you are returned to the Threads page, showing the threads for this topic.

Displaying the messages

I use a Repeater to display the messages retrieved from the database:

<asp:Repeater

id="MessageRepeater"

Runat="Server">

<ItemTemplate>

<table width="100%" border="1" cellspacing="0"

cellpadding="3" bordercolor="DarkBlue">

<tr bgcolor="DarkBlue"><td>

<font color="White" face="arial"><b>

<%# Container.DataItem("Subject") %></b></font>

</td>

<td align="right" >

<font color="White" face="arial" size="1">

<%# Container.DataItem("Posted") %></font><br/>

</td></tr><tr><td colspan="2">

<font face="arial" size="2">

<%# Container.DataItem("Message") %><br />

<i><%# Container.DataItem("Author") %></i></font>

</td></tr>

</table><br>

</ItemTemplate>

</asp:Repeater>

This page is different from the Topics and Threads pages. I don't need to respond to user interaction as I did on those pages. And I need to have more flexibility in the layout to make the messages look presentable and easy to read. So I decide a Repeater would be a better choice than the DataList.

Each message appears inside its own table. The table is as wide as the page and has a border all the way around it. The Subject line appears white on dark blue, while the message appears below it as the standard black text on a white background. The author's e-mail address appears after the message.

Links to threads and topics

Typically, the user will come to the Messages page, read through the messages, and then click one of the links at the bottom of the page to go to either the Topics page or back to the Threads page. That's what this code provides:

<a href="threads.aspx?TopicID=

<%=Request.QueryString("TopicID")%>

&TopicName=<%=Request.QueryString("TopicName")%>">

Return to <b><%=Request.QueryString("TopicName") %>

</b> Threads</a><br>

<a href="topics.aspx">

Return to list of <b>Topics</b></a>

The new message form

However, sometimes, the reader will decide to add a message to the forum. For this, I've provided a form at the bottom of the page.

The form itself

I could have placed a link here at the bottom of the Messages page, instead, and put the form on a different page. But I decided that having the form readily available would make people more likely to post and be active in the conversations.

<table width="100%" border="1" cellspacing="0"

cellpadding="3" bordercolor="DarkBlue">

<tr><td bgcolor="LightGrey">

<p>To post a new message to this thread, type your

message below and click Post.</p>

<p><font size="2">Subject:</font><br/>

<asp:textbox id="SubjectText" runat="server"

columns="50"/></p>

<p><font size="2">Message:</font><br />

<asp:textbox id="MessageText" runat="server"

textmode="multiline"

columns="50" rows="3"/></p>

<p><font size="2">Your Email Address:</font><br />

<asp:textbox id="AuthorText" runat="server"

columns="50"/></p>

<center>

<asp:button runat="server" text=" Post "

onclick="PostClick" /><br />

</center>

</td></tr></table>

Calling PostClick

After the user has filled in the subject, message, and e-mail address textboxes, they click the Post button, which sends back the result to the server and executes the PostClick subroutine:

Sub PostClick( obj As Object, e As EventArgs )

Dim MessageConnection As OleDbConnection

Dim MessageCommand As OleDbCommand

Dim InsertString as string

Dim Message As String

The SQL Insert statement

PostClick inserts a new row in the Message table with the information entered by the user. A connection to the database is made, just as you normally would:

MessageConnection = _

Соседние файлы в папке c#