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

ASP.NET 2.0 Everyday Apps For Dummies (2006)

.pdf
Скачиваний:
57
Добавлен:
17.08.2013
Размер:
10.07 Mб
Скачать

Chapter 9: Building a Content Management System 327

Finally, the value of the item query string is tested. If it is -1, the DetailsView control is placed in Insert mode to allow the user to create a new content item.

2 DetailsView1_ItemDeleted: This method is called when an item has been successfully deleted from the DetailsView control. It simply redirects the user back to the List page, passing on the dept query string that it received.

3 DetailsView1_ItemInserted: This method is called when an item has been successfully inserted into the DetailsView control. It redirects the user back to the List page, passing on the dept query string that it received.

4 DetailsView1_ItemCommand: This method is called whenever the DetailsView control receives a command, such as Update, Edit, Insert, and Cancel. You can determine which command caused the method to be invoked by checking the CommandName property of the e argument.

The purpose of this method is to return to the List page if the user clicks Cancel while inserting a new content item. As a result, it calls Response.Redirect if e.Command is Cancel and if the

DetailsView control is in Insert mode. Otherwise, this method doesn’t do anything.

5 LinkButton_Click: This method returns to the List page when the user clicks the Return link.

Listing 9-11: The code-behind file for the Content Detail page (VB)

Partial Class Detail

Inherits System.Web.UI.Page

 

Private deptid As String

 

Private typeid As String

 

Protected Sub Page_Load( _

1

ByVal sender As Object, _

ByVal e As System.EventArgs) _

Handles Me.Load

deptid = Request.QueryString(“dept”) typeid = Request.QueryString(“type”) If User.IsInRole(deptid) Then

DetailsView1.AutoGenerateDeleteButton = True DetailsView1.AutoGenerateEditButton = True If Request.QueryString(“item”) = “-1” Then

DetailsView1.DefaultMode _ = DetailsViewMode.Insert

(continued)

328 Part V: Building Community Applications

Listing 9-11 (continued)

DetailsView1.AutoGenerateInsertButton = True

End If

Else

DetailsView1.AutoGenerateDeleteButton = False

DetailsView1.AutoGenerateEditButton = False

End If

End Sub

Protected Sub

DetailsView1_ItemDeleted( _

2

ByVal

sender As Object, _

 

ByVal e As System.Web.UI.WebControls _

 

.DetailsViewDeletedEventArgs) _

 

Handles DetailsView1.ItemDeleted

 

Response.Redirect(“List.aspx?type=” + typeid _

 

+ “&dept=” + deptid)

 

End Sub

 

 

Protected Sub DetailsView1_ItemInserted( _

3

ByVal sender As Object, _

 

ByVal e As System.Web.UI.WebControls _

 

.DetailsViewInsertedEventArgs) _

 

Handles DetailsView1.ItemInserted

 

Response.Redirect(“List.aspx?type=” + typeid _

 

+ “&dept=” + deptid)

 

End Sub

 

 

Protected Sub DetailsView1_ItemCommand( _

4

ByVal sender As Object, _

 

ByVal e As System.Web.UI.WebControls _

 

.DetailsViewCommandEventArgs) _

 

Handles DetailsView1.ItemCommand

 

Response.Redirect(“List.aspx?type=” + typeid _

 

+ “&dept=” + deptid)

 

End Sub

 

 

Protected Sub btnReturn_Click( _

5

ByVal sender As Object, _

 

ByVal e As System.EventArgs) _

 

Handles btnReturn.Click

 

Response.Redirect(“List.aspx?type=” + typeid _

 

+ “&dept=” + deptid)

 

End Sub

 

 

End Class

 

 

Chapter 10

Building a Web Forum

In This Chapter

Designing the Forum application

Creating the database for the Forum application

Building the Forum application’s pages

AWeb forum is, essentially, a form of online communication — a Web application that lets users post messages as well as reply to messages

left by other users. Forums are sort of like the back fences of the Internet, where people gather to talk about the important topics of the day.

Most forums are devoted to a particular subject so people with similar interests can gather to discuss topics that are important to them. For example, a forum might be devoted to music, politics, religion, or even (yes, it’s true) computer programming. And because these subjects tend to be broad, most forums are further organized into more focused discussion topics. For example, a forum on computer programming might have topics on programming languages, database programming, Web programming, and Windows programming.

Because forums are so popular as Web applications, many prebuilt forum applications are available to save you the trouble of creating your own — though you may still want to do so for a variety of reasons. If so, this chapter is for you. Here you’ll find a simple forum application written in ASP.NET.

Designing the Forum Application

The basic function of the Forum application is to let users post messages, which others can then display later and reply to. But within this basic requirement, there are many design decisions to be made along the way. Here’s a quick review of some of these basic decisions:

330 Part V: Building Community Applications

How will your forum messages be organized? A popular forum can receive hundreds, thousands, or even tens of thousands of posts per day. Clearly, you’ll need a way to keep all that information reasonably organized. The most common way to organize forum messages is to divide them into related topics. Then, when a user posts a message, he or she can specify the appropriate topic for the message. The Forum application presented in this chapter lets you organize your forum into any number of topics.

To provide an additional layer of organization, the Forum application lets you group the topics together into forums. For example, if you’re hosting discussions about holiday decorating, you might have a forum for each holiday. Then you might create topics for more specific subjects such as decorating, party tips, costumes, and so on.

