Some of the user-selected items are carried in session variables. Table 23.3 shows and describes the session variables that are used in the application. Listing 23.3 shows the Page_Load() method that's behind the Default.aspx page. This method initializes the session variables if they don't exist and populates the user interface objects if this is not a post back.
An application variable named CornerImages contains the list of corner images. This application variable holds a collection of file names. This list of file names is generated in the Application_Start() method of the Global.asax file (see Listing 23.4). Another application variable named TileList contains the available tile images found in the BackgroundTiles directory. Four DropDownList objects contain a selectable list of corner images. The following code shows how the first DropDownList object is populated; the subsequent DropDownList objects are populated by setting the DataSource property and calling the DataBind() method: C#String strFileList() strFileList = (String()) Application["CornerImages"]; ul.DataSource = strFileList; ul.DataBind();VB Dim strFileList() As String strFileList = Application("CornerImages") ul.DataSource = strFileList ul.DataBind() When the application first runs, all you see is a blank certificate, as shown in Figure 23.10. But the user interface objects will have been populated with the list of tiles and corner images. Listing 23.3 Page Initialization Code for Default.aspxPrivate Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load If Session("Tile") Is Nothing Then Session("Tile") = "LBlueBack.gif" End If If Session("Filename") Is Nothing Then Session("Filename") = Session.SessionID + ".gif" End If If Session("MainText") Is Nothing Then Session("MainText") = "" End If If Session("RightText") Is Nothing Then Session("RightText") = "" End If If Session("LeftText") Is Nothing Then Session("LeftText") = "" End If If Not IsPostBack Then Dim strFileList() As String strFileList = Application("CornerImages") ul.DataSource = strFileList ul.DataBind() ul.Items.Insert(0, "<Select Picture>") ur.DataSource = strFileList ur.DataBind() ur.Items.Insert(0, "<Select Picture>") bl.DataSource = strFileList bl.DataBind() bl.Items.Insert(0, "<Select Picture>") br.DataSource = strFileList br.DataBind() br.Items.Insert(0, "<Select Picture>") strFileList = Application("TileList") tile.DataSource = strFileList tile.DataBind() tile.SelectedIndex = 27 OutputImage() End If End Sub Figure 23.10. The Application Starts with a Clean Slate and Shows Only the Background.Listing 23.4 has the Application_Start() method. This method gets a list of .gif files from the BackgroundTiles directory and another list of .gif files from the CornerImages directory. These lists will be used during the life of the application, and keeping them in persistent lists will reduce the load on the server without requiring an adverse amount of resources. Getting a list of files in a directory is simple; we simply use the Directory.GetFiles() method and specify a path and a file mask. This method returns a list of files, and the following code shows how this is done: C#String strFileList(); strFileList = Directory.GetFiles( @"c:\inetpub\ASPNetSolutionsRoot\CertificatesVB\BackgroundTiles", "*.Gif");VB Dim strFileList() As String strFileList = _ Directory.GetFiles( _ "c:\inetpub\ASPNetSolutionsRoot\CertificatesVB\BackgroundTiles", _ "*.Gif") We don't need to keep the entire path because all we'll use in the application is the file name. We'll have to loop through the list and extract each file name. To do this, we find the index of the last '\' character. This will be the character immediately in front of the file name. (You could also use the System.IO.Path class instead.) If we take what's to the right of the '\' character to the end of the string, we'll have just the file name. Listing 23.4 The Code That Creates the List of the Corner Images and the Tile ImagesSub Application_Start(ByVal sender As Object, ByVal e As EventArgs) Try Dim strFileList() As String strFileList = _ Directory.GetFiles( _ "c:\inetpub\ASPNetSolutionsRoot\CertificatesVB\BackgroundTiles", _ "*.Gif") Dim i As Integer For i = 0 To strFileList.Length - 1 Dim nIndex As Integer = strFileList(i).LastIndexOf("\") Dim nLength As Integer = strFileList(i).Length strFileList(i) = strFileList(i).Substring(nIndex + 1, _ nLength - nIndex - 5) Next Application("TileList") = strFileList strFileList = _ Directory.GetFiles( _ "c:\inetpub\ASPNetSolutionsRoot\CertificatesVB\CornerImages", _ "*.Gif") For i = 0 To strFileList.Length - 1 Dim nIndex As Integer = strFileList(i).LastIndexOf("\") Dim nLength As Integer = strFileList(i).Length strFileList(i) = strFileList(i).Substring(nIndex + 1, _ nLength - nIndex - 5) Next Application("CornerImages") = strFileList Catch ex As Exception End Try End Sub |