Creating the Slideshows Master Page


The page you ll create to enter slideshow master information is a variation on pages you created in earlier chapters. This new page will allow you to enter a slideshow name (which can be a code or an ID) and the title; the page will then insert the two values into the Slideshows table in the WebMatrix database. This type of entry page adds records to the database much as the guestbook page did that you created in Chapter 10.

You can improve on the functions of a basic data-entry page in a variety of ways. You can make some improvements by using the MxDataGrid control. In this section, I ll show you how to create a slideshow entry page that has the following features:

  • The page checks existing slideshows by name and displays an error if you try to reuse a slideshow name.

  • The page displays all the slideshows you ve created already.

  • The page allows you to jump to another page, where you can enter detail information (individual slides) for a selected slideshow.

When the page is running, it will look like Figure 13-1.

click to expand
Figure 13-1: A page that allows you to enter slideshow master information as well as jump to a details page.

As you can see, this page is not very complicated. To insert new slideshow records, you need only a couple of text boxes one for the slideshow name and another for the slideshow title. You ll start by creating the page and configuring the text boxes.

Create the  CreateSlideshow.aspx page and add controls

  1. In Web Matrix, create a new page named  CreateSlideshow.aspx.

  2. Add a heading, such as Create and Edit Slideshows.

  3. Using Figure 13-1 as a guide, drag the following controls from the Web Controls tab of the Toolbox onto the page and set their properties as indicated:

    Control

    Property Settings

    TextBox

    ID: textSlideshowName

    MaxLength: 15

    RequiredFieldValidator

    ControlToValidate: textSlideshowName

    ErrorMessage: You must enter a slideshow name!

    TextBox

    ID: textSlideshowTitle

    MaxLength: 70

    RequiredFieldValidator

    ControlToValidate: textSlideshowTitle

    ErrorMessage: You must enter a slideshow title!

    Button

    ID: buttonCreateSlideshow

    Text: Create Slideshow

    Label

    ID: labelError

    Text: (empty)

    Visible: False

    Font.Bold: True

    ForeColor: Red

