Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

ASP.NET 2.0 Everyday Apps For Dumies 2006 phần 10 potx
PREMIUM
Số trang
80
Kích thước
1.5 MB
Định dạng
PDF
Lượt xem
1065

ASP.NET 2.0 Everyday Apps For Dumies 2006 phần 10 potx

Nội dung xem thử

Mô tả chi tiết

Building the Leave Comment Page

The Leave Comment page lets a Web site visitor add a comment to a post. To

see what this page looks like, flip back to Figure 11-5. The following sections

present the .aspx file and the code-behind files for this page.

The Comment.aspx page

The .aspx file for the Leave Comment page is shown in Listing 11-10. This

page displays the topic name in a FormView control at the top of the page.

Then text boxes are used to get the user’s name and comment.

Listing 11-10: The Comment.aspx page

<%@ Page Language=”C#” ➝1

MasterPageFile=”~/MasterPage.master”

AutoEventWireup=”true”

CodeFile=”Comment.aspx.cs”

Inherits=”Comment”

Title=”Blog-O-Rama” %>

<asp:Content ID=”Content1” Runat=”Server” ➝2

ContentPlaceHolderID=”ContentPlaceHolder1” >

<table border=”0” width=”700” > ➝3

<tr>

<td width=”80” valign=”top”>

Your name:

</td>

<td width=”620” valign=”top”>

<asp:TextBox ID=”txtName” runat=”server” ➝4

Width=”400px”/>

</td>

</tr>

<tr>

<td width=”80” valign=”top”>

Your comment:

</td>

<td width=”620” valign=”top”>

<asp:TextBox ID=”txtComment” runat=”server” ➝5

TextMode=”MultiLine”

Height=”200px”

Width=”400px” />

</td>

</tr>

</table>

<asp:Button ID=”btnPost” runat=”server” ➝6

Text=”Post”

OnClick=”btnPost_Click” />

(continued)

Chapter 11: Building a Blog Application 407

19_597760 ch11.qxp 1/11/06 10:00 PM Page 407

Listing 11-10 (continued)

<asp:Button ID=”btnCancel” runat=”server” ➝7

Text=”Cancel”

OnClick=”btnCancel_Click”/>

<asp:SqlDataSource ID=”SqlDataSource1” ➝8

runat=”server”

ConnectionString

=”<%$ ConnectionStrings:BlogConnectionString %>”

InsertCommand=”INSERT INTO [Comments]

([postid], [username], [comment])

VALUES (@postid, @username, @comment)” >

<InsertParameters>

<asp:QueryStringParameter Name=”postid” ➝9

Type=”String”

QueryStringField=”postid” />

<asp:ControlParameter Name=”username” ➝10

Type=”String”

ControlID=”txtName”

PropertyName=”Text” />

<asp:ControlParameter Name=”comment” ➝11

Type=”String”

ControlID=”txtComment”

PropertyName=”Text” />

</InsertParameters>

</asp:SqlDataSource>

</asp:Content>

The critical lines of this listing are described in the following paragraphs:

➝ 1 Don’t forget 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 This text box lets the Web site visitor enter his or her name.

➝ 5 This multi-line text box lets the Web site visitor enter the text of

his or her comment.

➝ 6 The Web site visitor clicks this button to record the comment.

For this line and the next, you should remove the OnClick

attribute if you’re using Visual Basic instead of C#.

➝ 7 This button cancels the comment and returns to the Blog page.

