Programming the Slideshow Viewer Page


The code you write for the slideshow viewer page will be similar to that of the original slideshow page, but you ll have to include the following modifications:

  • The slideshow viewer page must accept the slideshow name and title passed to it by the slideshow picker page.

  • The page must fetch the slides for the selected slideshow and load them into a dataset.

  • The code for the navigation controls must be modified to work with a dataset instead of an array.

Let s start by getting rid of the old code. In the  SlideshowViewer.aspx page, switch to Code view. Select all the existing code and delete it. (You probably don t often see instructions that tell you to delete everything.)

Now let s create the new code. First we ll read the name and title of the slideshow out of the query string. In Code view, create a new Page_Load handler, and add the following code to it, as shown here:

Sub Page_Load(     If Not Page.IsPostBack The         Dim slideShowName as Strin         Dim slideShowTitle as Strin         If Request.QueryString("slideShowName" ) Is Nothing The             labelSlideshowTitle.Text = "No sli deshow was selected!             Exit Su         Els             slideShowName = Request.QueryStrin g("slideShowName"             slideShowTitle = Request.QueryStri ng("slideShowTitle"             labelSlideshowTitle.Text = Server. HtmlEncode(slideShowTitle         End I     End I End Sub

The code checks the page s IsPostBack property to see whether this is the first time the page is running. If it is the first time, the code then checks that the slideShowName value has been passed in the query string. Checking for a query string value is necessary in case the page somehow runs without being called from the slideshow picker. (We re testing only for one of the two values we need from the query string, on the assumption that if we get one value, we ll also get the other value.) If the query string does contain values, the code assigns them to some local variables and displays the title in the Label control we provided for that purpose. The next step is to have Web Matrix generate code to create a dataset.

Programming the Dataset

As mentioned, you want to keep the slide information in a dataset so that users can move back and forth between slides. You can have Web Matrix generate the code to query the database and create a dataset.

Generate code to create the dataset

  1. While in Code view, drag a Select Data Method item into Code view and drop it at the bottom of the page. Web Matrix starts the wizard that will help you create and populate a dataset.

  2. In the Connect To Database dialog box, type the name of the server you use to connect to MSDE and select the WebMatrix database from the drop-down list. After connecting to the database, Web Matrix displays the Query Builder dialog box to help you build a SQL Select statement.

  3. Under Tables, select Slides. Under Columns, select SlideUrl, SlideCaption, and SlideDate. For the slideshow viewer, we don t need the other columns. The Query Builder dialog box will look like this:

    click to expand

  • Click the WHERE button. Web Matrix displays the WHERE Clause Builder dialog box, shown here:

    click to expand

  • By creating a Where clause for the SQL Select statement, you ll be able to select only the slides for the slideshow the user wants to view.

  • Under Column, select SlideshowName. Under Right Operand, Web Matrix fills in @SlideshowName. That s the name of a variable that we ll fill in using the slideshow name we obtain from the user.

  • Click OK to close the WHERE Clause Builder dialog box. Web Matrix returns you to the Query Builder dialog box.

  • Click Next. Web Matrix displays the Query Preview dialog box.

  • Click Test Query. Web Matrix displays the Preview dialog box, which prompts you to supply a value for the @SlideshowName variable you created in step 5. Type the code or abbreviation or number of a slideshow that you created in the Slideshows table, and then click OK.

    Note 

    This step is optional if you can t remember any slideshow names, you can skip the test.

    If the test goes as hoped, you ll see a list of the slides that belong to the slideshow you specified. If you see no results, you might try the test again with a different slideshow name.

  • Click Next. Web Matrix displays a dialog box with the suggested name MyQueryMethod. Change the method name to GetSlides, and make sure the DataSet option button is selected. You re telling Web Matrix to create a function named GetSlides and that the function will create a dataset out of the result set returned by the SQL Select statement you ve built.

  • Click Finish. Web Matrix generates a function of about a dozen lines of code and inserts them into the page.

  • You now have the dataset defined for your page. You need to add code that creates an instance of the dataset and then populates it. However, you want to populate the dataset only the first time the page runs. After you ve filled the dataset, you can store it in the Session object. Thereafter, on every round trip you can simply get the dataset back out of the Session object.

    In Chapter 5, we stored the current slide number in viewstate to preserve the current slide number between round trips. We could do that again here, but since we re already working with the Session object, we might as well store the current slide number in the Session object as well. For a value as tiny as the current slide number, it makes virtually no difference whether we store the value in viewstate or in the Session object.

    Start by creating variables to hold the slide counters and the dataset. At the top of the page, outside of the Page_Load handler, add the following code:

    Dim currentSlide as Intege Dim slideCount as Intege Dim dsSlides as System.Data.DataSet

    Notice that when you specify the data type of the variable to hold the dataset (dsSlides), you need to provide a fully qualified type name (System.Data.DataSet). You have to qualify data objects when you work with them because they aren t inherently part of Web pages that is, data objects aren t in the System.Web namespace. (For more details about qualifying class names, see the sidebar Namespaces in Chapter 6.)

    The next step is to call the GetSlides function that you generated a few moments ago to create a dataset and then save the dataset in the Session object. In the Page_Load handler, add the following boldfaced code:

    Sub Page_Load(     If Not Page.IsPostBack The         Dim slideShowName as Strin         Dim slideShowTitle as Strin         If Request.QueryString("slideShowName" ) Is Nothing The             labelSlideshowTitle.Text = "No sli deshow was selected!             Exit Su         Els             slideShowName = Request.QueryStrin g("slideShowName"             slideShowTitle = Request.QueryStri ng("slideShowTitle"             labelSlideshowTitle.Text = slideSh owTitl         End I         dsSlides = GetSlides(slideShowName)         Session("dsSlides") = dsSlides         currentSlide = 0         DisplayCurrentSlide()     Else         dsSlides = Session("dsSlides")         currentSlide = Session("currentSlide")     End I End Sub 

    Calling the GetSlides function and passing it a slideshow name executes a SQL Select statement and fetches the appropriate slide records. The GetSlides function returns a dataset with the slide records, which you assign to the dsSlides variable you declared earlier. You then store the dataset in the Session object under the name dsSlides. The name you use for storing items in the Session object isn t important, but by using the same name for the dataset variable and the name of the value in the Session object, you ll reduce coding errors. You store the currentSlide value in the Session object in a similar way. The first time the page runs, you initialize the currentSlide value to 0 and store it in the Session object. (Remember that counting begins with 0 in Visual Basic .NET.)

    Take a look at the Else block of the Not Page.IsPostBack test. During a postback if IsPostBack is true we get the values we stored earlier back out of the Session object. This technique is very similar to what we did in Chapter 5 with the currentSlide value, only this time we also store and retrieve the dataset.

    The code also contains a call to a subroutine named DisplayCurrentSlide. You can guess that the routine is supposed to display a slide in the page. We ll create the DisplayCurrentSlide subroutine as part of the process of displaying the current slide, as I ll explain next.

    Displaying the Current Slide

    The technique for displaying a slide should be familiar to you from the first slideshow page you created you determine what slide number you re on, read the slide information from the list (in the current page, from the dataset), and then set properties of the Image control and the caption Label control using the slide information. If you added a Label control for the slide date, you ll want to display the slide date value too.

    The difference between the current page and the  Slideshow1.aspx page is in how to read the current slide information. I said earlier that the dataset can store one or more tables. (In the slideshow viewer page, we load only one table the Slides table into the dataset.) Each table in the dataset can contain multiple rows (records), of course. And each row has a number of columns, which in datasets are referred to as items. Because the dataset structure has several nested levels, getting values out of the dataset structure requires a somewhat lengthy expression. For example, the following statement gets the value of the SlideUrl column out of the first row (row 0) of the first table (table 0) of the dataset in dsSlides:

    slideURL = dsSlides.Tables(0).Rows(0).Item("Sl ideUrl") 

    Technically, Tables and Rows are collections (like an ArrayList object) and you re referencing specific elements in those collections. Because we re loading only one table into the dataset, we can refer to the table as Tables(0).

    For Next and Previous navigation, we ll need to know how many rows the dataset contains. As with the ArrayList object we used in Chapter 7, we can get that information from the Count property of the collection we want in this case, the Rows collection.

    To actually display the current slide, you ll create the subroutine named DisplayCurrentSlide that you referenced in the Page_Load handler. The code for the DisplayCurrentSlide subroutine looks like this:

    Sub DisplayCurrentSlide(     Dim slideURL As Strin     Dim caption as Strin     Dim slideDate as Strin     If dsSlides.Tables(0).Rows.Count = 0 The         labelSlideshowTitle.Text = "No slides  for this show!         Exit Su     End I     slideURL = dsSlides.Tables(0).Rows(current Slide).Item("SlideUrl"     caption = dsSlides.Tables(0).Rows(currentS lide).Item("SlideCaption"     slideDate = dsSlides.Tables(0).Rows(curren tSlide).Item("SlideDate"     imageSlide.ImageUrl = slideUr     labelCaption.Text = captio     If slideDate <> "" The         labelSlideDate.Text = "(" & slideDate  & ")     End I     labelNavbar.Text = "Slide " & (currentSlid e + 1) & " of " _          & dsSlides.Tables(0).Rows.Coun     Session("currentSlide") = currentSlid End Sub

    After declaring variables to hold the database values, the code gets a count of the number of rows in table 0 (the first and only table in the dataset). If the count is 0, the code quits the subroutine, because there are no slides to display. (A nice touch that I did not include here is to set the Visible property of the navigation buttons and of the imageSlide control to false if there are no slides to show.) If the count isn t 0, however, the code retrieves values out of the current row, which we identify using the current slide number, as shown here:

    slideURL = dsSlides.Tables(0).Rows(currentSlide). Item("SlideUrl") 

    The values that the code retrieves from the current row are then assigned to control properties, as we did in the  Slideshow1.aspx page. Notice that if the slideDate row value isn t blank, I put parentheses around the date before displaying it. After displaying navigation information, which again requires a count of the rows in the dataset, the code stores the current slide number in the Session object so that the slide number can be retrieved during the next round trip.

    Navigating Between Slides

    The final step in displaying slides is to re-create the navigation logic for the Next and Previous buttons. The technique is similar to the one you used to implement navigation in the  Slideshow1.aspx page, except that in the current page, you check the dataset row count to determine whether you ve run out of slides instead of checking the length of the array that you used in Chapter 5.

    To re-create the navigation, switch to Design view and double- click the Next button to create a Click handler. In the Click handler, increment the currentSlide number. Compare the current slide number with the count of rows in the dataset, and adjust the value accordingly. Then call the DisplayCurrentSlide routine. The code looks like this:

    Sub buttonNext_Click(sender As Object, e As Ev entArgs     currentSlide +=      If currentSlide > dsSlides.Tables(0).Rows. Count - 1  The         currentSlide =      End I     DisplayCurrentSlide( End Sub

    In Design view, double-click the Previous button to create a similar handler for the buttonPrevious control, which will look like the following:

    Sub buttonPrevious_Click(sender As Object, e A s EventArgs     currentSlide -=      If currentSlide < 0 The         currentSlide = dsSlides.Tables(0).Rows .Count -      End I     DisplayCurrentSlide( End Sub

    And that s the end of the code for the slideshow. It s time to test what you ve done.

    Testing the Slideshow Viewer Page

    You have a number of scenarios to test with the new slideshow viewer page, including a couple of error scenarios. First run the  SlideshowViewer.aspx page. By running the page directly, you ll check what happens if the page is called without a query string. Confirm that the page displays the error message No slideshow was selected!

    Close the browser. In Web Matrix, switch to or load the  SlideshowPicker.aspx page and run it. In the picker page, select the title of a slideshow that you know has some slides in it and then click Run Slideshow. The viewer page will be displayed, and the first slide in the slideshow will appear. Use the navigation buttons to move between slides. Be sure to navigate to the end of the slide list to verify that the slides will wrap around when you run out of slides.

    Finally, click the Return To Slideshow Picker link. If you have a slideshow that doesn t have any slides defined for it, select that slideshow and then click Run Slideshow. When the slideshow viewer page is displayed, the page should display the message No slides for this show! .




    Microsoft ASP. NET Web Matrix Starter Kit
    Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
    ISBN: 0735618569
    EAN: 2147483647
    Year: 2003
    Pages: 169
    Authors: Mike Pope
    BUY ON AMAZON

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