
ASP.NET 2.0 Everyday Apps for Dummies
.pdf

418 Part V: Building Community Applications
Listing 11-17 (continued)
Handles Me.Load
SqlDataSource1.SelectParameters(“username”) _
.DefaultValue = User.Identity.Name
End Sub |
|
Protected Sub btnCreate_Click( _ |
2 |
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnCreate.Click
SqlDataSource2.InsertParameters(“username”) _
.DefaultValue = User.Identity.Name
SqlDataSource2.InsertParameters(“name”) _
.DefaultValue = txtBlogName.Text
SqlDataSource2.InsertParameters(“description”) _
.DefaultValue = txtDescription.Text
SqlDataSource2.Insert()
GridView1.DataBind()
End Sub
End Class
Building the New Post Page
The New Post page lets a registered and logged-in user add a new post to one of his or her blogs. To see this page in action, refer to Figure 11-9. The following sections present the .aspx file and the code-behind files for this page.
The NewPost.aspx page
The .aspx file for the New Post page is shown in Listing 11-18. This page uses text boxes to let the user enter the subject and text for the new post and a SqlDataSource control to provide the INSERT statement used to record the post.
Listing 11-18: The NewPost.aspx page
<%@ Page Language=”C#” |
1 |
MasterPageFile=”~/MasterPage.master” |
|
AutoEventWireup=”true” |
|
CodeFile=”NewPost.aspx.cs” |
|
Inherits=”NewPost” |
|
Title=”Blog-O-Rama” %> |
|
<asp:Content ID=”Content1” Runat=”Server” |
2 |
|
|


420 Part V: Building Community Applications
Listing 11-18 (continued)
<asp:QueryStringParameter Name=”blogid” |
9 |
QueryStringField=”blog” |
|
Type=”String” /> |
|
<asp:ControlParameter Name=”subject” |
10 |
ControlID=”txtSubject” |
|
PropertyName=”Text” |
|
Type=”String” /> |
|
<asp:ControlParameter Name=”post” |
11 |
ControlID=”txtPostText”
PropertyName=”Text” Type=”String” />
</InsertParameters>
</asp:SqlDataSource>
</asp:Content>
The most important lines of this file are described in the following paragraphs:
1 You will have 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 page uses an HTML table to manage the layout of its controls.
4 A text box lets the user enter the subject for the new post. Note that a RequiredFieldValidator is used to ensure that the user doesn’t leave the subject blank.
5 A multi-line text box lets the user enter the text of the new post. Again, a RequiredFieldValidator is used to make sure the post isn’t left blank.
6 The Post button causes the new post to be added to the database. You should remove the OnClick attribute if you’re using Visual Basic instead of C#.
7 The Cancel button cancels the post and returns to the My Blogs page.
8 Although this page doesn’t use bound controls, a SQL Data Source is still used to insert the post into the database. The INSERT statement requires three parameters: blogid, subject,
and post.
9 The value of the blogid parameter is obtained from the query string named blog.

Chapter 11: Building a Blog Application 421
10 The subject parameter is bound to the Text property of the txtSubject text box.
11 The post parameter is bound to the Text property of the txtPostText text box.
The code-behind file for the New Post page
Listings 11-19 and 11-20 present the C# and Visual Basic versions of the codebehind file for the New Post page. As you can see, these code-behind files contain just one method, which handles the click event for the Post button.
Listing 11-19: The code-behind file for the New Post page (C#)
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 NewPost : System.Web.UI.Page |
|
{ |
|
protected void btnPost_Click( |
1 |
object sender, EventArgs e)
{
SqlDataSource1.Insert();
Response.Redirect(“MyBlogs.aspx”);
}
}
There’s only one numbered line to consider for this listing:
1 The btnPost_Click method executes when the user clicks the Post button. This method calls the Insert method of the data source to insert the new post into the Posts table. Then it redirects the user back to the MyBlogs.aspx page. Pretty simple, eh?

422 Part V: Building Community Applications
Listing 11-20: The code-behind file for the New Post page (VB)
Partial Class NewPost
Inherits System.Web.UI.Page |
|
Protected Sub btnPost_Click( _ |
1 |
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnPost.Click
SqlDataSource1.Insert()
Response.Redirect(“MyBlogs.aspx”)
End Sub
End Class

Part VI
The Part of Tens

In this part . . .
If you keep this book in the bathroom, the chapters in this section are the ones that you’ll read most. Each chapter consists of ten (more or less) entertaining (okay,
useful ) things that are worth knowing about various aspects of ASP.NET programming. Without further ado, here they are, direct from the home office in sunny Fresno, California.

Chapter 12
Ten New Features of ASP.NET 2.0
In This Chapter
The new code-behind model
Special application folders such as App_Code and App_Data
Master Pages
Login controls
Data controls
The Wizard control
The Generics feature
This book assumes that you already know a bit of ASP.NET programming. Okay, you don’t have to be an expert, but this book is not a beginner’s
tutorial; it assumes you know the basics — concepts such as how to code ASP tags to create server controls, and how to write Visual Basic or C# code to handle events such as button clicks.
If you have never written a line of ASP.NET code, I suggest you put this book down momentarily and spend some quality time in a copy of my book,
ASP.NET 2.0 All-In-One Desk Reference For Dummies, published (of course) by the good people at Wiley.
With that important disclaimer out of the way, I realize that although you may have worked with ASP.NET 1.0 or 1.1, this may well be your first exposure to ASP.NET 2.0, the new release issued in November 2005. ASP.NET 2.0 introduces a bevy of new and important features to the ASP.NET programmer’s tool chest.
And so, without further ado, this chapter introduces you to ten of the best new features of ASP.NET 2.0. You’ll find that all applications presented in this book use one or more of these new features. And each of these new features is used at least once in this book.
Note that this isn’t a comprehensive list of what’s new in ASP.NET 2.0. Instead, I’ve focused on the new programming features that I’ve utilized to create the applications in this book. I didn’t include features that I didn’t use in these applications, such as Web Parts or Themes.
