
ASP.NET 2.0 Everyday Apps For Dummies (2006)
.pdf

358 Part V: Building Community Applications
Listing 10-9 (continued)
</asp:SqlDataSource><br /> |
|
<asp:Button ID=”btnAdd” runat=”server” |
13 |
Text=”Add a Reply to this Thread” |
|
OnClick=”btnAdd_Click” /> |
|
<br /><br /> |
|
<asp:LinkButton ID=”btnReturn” |
14 |
runat=”server” |
|
Text=”Return to Threads page” |
|
OnClick=”btnReturn_Click” /> </asp:Content>
This page has 14 key points:
1 You’ll need to change the Language, AutoEventWireup, and CodeFile attributes in the Page directive if you use Visual Basic instead of C#.
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 table, 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 This FormView control displays the thread subject. It’s bound to the data source named SqlDataSource2.
7 The SqlDataSource2 data source retrieves the thread subject from the Threads table.
8 The value of the threadid parameter is bound to the query string named thread.
9 The GridView control displays the messages for the selected thread. Paging is enabled, and the data source is
SqlDataSource3.
10 The <Columns> element consists of a single template field that displays the date of the post, the e-mail address of the poster, and the text of the message.
11 The SqlDataSource3 data source retrieves all messages for the selected thread.

Chapter 10: Building a Web Forum 359
12 The value of the threadid parameter is taken from the thread query string.
13 The btnAdd button lets the user add a reply to the thread. (If you’re working with Visual Basic, you should omit the OnClick attribute.)
14 The btnReturn button returns the user to the Threads page.
The code-behind file for the Messages page
Listings 10-10 and 10-11 show the C# and Visual Basic versions of the codebehind file for the Messages page, which handles the Click events for the two buttons on this page.
Listing 10-10: The code-behind file for the Messages 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 Messages : System.Web.UI.Page |
|
{ |
|
protected void btnAdd_Click(object sender, |
1 |
EventArgs e) |
|
{ |
|
string topicid = Request.QueryString[“topic”]; string threadid = Request.QueryString[“thread”]; Response.Redirect(“NewMessage.aspx?topic=”
+ topicid + “&thread=” + threadid);
} |
|
protected void btnReturn_Click(object sender, |
2 |
EventArgs e)
{
Response.Redirect(“Threads.aspx?topic=”
+ Request.QueryString[“topic”]);
}
}



362 Part V: Building Community Applications
Listing 10-12 (continued)
ControlToValidate=”txtEmail” |
|
ErrorMessage=”Required.” /> |
|
</td></tr> |
|
<tr><td Width=”125”> |
|
Subject: |
|
</td><td width=”300” valign=”top”> |
|
<asp:TextBox ID=”txtSubject” runat=”server” |
7 |
Width=”300px” /> |
|
<asp:RequiredFieldValidator |
|
ID=”RequiredFieldValidator2” |
|
runat=”server” |
|
ControlToValidate=”txtMessage” |
|
ErrorMessage=”Required.” /> |
|
</td></tr> |
|
<tr><td width=”125” valign=”top”> |
|
Message: |
|
</td><td width=”300”> |
|
<asp:TextBox ID=”txtMessage” runat=”server” |
8 |
TextMode=”MultiLine” |
|
Width=”300px” Height=”300px”/> |
|
<asp:RequiredFieldValidator |
|
ID=”RequiredFieldValidator3” |
|
runat=”server” |
|
ControlToValidate=”txtSubject” |
|
ErrorMessage=”Required.” /> |
|
</td></tr> |
|
</table> |
|
<asp:Button ID=”btnPost” runat=”server” |
9 |
OnClick=”btnPost_Click” |
|
Text=”Post Message” /> |
|
<asp:Button ID=”btnCancel” runat=”server” |
10 |
OnClick=”btnCancel_Click” Text=”Cancel” /> |
|
</asp:Content>
Here are details of the ten most important lines of this file:
1 You’ll need to change the Language, AutoEventWireup, and CodeFile attributes in the Page directive if you use Visual Basic instead of C#.
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.


