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 9 potx
Nội dung xem thử
Mô tả chi tiết
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();
}
}
Chapter 10: Building a Web Forum 347
18_597760 ch10.qxp 1/11/06 9:59 PM Page 347
Here’s a set of 14 explanations, matched to the 14 key lines of this listing:
➝ 1 The code-behind file needs access to the System.Web.
Configuration namespace because it uses the
WebConfigurationManager class to retrieve the database
connection string from the web.config file.
➝ 2 The System.Data.SqlClient namespace is required to use
ADO.NET classes such as DataSet, SqlConnection, and
SqlCommand.
➝ 3 This line defines a class instance variable of type DataSet. That
way, the dataset will be available to both of the methods in the
code-behind file.
➝ 4 The Page_Load method executes when the page loads. It fills the
dataset with data retrieved from the Forums and Topics tables; it
also sets up data binding for the ForumRepeater control and calls
that control’s DataBind method, which completes the binding.
➝ 5 This line retrieves the connection string from the web.config
file.
➝ 6 This statement creates an empty dataset object and assigns it to
the ds variable.
➝ 7 These lines create a table in the ds dataset named Forums by a)
creating a data adapter that retrieves rows from the Forums table
and b) calling the data adapter’s Fill method.
➝ 8 These lines create a second table in the dataset (named Topics)
by creating a data adapter (which retrieves data from this new
Topics table) and calling the data adapter’s Fill method.
➝ 9 These lines set the data source for the ForumRepeater control
to the Forums table in the dataset, and then call the DataBind
method to bind the Repeater control to its data.
➝ 10 This method is called each time an item is bound to the outer
Repeater. It binds the inner Repeater control to the topics
that are associated with the forum represented by the item.
➝ 11 The e argument includes a property named Item that represents the
item being bound. This statement calls that item’s FindControl
method to find the Repeater control named TopicRepeater.
That particular Repeater is then assigned to the variable named
r so it can be used later.
➝ 12 These lines retrieve the id of the forum represented by the current Repeater item. First, e.Item.DataItem is used to retrieve
a DataRowView object that lets you access the individual data
fields for the Repeater item. Then the forumid field is retrieved
and saved in a local variable named forumid.
348 Part V: Building Community Applications
18_597760 ch10.qxp 1/11/06 9:59 PM Page 348
➝ 13 These lines retrieve a DataView object for the Topics table of
the data set and set its row filter so it views only those rows
whose forumid field matches the value of the forumid variable.
➝ 14 These lines bind the Repeater control to the data view. This
causes the Repeater control to create a line for each topic that’s
associated with the forum.
Listing 10-5: The code-behind file for the home page (VB version)
Imports System.Web.Configuration ➝1
Imports System.Data.SqlClient ➝2
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Private ds As DataSet ➝3
Protected Sub Page_Load( _ ➝4
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
Dim con As String _ ➝5
= WebConfigurationManager _
.ConnectionStrings(“ForumConnectionString”) _
.ConnectionString
ds = New DataSet() ➝6
‘fill the Forums table ➝7
Dim sel As String _
= “SELECT [forumid], [name] “ _
+ “FROM Forums “ _
+ “ORDER BY [name]”
Dim da As SqlDataAdapter _
= 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()
End Sub
(continued)
Chapter 10: Building a Web Forum 349
18_597760 ch10.qxp 1/11/06 9:59 PM Page 349
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”
350 Part V: Building Community Applications
18_597760 ch10.qxp 1/11/06 9:59 PM Page 350
Title=”Topic Threads” %>
<asp:Content ID=”Content1” Runat=”Server” ➝2
ContentPlaceHolderID=”ContentPlaceHolder1” >
<asp:FormView ID=”FormView1” runat=”server” ➝3
DataSourceID=”SqlDataSource1”>
<ItemTemplate>
<h2>
<asp:Label ID=”nameLabel” runat=”server”
Text=’<%# Bind(“name”) %>’ />
</h2>
<h3>
<asp:Label ID=”descriptionLabel”
runat=”server”
Text=’<%# Bind(“description”) %>’ />
</h3>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID=”SqlDataSource1” ➝4
runat=”server”
ConnectionString=”<%$ ConnectionStrings
:ForumConnectionString %>”
SelectCommand=”SELECT [name], [description]
FROM [Topics] WHERE ([topicid] = @topicid)”>
<SelectParameters>
<asp:QueryStringParameter ➝5
Name=”topicid”
QueryStringField=”topic”
Type=”Int32” />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID=”GridView1” runat=”server” ➝6
AutoGenerateColumns=”False”
DataSourceID=”SqlDataSource2”
DataKeyNames=”threadid”
AllowPaging=”True”
PageSize=”15”
PagerSettings-Mode=”NumericFirstLast”
OnSelectedIndexChanged
=”GridView1_SelectedIndexChanged”>
<Columns>
<asp:ButtonField ➝7
CommandName=”Select”
DataTextField=”subject”
HeaderText=”Subject”
Text=”Button”>
<ItemStyle HorizontalAlign=”Left”
Width=”250px” />
<HeaderStyle HorizontalAlign=”Left” />
</asp:ButtonField>
<asp:BoundField ➝8
DataField=”author”
HeaderText=”Author” >
(continued)
Chapter 10: Building a Web Forum 351
18_597760 ch10.qxp 1/11/06 9:59 PM Page 351
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.
352 Part V: Building Community Applications
18_597760 ch10.qxp 1/11/06 9:59 PM Page 352