
ASP.NET 2.0 Everyday Apps For Dummies (2006)
.pdf
Chapter 10: Building a Web Forum 347
EventArgs e) |
|
{ |
|
string con = WebConfigurationManager |
5 |
.ConnectionStrings[“ForumConnectionString”] |
|
.ConnectionString; |
|
ds = new DataSet(); |
6 |
// fill the Forums table |
7 |
string sel = “SELECT [forumid], [name] “ |
|
+ “FROM Forums “ |
|
+ “ORDER BY [name]”; |
|
SqlDataAdapter da = new SqlDataAdapter(sel, con); |
|
da.Fill(ds, “Forums”); |
|
// fill the Topics table |
8 |
sel = “SELECT [forumid], [topicid], “ |
|
+ “[name], [description] “ |
|
+ “FROM Topics “ |
|
+ “ORDER BY [name]”; |
|
da = new SqlDataAdapter(sel, con); |
|
da.Fill(ds, “Topics”); |
|
// bind the Forum repeater |
9 |
ForumRepeater.DataSource = ds.Tables[“Forums”] |
|
.DefaultView; |
|
ForumRepeater.DataBind(); |
|
} |
|
public void ForumRepeater_ItemDataBound( |
10 |
Object sender, RepeaterItemEventArgs e) |
|
{ |
|
Repeater r = ((Repeater)e.Item |
11 |
.FindControl(“TopicRepeater”)); |
|
DataRowView drv |
12 |
= (DataRowView)e.Item.DataItem; |
|
string forumid = drv[“forumid”].ToString(); |
|
DataView dv |
13 |
= ds.Tables[“Topics”].DefaultView; |
|
dv.RowFilter = “forumid=” + forumid; |
|
r.DataSource = dv; |
14 |
r.DataBind(); |
|
} |
|
}



350 Part V: Building Community Applications
Listing 10-5 (continued)
Protected Sub ForumRepeater_ItemDataBound( _ |
10 |
ByVal sender As Object, _ |
|
ByVal e As RepeaterItemEventArgs) _ |
|
Handles ForumRepeater.ItemDataBound |
|
Dim r As Repeater |
11 |
r = e.Item.FindControl(“TopicRepeater”) |
|
Dim drv As DataRowView |
12 |
drv = e.Item.DataItem |
|
Dim forumid As String = drv(“forumid”) |
|
Dim dv As DataView |
13 |
dv = ds.Tables(“Topics”).DefaultView |
|
dv.RowFilter = “forumid=” + forumid |
|
r.DataSource = dv |
14 |
r.DataBind() |
|
End Sub
End Class
Building the Threads Page
The Threads page displays all threads for the topic selected by the user. The Threads page knows which topic to display because the topic is passed as a query string field from the Default.aspx page. Then the Threads page uses a simple GridView control bound to a SqlDataSource to retrieve and display the threads.
A code-behind file is required to handle the Click event raised by the Start a New Thread link or the SelectedItemChanged event raised by the
GridView control.
The Threads.aspx page
Listing 10-6 presents the .aspx code for the Threads page. There’s nothing unusual about the code for this page, so you shouldn’t have much trouble following it.
Listing 10-6: The Threads.aspx page
<%@ Page Language=”C#” |
1 |
MasterPageFile=”~/MasterPage.master”
AutoEventWireup=”true”
CodeFile=”Threads.aspx.cs”
Inherits=”Threads”