Besides breaking down the site into forums and topics, each topic consists of one or more threads. A thread consists of an initial message posted by a user and additional messages posted in response to the initial message. Typically, the first message in a thread will be a question, and the follow-up messages will be answers to the initial question. There are two ways to organize replies within a thread:

Associate each reply with a specific message in the thread. Then threads take on a tree-like structure.

Associate each reply with the thread itself. Then the threads have a simple list structure.

For simplicity, the Forum application presented in this chapter associates replies with threads, not with other messages.

Will users be required to log in? If you want your forum’s membership to be limited, you should require that your users log in before they can post messages. To keep the code for this chapter’s application simple, it doesn’t require users to log in. Instead, each user must supply an e-mail address whenever he or she posts a message.

For more information about requiring users to log in, see Chapter 4.

Will the discussions be moderated? In an unmoderated forum, any user can post messages, and those messages are immediately viewable by other users. Unmoderated threads tend to be lively and free-flowing, but can also become unfocused and sometimes offensive.

The alternative to such potential chaos is to offer a moderated forum, in which a moderator must approve all messages posted to the forum. Moderated forums tend to stay closer to the topic at hand. Plus, the moderator can ban offensive posts. However, moderated forums require ongoing work from the moderator, and the time required for the moderator to approve new posts can stifle discussion.

For the sake of simplicity, the Forum application presented in this chapter is not moderated.

Chapter 10: Building a Web Forum 331

The User Interface for the

Forum Application

The Forum application uses the five pages shown in Figure 10-1. These pages are described in detail in the following sections.

The Forum Home page

The home page for the Forum application, Default.aspx, is shown in Figure 10-2. As you can see, this page lists the forums and topics that are available at the forum site. In this case, there are two forums and a total of five topics. To view the threads in one of the topics, the user clicks the link for the topic.

In case you’re wondering how the forums and topics are displayed, nested Repeater controls do the trick. The outer Repeater control displays the forums; then, for each forum, the inner Repeater control displays the available topics. (More about how to pull off this bit of programming trickery later in this chapter.)

Default.aspx

Forum

Home page

Threads.apx

Threads page

Messages.aspx

Messages

NewThread.aspx

page

Start New Thread page

Figure 10-1:

NewMessage.asp

The page

flow for the

Post Reply

Forum

page

application.

 

332 Part V: Building Community Applications

Figure 10-2:

The Forum

Home page.

The Threads page

The Threads page, shown in Figure 10-3, displays the threads in a given topic. For each thread, the page lists the thread’s subject, the e-mail address of the person who initiated the thread, the number of replies to the thread, and the date the thread was last replied to. A GridView control is used to display this list.

Note that the thread subjects are links. If the user clicks one of these links, the Forum application displays the messages for that thread, as described in the next section.

Notice also that beneath the list of threads are two links. The first takes the user to the New Thread page to create a new thread. The second link takes the user back to the forum’s Home page.

The Messages page

When the user clicks one of the threads in the Threads page, the messages for that thread are displayed, as shown in Figure 10-4. This page displays the topic name and the thread subject, followed by the thread’s original message and any replies. Note that each message is preceded by a header that identifies who posted the message and when.

Chapter 10: Building a Web Forum 333

Figure 10-3:

The Threads

page.

Figure 10-4:

The

Messages

page.

334 Part V: Building Community Applications

The New Thread page

If the user clicks the Start a New Thread link from the Threads page, the New Thread page is displayed, as shown in Figure 10-5. This page simply lets the user enter his or her e-mail address, a subject line, and the text for the

thread’s initial message. Then, when the user clicks the Post Message button, the new thread is written to the Forum application’s database.

Figure 10-5:

The New

Thread

page.

The Post Reply page

Figure 10-6 shows the Post Reply page, where you can add a message to an existing thread. To post a reply, you must enter your e-mail address and the text of your reply, then click the Post Reply button. This updates the database, then returns the user to the Messages page.

Chapter 10: Building a Web Forum 335

Figure 10-6:

The Post

Reply page.

Designing the Database

The database for the Forum application requires the following four tables:

Forums

Topics

Threads

Messages

Figure 10-7 presents a diagram that shows how these tables are related, and the following sections describe each table individually.

The Forums table

The Forums table stores information about each forum supported by the Web site. Table 10-1 lists the columns defined for this table.

336 Part V: Building Community Applications

 

Forums

 

 

 

forumid

 

 

 

name

Threads

Messages

 

description

 

threadid

 

 

msgid

 

 

topicid

 

 

threadid

 

 

subject

 

 

author

 

 

replies

 

 

date

 

 

author

 

Topics

message

 

lastpostdate

Figure 10-7:

topicid

 

 

 

formid

 

 

A diagram

 

 

name

 

 

of the Forum

 

 

description

 

 

database.

 

 

Table 10-1

The Forums Table

Column name

Type

Description

forumid

INT IDENTITY

An identity column that provides a

 

 

unique ID number for each forum

 

 

supported by the site. This is the

 

 

primary key for the Forums

 

 

table.

 

 

 

name

VARCHAR(255)

The forum’s name.

 

 

 

description

VARCHAR(255)

A short description of the forum.

 

 

 

The Topics table

The Topics table stores information for each of the topic areas available to users. Table 10-2 lists the columns defined for this table.

Table 10-2

The Topics Table

Column name

Type

Description

topicid

INT IDENTITY

An identity column that pro-

 

 

vides a unique ID number for

each topic area supported by the site. This is the primary key for the Topics table.