364 Part V: Building Community Applications
Listing 10-13 (continued)
string cs = WebConfigurationManager
.ConnectionStrings[“ForumConnectionString”]
.ConnectionString;
string insertThread = “INSERT Threads “
+“(topicid, subject, replies, “
+“author, lastpostdate)”
+“VALUES(@topicid, @subject, @replies, “
+“ uthor, @lastpostdate)”;
string getThreadID = “SELECT @@IDENTITY”; string insertMessage = “INSERT Messages “
+“(threadid, author, date, message) “
+“VALUES(@threadid, uthor, @date, @message)”; SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand(insertThread,
con);
// insert the thread |
5 |
cmd.Parameters.AddWithValue(“topicid”, |
|
Request.QueryString[“topic”]); |
|
cmd.Parameters.AddWithValue(“subject”, |
|
txtSubject.Text); |
|
cmd.Parameters.AddWithValue(“replies”, 0); |
|
cmd.Parameters.AddWithValue(“author”, |
|
txtEmail.Text); |
|
cmd.Parameters.AddWithValue(“lastpostdate”, |
|
DateTime.Now); |
|
con.Open(); |
|
cmd.ExecuteNonQuery(); |
|
// get the thread ID |
6 |
cmd.CommandText = getThreadID; |
|
string threadid = cmd.ExecuteScalar().ToString(); |
|
// insert the message |
7 |
cmd.CommandText = insertMessage; |
|
cmd.Parameters.Clear(); |
|
cmd.Parameters.AddWithValue(“threadid”, |
|
threadid); |
|
cmd.Parameters.AddWithValue(“author”, |
|
txtEmail.Text); |
|
cmd.Parameters.AddWithValue(“date”, |
|
DateTime.Now); |
|
cmd.Parameters.AddWithValue(“message”, |
|
txtMessage.Text); |
|
cmd.ExecuteNonQuery(); |
|
con.Close(); |
|
Response.Redirect(“Messages.aspx?topic=” 8
+Request.QueryString[“topic”]
+“&thread=” + threadid);

Chapter 10: Building a Web Forum 365
} |
|
protected void btnCancel_Click( |
9 |
object sender, EventArgs e)
{
Response.Redirect(“Threads.aspx?topic=”
+ Request.QueryString[“topic”]);
}
}
Here are the highlights of this code-behind file:
1 Because the code-behind file uses the WebConfigurationManager class to retrieve the database connection string from the web.config file, the code-behind file requires the System.Web.Configuration namespace.
2 Similarly, the System.Data.SqlClient namespace is required because the code-behind file uses ADO.NET classes.
3 The btnPost_Click method executes when the user clicks the Post Message button, writing a row to both the Threads table and the Messages table.
4 These lines create the ADO.NET database objects used by the btnPost_Click method, in a specific sequence:
1.The connection string is retrieved from web.config.
2.Three string variables are created and initialized to the INSERT statements needed to insert data into the Threads and
Messages tables.
3.The SELECT statement used to determine the threadid created for the new thread.
4.SqlConnection and SqlCommand objects are created.
5 These lines insert the thread data into the Threads table. First the parameters used by the INSERT statement for the Threads table are created. Then the connection opens and the INSERT statement executes.
6 These lines use the SQL statement SELECT @@IDENTITY to retrieve the threadid value that was assigned to the newly created thread. The threadid value is saved in a local variable named threadid.
7 These lines insert the message for the thread into the Messages table. First, the parameters used by the INSERT statement are created. Then, the INSERT statement executes and the connection is closed.

366 Part V: Building Community Applications
8 This line redirects the user to the Messages.aspx page, which shows the newly created thread.
9 This method executes when the user clicks the Cancel button. It simply redirects the user back to the Threads.aspx page.
Listing 10-14: The code-behind file for the New Thread page (VB version)
Imports System.Web.Configuration |
1 |
Imports System.Data.SqlClient |
2 |
Partial Class NewThread |
|
Inherits System.Web.UI.Page |
|
Protected Sub btnPost_Click( _ |
3 |
ByVal sender As Object, _ |
|
ByVal e As System.EventArgs) _ |
|
Handles btnPost.Click |
|
‘set up the data objects |
4 |
Dim cs As String |
|
cs = WebConfigurationManager _ |
|
.ConnectionStrings(“ForumConnectionString”) _ |
|
.ConnectionString |
|
Dim insertThread As String |
|
insertThread = “INSERT Threads “ _ |
|
+“(topicid, subject, replies, “ _
+“author, lastpostdate)” _
+“VALUES(@topicid, @subject, @replies, “ _
+“ uthor, @lastpostdate)”
Dim getThreadID As String getThreadID = “SELECT @@IDENTITY” Dim insertMessage As String insertMessage = “INSERT Messages “ _
+“(threadid, author, date, message) “ _
+“VALUES(@threadid, uthor, @date, @message)”
Dim con As SqlConnection con = New SqlConnection(cs) Dim cmd As SqlCommand
cmd = New SqlCommand(insertThread, con)
‘insert the thread |
5 |
cmd.Parameters.AddWithValue(“topicid”, _ |
|
Request.QueryString(“topic”)) |
|
cmd.Parameters.AddWithValue(“subject”, _ |
|
txtSubject.Text) |
|
cmd.Parameters.AddWithValue(“replies”, 0) |
|