352 Part V: Building Community Applications
Listing 10-6 (continued)
<HeaderStyle HorizontalAlign=”Left” /> |
|
<ItemStyle Width=”100px” /> |
|
</asp:BoundField> |
|
<asp:BoundField |
9 |
DataField=”replies” |
|
HeaderText=”Replies” > |
|
<HeaderStyle HorizontalAlign=”Center” /> |
|
<ItemStyle HorizontalAlign=”Center” |
|
Width=”70px” /> |
|
</asp:BoundField> |
|
<asp:BoundField |
10 |
DataField=”lastpostdate” |
|
HeaderText=”Last Post” |
|
DataFormatString=”{0:d}” > |
|
<HeaderStyle HorizontalAlign=”Center” /> |
|
<ItemStyle Width=”70px” /> |
|
</asp:BoundField> |
|
</Columns> |
|
</asp:GridView> |
|
<asp:SqlDataSource ID=”SqlDataSource2” |
11 |
runat=”server” |
|
ConnectionString=”<%$ ConnectionStrings |
|
:ForumConnectionString %>” |
|
SelectCommand=”SELECT [threadid], [topicid], |
|
[subject], [replies], [author], [lastpostdate] |
|
FROM [Threads] |
|
WHERE ([topicid] = @topicid) |
|
ORDER BY [lastpostdate]”> |
|
<SelectParameters> |
|
<asp:QueryStringParameter |
12 |
Name=”topicid” |
|
QueryStringField=”topic” |
|
Type=”Int32” /> |
|
</SelectParameters> |
|
</asp:SqlDataSource> |
|
<br /> |
|
<asp:LinkButton ID=”LinkButton1” |
13 |
runat=”server” |
|
Text=”Start a New Thread” |
|
OnClick=”LinkButton1_Click” /> |
|
<br /><br /> |
|
<asp:LinkButton ID=”btnReturn” |
14 |
runat=”server” |
|
PostBackUrl=”~/Default.aspx” |
|
Text=”Return to Forum Page” /> |
|
</asp:Content> |
|
This listing has 14 key points to explain: |
|
1 If you use the Visual Basic version of the code-behind file, be sure to change the Language, AutoEventWireup, and CodeFile attributes in the Page directive.

Chapter 10: Building a Web Forum 353
2 The <Content> element provides the content that’s displayed for the page.
3 This FormView control displays the topic name and description. It’s bound to the data source named SqlDataSource1.
4 The SqlDataSource1 data source retrieves the topic information from the Topics row, using the topicid parameter to specify the topic to be retrieved.
5 The value of the topicid parameter is bound to the query string named topic.
6 The GridView control displays the threads for the selected topic.
It’s bound to the SqlDataSource2 data source, and paging is enabled. Note that the GridView1_SelectedIndexChanged method is called if the user selects a thread.
If you’re working with Visual Basic, remove the
OnSelectedIndexChanged attribute.
7 The first column for the GridView control is a button field that displays the thread subject. The CommandName attribute specifies Select, so the row is selected when the user clicks this link.
8 The next column displays the e-mail address of the user that initially started the thread.
9 This column displays the number of replies to the thread.
10 This column displays the date of the last message posted to the thread.
11 The SqlDataSource2 data source provides the data for the GridView control. Its SelectCommand attribute specifies a SELECT statement that retrieves threads for the topic specified by the topicid parameter.
12 The topicid parameter is bound to the topic query string field.
13 This LinkButton control lets the user start a new thread.
If you’re using Visual Basic, you should omit the OnClick attribute.
14 This LinkButton sends the user back to the Forum Home page.
The code-behind file for the Threads page
The code-behind file for the Threads page handles the Click event for the Start a New Thread link and the SelectedIndexChanged event for the GridView control. Listings 10-7 and 10-8 show the C# and Visual Basic versions of this code-behind file.

354 Part V: Building Community Applications
Listing 10-7: The code-behind file for the Threads page (C# version)
using System; using System.Data;
using System.Configuration; using System.Collections; using System.Web;
using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class Threads : System.Web.UI.Page |
|
{ |
|
protected void LinkButton1_Click(object sender, |
1 |
EventArgs e) |
|
{ |
|
Response.Redirect(“NewThread.aspx?topic=” |
|
+ Request.QueryString[“topic”]); |
|
} |
|
protected void GridView1_SelectedIndexChanged( |
2 |
object sender, EventArgs e) |
|
{ |
|
string threadid
= GridView1.SelectedValue.ToString(); string topicid = Request.QueryString[“topic”];
Response.Redirect(“Messages.aspx?topic=” + topicid + “&thread=” + threadid);
}
}
The two methods in this code-behind file are the heart of how it works:
1 The LinkButton1_Click method is called when the user clicks the Start a New Thread link. It simply redirects to the NewThread.aspx page, passing the topic query string so the New Thread page knows which topic to create the thread in.
2 The GridView1_SelectedIndexChanged method is called when the user selects a thread in the GridView control. It retrieves the thread ID from the GridView control’s SelectedValue property and the topic ID from the topic query string field, then redirects to the Messages.aspx page, passing the thread and topic IDs as query string fields.

