Part V: Creating the Magazine Articles

In this part of the project, you will create an ASP page to dynamically generate the magazine content based on the entries in the Magazine data source. ActiveX Data Objects (ADO) will read the data source and build the magazine article, and the linked style sheet will format the page.

Step 1

If you don't already have the Magazine project open, open it now. Choose New from the File menu. In the New dialog box, insert a new ASP page, name it CONTENT.ASP, and click OK.

Step 2

Creating the article page is a simple matter of reading the data out of the database and generating the article content. Add the following code to the BODY section of CONTENT.ASP:

<% ` Variables Dim objConnection Dim objRecordset Dim strSQL ` Open data source Set objConnection = Server.CreateObject("ADODB.Connection") objConnection.Open "Magazine" ` Build SQL statement strSQL = "SELECT * FROM Articles,Sections " strSQL = strSQL & "WHERE Sections.ArticleID = Articles.ArticleID " strSQL = strSQL & "AND Articles.ArticleID = " & _     Request.QueryString("ArticleID") ` Get records Set objRecordset = Server.CreateObject("ADODB.Recordset") objRecordset.Open strSQL, objConnection ` Get article information Dim strTitle Dim strSubTitle Dim strNote strTitle = objRecordset("ArticleTitle") & "" strSubTitle = objRecordset("ArticleSubTitle") & "" strNote = objRecordset("ArticleNote") & "" ` Article title If strTitle <> "" Then %>     <H1><%=strTitle%></H1><% End If ` Article subtitle If strSubTitle <> "" Then %>     <H4><%=strSubTitle%></H4><% End If ` Build article body ` Variables Dim strSection Dim strText Do While Not objRecordset.EOF     ` Get section header     strSection = objRecordset("SectionTitle") & ""     If strSection <> "" Then %>         <H2><%=strSection%></H2><%     End If     ` Get section text     strText = objRecordset("SectionText") & ""     If strText <> "" Then %>         <%=strText%> <%     End if     ` Get next section     objRecordset.MoveNext Loop ` Close database objRecordset.Close objConnection.Close Set objRecordset = Nothing Set objConnection = Nothing %> 

Step 3

The site style sheet manages font styles for the magazine articles. Add the following code to the HEAD section of CONTENT.ASP to establish a link to the style sheet:

<LINK REL=STYLESHEET HREF="Magazine.css"> 

Step 4

Save your work, and preview the site by right-clicking DEFAULT.ASP and selecting Browse With from the pop-up menu. In the Browse With dialog box, choose Internet Explorer. From the table of contents, select an article to view, and verify that your site is working. The result should look like the example in Figure 8-6.

Figure 8-6. Viewing an article.

Step 5

The Content Linking component can not only produce a table of contents, but it can also navigate through the linked pages. In this portion of the project, you will use the component to create navigational tools—two images that allow the user to move backward and forward through the linked pages. To start, change the CONTENT.ASP page attributes that display hyperlinks. In particular, change the <BODY> tag to prevent the images you use from being highlighted in the page. Use this code:

<BODY LINK="White" VLINK="White" ALINK="White"> 

If you use hard-coded Web pages in your content linking text file, the Content Linking component can automatically generate the next and previous links. In this project, however, you are generating content from a database, so you must perform some trickery to build the navigational tools. Add the following code to the top of the BODY section in CONTENT.ASP to create the navigational tools:

<% ` This code creates navigational tools for the site ` Variables Public objLinker Public intArticleID Public intArticles ` Get Content Linking object info Set objLinker = Server.CreateObject("MSWC.NextLink") ` Get ArticleID intArticleID = CInt(Request.QueryString("ArticleID")) intArticles = CInt(objLinker.GetListCount("Magazine.txt")) %> <!-- Navigational tools --> <DIV> <!-- Next article --> <%If intArticleID > 1 Then%>     <A HREF=         <%=objLinker.GetNthURL("Magazine.txt", intArticleID - 1)%>>     <IMG ID="imgBack" SRC="/Magazine/Images/Back.gif">     </A> <%End If%> <!-- Previous article --> <%If intArticleID < intArticles Then%>     <A HREF=<%=objLinker.GetNthURL("Magazine.txt", intArticleID + 1)%>>     <IMG ID="imgNext" SRC="/Magazine/Images/Next.gif">     </A> <%End If%> 

Step 6

When the user passes the mouse over one of the navigational controls, the page should provide some information about the article that will be displayed if the link is activated. You can show a description of the next link by using Dynamic HTML and the information in the content linking text file. In this step, you will create two <FONT> tags that link descriptions of the articles to the Next and Back icons. Add the following code to the BODY section of the page, just below the section that generates the image controls for navigation:

<!-- Article descriptions --> <%If intArticleID > 1 Then%>     <FONT FACE="Verdana" ID="txtBack" SIZE=4     STYLE="position:relative;visibility:hidden;color:red;margin-left:20px">     <%=objLinker.GetNthDescription("Magazine.txt", intArticleID - 1)%>     </FONT><BR> <%End If%> <%If intArticleID < intArticles Then%>     <FONT FACE="Verdana" ID="txtNext" SIZE=4     STYLE="position:relative;visibility:hidden;color:red;margin-left:20px">     <%=objLinker.GetNthDescription("Magazine.txt", intArticleID + 1)%>     </FONT> <%End If%> 

Step 7

In the <FONT> tags, the visibility argument of the STYLE attribute is initially set to hidden. Therefore, neither of the descriptions is visible at the start. A description will be displayed when the mouse passes over one of the images. Create a SCRIPT section in the HEAD section of the page to trap the mouse events and display the appropriate text. Here is the code:

<SCRIPT LANGUAGE="VBScript"> <!--     Sub imgBack_OnMouseOver()         txtBack.Style.Visibility = "Visible"     End Sub     Sub imgBack_OnMouseOut()         txtBack.Style.Visibility = "Hidden"     End Sub     Sub imgNext_OnMouseOver()         txtNext.Style.Visibility = "Visible"     End Sub     Sub imgNext_OnMouseOut()         txtNext.Style.Visibility = "Hidden"     End Sub --> </SCRIPT> 

Step 8

Save your work, and test the site. Your navigational controls and descriptions should be fully functional and should look like the example shown in Figure 8-7.

Figure 8-7. The navigational tools.



Programming Active Server Pages
Programming Active Server Pages (Microsoft Programming Series)
ISBN: 1572317000
EAN: 2147483647
Year: 1996
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net