Notice that I m having you set the MaxLength property for the two text boxes, which is an easy way to guarantee that users don t enter more text than the column definitions in the database will allow. In Chapter 10, you used a CustomValidator control to test the length of the user s input, but you were checking for both a minimum and a maximum length. If you want to check only the maximum length, you can simply set the MaxLength property for a text box control.

  • Type text into the page as captions for the two text boxes. As in step 3, use Figure 13-1 as a guide.

  • You need the controls you ve added here to enter new slideshows to the Slideshows data table. Next you ll add the ability to view the slideshows that already exist.

    Add an MxDataGrid control to display existing slideshows

    1. If you ve closed Web Matrix since you last worked with the Slideshows table, create a connection to the WebMatrix database in the Data window. If you need help creating a connection to the WebMatrix database, follow the steps in the section Creating the Guestbook Database Table, in Chapter 10.

    2. From the Data window, drag the Slideshows table onto the page. Dropping the Slideshows table onto the page creates the SqlDataSourceControl1 and MxDataGrid1 controls, already configured to access the Slideshows table.

    3. Select the MxDataGrid control, and set its AllowPaging property to False. That way, you ll be able to see all the slideshows you ve created without having to page through the grid.

    4. If you want, test the page by running it. Although the Create Slideshow button isn t hooked up yet, you can still check that the MxDataGrid1 control is displaying the slideshow data that you created in Chapter 12.

    You ve now finished adding controls to the page. It s time to write the code to insert slideshow records into the database.

    Inserting Slideshow Records into the Database

    As with the guestbook in Chapter 10, you can have Web Matrix create a method to insert a new record into the database. You ll add code that will call the generated method, and you ll add a few lines of code that will display a message if you try to enter a duplicate record. You ll also add code to make the MxDataGrid control show the new slideshow record as soon as you enter it.

    Generate code to insert a new slideshow

    1. In Web Matrix, switch to Code view.

    2. From the Toolbox, drag the Insert Data Method element onto the page, and when Web Matrix prompts you, type the information for connecting to the WebMatrix database.

    3. In the Query Builder Wizard, select the Slideshows table and then make sure that both the SlideshowName and SlideshowTitle fields are unchecked. When you re ready, the wizard will look like this:

      click to expand

  • Click Next, name the insert function InsertSlideshow, and then click Finish. Web Matrix writes a function into the page for inserting a new record into the Slideshows table.

  • The code to insert the record is complete; now you need to call that code. Switch to Design view, and double-click the Create Slideshow button to create a Click handler. Web Matrix switches you back to Code view. Add the following code to the buttonCreateSlideshow_Click handler:

    Sub buttonCreateSlideshow_Click(sender As Obje ct, e As EventArgs     labelError.Text = "     labelError.Visible = fals     Dim slideShowName As Strin     Dim slideShowTitle As Strin     slideShowName = textSlideshowName.Tex     If slideShowName <> Server.HtmlEncode(slid eShowName) The         labelError.Visible = tru         labelError.Text = "Illegal characters in slideshow name!         Exit Su     End I     slideShowTitle = Server.HtmlEncode(textSli deshowTitle.Text     Tr         InsertSlideshow(slideShowName, slideSh owTitle         textSlideshowName.Text = "         textSlideshowTitle.Text = "         MxDataGrid1.DataBind(     Catch ex As Exceptio         labelError.Visible = Tru         If ex.Message.IndexOf("PRIMARY KEY con straint") >= 0 The             labelError.Text =                  "Slideshow with that name already exists!         Els             labelError.Text = "Unable to save your slideshow.         End i     End Tr End Sub

    You re using a few new techniques in this Click handler, although for the most part the code should look familiar. You start by declaring the variables you ll need in the code. After getting values out of the text boxes, you check whether the user input contains any HTML characters. As I explained in Chapter 10, you shouldn t trust user input. I recommend that you disallow any HTML characters in the slideshow name. To disallow HTML characters in the slideshow name, you compare the value that the user entered in the text box against the results of encoding the string with the Server.HtmlEncode method. If the values are not equal, the Server.HtmlEncode method has found some HTML and converted it. In that case, you display an error and exit the handler. For the slideshow title, use the Server.HtmlEncode method to convert potentially harmful HTML characters to their display format.

    The code that calls the InsertSlideshow method is in a Try-Catch block, as usual. When you call the InsertSlideshow method, you pass it the values of the two text boxes. After inserting the new record, but still within the Try block, you call the MxDataGrid1 control s DataBind method. This call forces the grid to invoke the SQL Select command of the SqlDataSourceControl1 control. As a result, the grid is refreshed and displays the new slideshow record.

    In the Catch block, you display an error message in the labelError control so that you ll know why the insertion failed. In a Catch statement, you can have Microsoft Visual Basic .NET pass you an exception object that contains information about the error that caused the Catch block to execute. In this example, you receive the exception object using the variable ex. The exception object has various properties; the most useful is its Message property, which contains the text of the error. By default, the labelError control has no text and is invisible. If the insert process does raise an error, I make the control visible and set its text to an error message. However, each time the Click handler runs, I clear the labelError control and make it invisible again.

    In my experience, a common error when inserting a new slideshow record is that the slideshow name for the new record is a duplicate. If you experiment with the exception object and try to enter a duplicate slideshow record, the message of the exception object is as follows:

    Violation of PRIMARY KEY constraint  PK_Slides hows_2__51 . Cannot  insert duplicate key in object  Slideshows . T he statement has been  terminated.

    As you might know, a primary key constraint violation is SQL language for a duplicate primary key value. Because this is a common error and because the error message is somewhat ugly, you should create some special handling for this error. In this example, you use the IndexOf function, available for any string, to determine whether the words primary key constraint appear anywhere in the error message. The IndexOf function returns a number indicating where in the message the text primary key constraint appears. If the text does appear, you display a friendlier version of the error message. If the text doesn t appear in the message, you display a generic error message. Checking for specific strings in an error message, as you do here, will work fine for this application. But checking for specific strings in an error message doesn t work well if the message might be in a language other than English. In that case, you ll want to base error checking around error numbers or some other language-neutral information.

    Caution 

    Be careful when displaying error messages not to reveal any information that a malicious person can use to compromise your application. For more details, see Appendix A.

    After you ve entered the code for inserting a new record, test the page again. This time you should be able to enter a new record and save it. As soon as you do, the grid will be refreshed and will display your new record.

    Navigating to the Details Page

    Now that you ve finished programming the controls you added to the slideshow page, you can add slideshow records. You have only one task left in the  CreateSlideshow.aspx page programming the page so that it will allow you to jump to another page, where you can enter individual slides for a selected slideshow. You ll accomplish this task by using a facility in the MxDataGrid control that allows you to add data-bound hyperlinks to individual rows. When you click a hyperlink, it will take you to the page for editing slides for the selected slideshow.

    Add navigation to the MxDataGrid control

    1. In Design view, select the MxDataGrid1 control.

    2. In the Properties window, click the ellipsis button in the Fields property to open the MxDataGridField Collection Editor dialog box.

    3. Click the drop-down button next to the Add button, and select HyperLinkField. HyperLinkField will be added to the Members list. If you want, use the up and down arrow buttons next to the Members list to move the new HyperLinkField object to the top of the list. The item at the top of the list is displayed leftmost in the grid.

    4. Select the HyperLinkField object, and in the properties grid, set the following properties:

      The settings for DataNavigateUrlField and DataNavigateUrlFormatString bear some explaining. First, it will be helpful to understand the task you want to accomplish. You re creating a column of hyperlinks in the grid. All the hyperlinks will take the user to a page named  AddSlides.aspx, which you ll create in the next section. You re going to use the same technique that you used in Chapter 12 when you selected a slideshow in the ListBox control: you ll navigate to another page and use a query string to pass the slideshow name to that page. What you need, then, is a hyperlink whose target looks something like the following:

      AddSlides.aspx?slideshowname=grandmabday

      All the hyperlinks in the grid will have the same target URL, except that they ll have a different value for the slideshowname variable in the query string. The value in the DataNavigateUrlField tells the MxDataGrid control where to get the value for the query string. In this case, we want to get the value of the SlideshowName column for the current grid row. The DataNavigateUrlFormatString is the tricky part: it s an expression that creates a display string using the value of DataNavigateUrlField. The formatting we want to do is to embed the value of the DataNavigateUrlField property into a URL as a query string value. The format string therefore contains the URL we want, with a placeholder ({0}) for the DataNavigateUrlField value. You might find this technique somewhat counterintuitive, but it should make more sense when you see the hyperlinks in the page when it s running.

    5. Close the collection editor, and then run the page to test it. This time you ll see the extra column in the grid with the Add/Edit Slides hyperlinks. Hover over each of these hyperlinks in turn and look at the browser s status bar as you do so. You ll see how the grid has created and formatted the URLs of the hyperlinks so that the slideshow name appears in the query string.

    The hyperlinks point to a page that doesn t exist yet. Creating that page is the next step.




    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