(Again, remove the OnClick attribute if you’re using VB instead

of C#.)

➝ 8 Even though this page doesn’t contain any bound controls, it still

uses SqlDataSource1 to insert the comment into the Comments

table. The InsertCommand attribute specifies an INSERT statement

that requires three parameters: postid, username, and comment.

408 Part V: Building Community Applications

19_597760 ch11.qxp 1/11/06 10:00 PM Page 408

➝ 9 The value of the postid parameter is obtained from the query

string field named postid.

➝ 10 The username parameter is bound to the txtName text box.

➝ 11 The comment parameter is bound to the txtComment text box.

The code-behind file for the

Leave Comment page

Listings 11-11 and 11-12 show the C# and Visual Basic versions of the code￾behind file for the Leave Comment page. As you can see, these code-behind

files contain just two methods, which handle the click event for the Post

and Cancel buttons.

Listing 11-11: The code-behind file for the Leave Comment 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 Comment : System.Web.UI.Page

{

protected void btnPost_Click( ➝1

object sender, EventArgs e)

{

SqlDataSource1.Insert();

Response.Redirect(“Blog.aspx?blog=”

+ Request.QueryString[“blog”]

+ “&post=”

+ Request.QueryString[“post”]);

}

protected void btnCancel_Click( ➝2

object sender, EventArgs e)

{

Response.Redirect(“Blog.aspx?blog=”

+ Request.QueryString[“blog”]

+ “&post=”

+ Request.QueryString[“post”]);

}

}

Chapter 11: Building a Blog Application 409

19_597760 ch11.qxp 1/11/06 10:00 PM Page 409

You’ll sleep better tonight if you read the following paragraphs, which

describe the most important two lines of this code-behind file:

➝ 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 comment into the Comments table. Then it

redirects to the Blog.aspx page.

➝ 2 The btnCancel_Click method is similar to the btnPost_Click

method, with one important exception: it doesn’t call the INSERT

method of the data source. As a result, any comment entered by

the user is ignored.

Listing 11-12: The code-behind file for the Leave Comment page (VB)

Partial Class Comment

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(“Blog.aspx?blog=” _

+ Request.QueryString(“blog”) _

+ “&post=” _

+ Request.QueryString(“post”))

End Sub

Protected Sub btnCancel_Click( _ ➝2

ByVal sender As Object, _

ByVal e As System.EventArgs) _

Handles btnCancel.Click

Response.Redirect(“Blog.aspx?blog=” _

+ Request.QueryString(“blog”) _

+ “&post=” _

+ Request.QueryString(“post”))

End Sub

End Class

Building the Login Page

The Login page is displayed if the user clicks the Login button provided by

the Master Page or tries to access the My Blogs page without first logging in.

The .aspx code for this page (pictured back in Figure 11-6) is shown in

Listing 11-13.

410 Part V: Building Community Applications

19_597760 ch11.qxp 1/11/06 10:00 PM Page 410

Listing 11-13: The Login.aspx page

<%@ Page Language=”C#” ➝1

MasterPageFile=”~/MasterPage.master”

AutoEventWireup=”true”

CodeFile=”Login.aspx.cs”

Inherits=”Login”

Title=”Blog-O-Rama” %>

<asp:Content ID=”Content1” Runat=”Server” ➝2

ContentPlaceHolderID=”ContentPlaceHolder1” >

<asp:Login ID=”Login1” runat=”Server” ➝3

DestinationPageUrl=”~/Default.aspx”

TitleText=”Please enter your account information:

<br /><br />”

CreateUserText=”New user?”

CreateUserUrl=”~/Register.aspx” />

</asp:Content>

A quick list explains the details of three key lines in this listing:

➝ 1 Remember to change the Language, AutoEventWireup, and

CodeFile attributes in the Page directive if you use Visual Basic.

➝ 2 The <Content> element provides the content that’s displayed for

the page.

➝ 3 This page displays just one control, a Login control that lets the

user enter a name and password to log in. For more information

about how this control works, refer to Chapter 4.

Building the Register Page

The Register page is displayed if the user clicks the New User? link on the

Login page or the Register link displayed by the Master Page. (To see what

the Register page looks like, flip back to Figure 11-7.) The .aspx file for this

page, which doesn’t require a code-behind file, is shown in Listing 11-14.

Listing 11-14: The Register.aspx page

<%@ Page Language=”C#” ➝1

AutoEventWireup=”true”

MasterPageFile=”~/MasterPage.master”

CodeFile=”Register.aspx.cs”

Inherits=”Register”

title=”Blog-O-Rama” %>

<asp:Content ID=”Content1” Runat=”Server” ➝2

(continued)

Chapter 11: Building a Blog Application 411

19_597760 ch11.qxp 1/11/06 10:00 PM Page 411

Listing 11-14 (continued)

ContentPlaceHolderID=”ContentPlaceHolder1” >

<asp:CreateUserWizard ID=”CreateUserWizard1” ➝3

runat=”server”

CreateUserButtonText=”Create Account”

ContinueDestinationPageUrl=”~\Admin\MyBlogs.aspx” >

</asp:CreateUserWizard>

</asp:Content>

Here are the details of three key lines in this listing:

➝ 1 Remember to change the Language, AutoEventWireup, and

CodeFile attributes in the Page directive if you use Visual Basic.

➝ 2 The <Content> element provides the content that’s displayed for

the page.

➝ 3 This page displays just one control, a CreateUserWizard con￾trol that walks the user through the steps required to register a

new user account. The ContinueDestinationPageUrl attribute

provides the URL of the page to be displayed when the user com￾pletes the Wizard. In this case, the My Blogs page will be displayed.

(For more information about how the CreateUserWizard con￾trol works, refer to Chapter 4.)

Building the My Blogs Page

The My Blogs page was originally shown back in Figure 11-8. It is similar to

the Blog Home page (Default.aspx), with four key differences:

1. It’s stored in the \Admin folder, which is protected from anonymous

access. That means that only users who have registered and logged in

can view it.

2. Rather than display all of the blogs in the Blogs table, it displays only

the blogs that were created by the current user.

3. It includes a link that takes the user to the Post page to add a new post

to one of his or her blogs.

4. It includes controls that let the user create a new blog.

The following sections present the .aspx code and code-behind files for

this page.

412 Part V: Building Community Applications

19_597760 ch11.qxp 1/11/06 10:00 PM Page 412

The MyBlogs.aspx page

The .aspx file for the My Blogs page is shown in Listing 11-15. It includes a

GridView control to display the user’s blogs and a set of text boxes, field val￾idators, and buttons that enable the user to create a new blog. In addition,

two SqlDataSource controls are used.

Listing 11-15: The My Blogs page (MyBlogs.aspx)

<%@ Page Language=”C#” ➝1

MasterPageFile=”~/MasterPage.master”

AutoEventWireup=”true”

CodeFile=”MyBlogs.aspx.cs”

Inherits=”MyBlogs”

Title=”My Blogs” %>

<asp:Content ID=”Content1” Runat=”Server” ➝2

ContentPlaceHolderID=”ContentPlaceHolder1” >

<h2>My Blogs</h2>

<asp:GridView ID=”GridView1” runat=”server” ➝3

AllowPaging=”True”

AutoGenerateColumns=”False”

DataSourceID=”SqlDataSource1”>

<Columns>

<asp:TemplateField> ➝4

<HeaderTemplate>

Blog

</HeaderTemplate>

<ItemTemplate>

<b>

<asp:LinkButton ID=”LinkButton1”

runat=”server”

Text=’<% #Bind(“name”) %>’

PostBackUrl=’<% #Bind(“blogid”,

“~\Blog.aspx?blog={0}”) %>’

CausesValidation=”False” />

<br />

<asp:Label ID=”Label2” runat=”server”

Text=’<% #Bind(“description”) %>’ />

</ItemTemplate>

<HeaderStyle HorizontalAlign=”Left” />

<ItemStyle HorizontalAlign=”Left”

Width=”250px” />

</asp:TemplateField>

<asp:BoundField ➝5

DataField=”username”

(continued)

Chapter 11: Building a Blog Application 413

19_597760 ch11.qxp 1/11/06 10:00 PM Page 413

Listing 11-15 (continued)

HeaderText=”Owner” >

<HeaderStyle HorizontalAlign=”Left” />

<ItemStyle Width=”100px” />

</asp:BoundField>

<asp:BoundField ➝6

DataField=”posts”

HeaderText=”Posts” >

<HeaderStyle HorizontalAlign=”Left” />

<ItemStyle Width=”80px” />

</asp:BoundField>

<asp:HyperLinkField ➝7

DataNavigateUrlFields=”blogid”

DataNavigateUrlFormatString

=”NewPost.aspx?blog={0}”

Text=”New Post”>

<ItemStyle Width=”70px” />

</asp:HyperLinkField>

</Columns>

</asp:GridView>

<asp:SqlDataSource ID=”SqlDataSource1” ➝8

runat=”server”

ConnectionString

=”<%$ ConnectionStrings:BlogConnectionString %>”

SelectCommand=”SELECT [blogid], [name],

[description], [username], [posts]

FROM [Blogs]

WHERE [username]=@username

ORDER BY [name]”>

<SelectParameters>

<asp:Parameter Name=”username” ➝9

Type=”String” />

</SelectParameters>

</asp:SqlDataSource>

<br />To create a new blog:<br />

<asp:Label ID=”Label3” runat=”server” ➝10

BorderStyle=”None” Text=”Blog name:”

Width=”80px” />

<asp:TextBox ID=”txtBlogName” runat=”server” />

<asp:RequiredFieldValidator

ID=”RequiredFieldValidator1” runat=”server”

ControlToValidate=”txtBlogName”

Display=”Dynamic”

ErrorMessage=”Required.” /><br />

<asp:Label ID=”Label4” runat=”server” ➝11

BorderStyle=”None” Text=”Description:”

Width=”80px” />

414 Part V: Building Community Applications

19_597760 ch11.qxp 1/11/06 10:00 PM Page 414

Tải ngay đi em, còn do dự, trời tối mất!