Beginning ASP.NET 1.1 with Visual C# .NET 2003 phần 10 pps

81 293 0
Beginning ASP.NET 1.1 with Visual C# .NET 2003 phần 10 pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Figure D-38 Formatting Blocks of Code I often find Visual Studio .NET a bit messy at times – after adding this table, you can see that the code generated on my system was hardly tidy. In my default preferences (you can specify your own preferences via Tools | Options from the main menu) I specified that I wanted all tags to be in lowercase when editing HTML code, and I wanted to use two spaces indentation for all lines of code, yet the editor added the code shown in Figure D-39: 782 Appendix D Figure D-39 The solution to this problem is to select all the code and use the Edit | Advanced | Format Document menu option. After running this, my code was spaced out a bit better, and my preferred capitalization rules were followed as can be seen in Figure D-40: 783 Web Application Development Using Visual Studio .NET Figure D-40 You will find that the results of this will vary according to the default preferences you have on your system, but once you have set up the defaults as you like them, you'll find that this feature can be very handy! Developing the User Control In this table, set the row and cell attributes as shown in the code below. Leave the last cell empty for the moment: <tr style="VERTICAL-ALIGN: middle"> <td style="TEXT-ALIGN: left" width="200"> <a href="default.aspx"><img src="images/logo.gif" border="0" /></a> </td> <td style="TEXT-ALIGN: center" width="400"> <img src="images/teamlogo.gif" /> </td> <td style="TEXT-ALIGN: right" width="200"> 784 Appendix D </td> </tr> If you switch back to Design view, you'll see that the page taking shape as shown in Figure D-41: Figure D-41 Recall from the chapters that the header control had an AdRotator control that displayed different pictures of authors each time the page was requested. It's now time to add the AdRotator to the page. Either drag and drop this control into the right-most cell, or click to position your cursor in this cell and double-click the AdRotator control to add it to the page. This is shown in Figure D-42: 785 Web Application Development Using Visual Studio .NET Figure D-42 By default, the control is created with standard width and height attributes. To fit our images, you should change these attributes in the Properties pane so that the width is 100 pixels and the height is 95 pixels. To select the source for the AdRotator control, you need to first create the XML source file. Creating an XML File Right click on the WroxUnited project and create a new file. In the dialog that appears (shown in Figure D-43), create a new XML file and call it faces.xml: 786 Appendix D Figure D-43 In the newly created file, enter the code that we used previously and save the file: <?xml version="1.0" encoding="utf-8" ?> <Advertisements> <Ad> <ImageUrl>images/chrish_s_t.gif</ImageUrl> <NavigateUrl>players.aspx</NavigateUrl> <AlternateText>Player: Chris Hart</AlternateText> <Impressions>80</Impressions> <Keyword>ChrisH</Keyword> </Ad> <Ad> <ImageUrl>images/chrisu_s_t.gif</ImageUrl> <NavigateUrl>players.aspx</NavigateUrl> <AlternateText>Player: Chris Ullman</AlternateText> <Impressions>80</Impressions> <Keyword>ChrisU</Keyword> </Ad> <Ad> <ImageUrl>images/dave_s_t.gif</ImageUrl> <NavigateUrl>players.aspx</NavigateUrl> <AlternateText>Player: Dave Sussman</AlternateText> <Impressions>80</Impressions> <Keyword>Dave</Keyword> </Ad> <Ad> <ImageUrl>images/john_s_t.gif</ImageUrl> <NavigateUrl>players.aspx</NavigateUrl> <AlternateText>Player: John Kauffman</AlternateText> <Impressions>80</Impressions> 787 Web Application Development Using Visual Studio .NET <Keyword>John</Keyword> </Ad> </Advertisements> That's all you need to do to create an XML file. Notice that with each element, Visual Studio .NET assists you by adding closing tags. Back in the header.ascx file, head to the Properties for the advertisement file again. Select the AdvertisementFile attribute and click on the button that appears. In the popup dialog shown in Figure D-44, select the Faces.xml file and click OK: Figure D-44 The header needs one last thing before it's finished, and that's the custom text. In HTML view, add the following code to the bottom of the control (below the last </table> statement): <h2><%= PageTitle %></h2> 788 Appendix D Finally, right-click on Header.ascx and select View Code. You are now in the code-behind page for the header control. Add the following line of code directly above the Page_Load sub, as shown in Figure D- 45: public string PageTitle = ""; Figure D-45 Time for a quick File | Save All and that's the control done! Time to add it to the page! Adding a User Control to a Page Switch to the Design view for Default.aspx. Click and drag Header.ascx from the Solution Explorer onto the page, right before the main header. This is shown in Figure D-46: 789 Web Application Development Using Visual Studio .NET Figure D-46 You'll notice that this will add the control its reference to the HTML of the page. Also, notice that the control appears as a gray box, with no design-time appearance (Web Matrix, you will recall, provided a sample of the appearance of each user control as it was added to a page). The newly added control is added with a default TagName of Header and TagPrefix of UC1. You can change this prefix to WroxUnited (if you prefer) by editing the HTML of the page. Since we need to tidy up this page in any case, let's do that now. Amend the highlighted lines of code (notice that you need to remove the <h1> tag and the default header text): <%@ Register TagPrefix="WroxUnited" TagName="Header" Src="Header.ascx" %> <%@ Page Language="vb" AutoEventWireup="false" Codebehind="Default.aspx.vb" debug="true" Inherits="WroxUnited.WebForm1"%> <body> <form id="Form1" method="post" runat="server"> <WroxUnited:header id="Header1" runat="server"> </WroxUnited:header> 790 Appendix D <table id="Table1" cellspacing="0" cellpadding="0" width="800" border="0"> The PageTitle attribute isn't used on this page, but it exists for use on other pages where the email update registration box doesn't appear. Running the page now will produce a more familiar screen, as shown in Figure D-47: Figure D-47 Adding Custom Classes It's time to look at adding a data access component. Right-click on the WroxUnited project and select Add New Item In the dialog shown in Figure D-48, select the Class template, and name the file DataAccessCode.cs. Figure D-48 791 Web Application Development Using Visual Studio .NET [...]... users If you tried to run an ASP.NET page, from a folder with the permission set 815 Appendix E to None, you would get the following – note the Execute Access Forbidden message in the page shown in Figure E-15: Figure E-15 ❑ Setting Execute permissions to Scripts Only means that users can also access any script-based pages, such as ASP.NET pages So if the user requests an ASP.NET page that's contained... confident with creating web applications in Visual Studio NET The best way to increase your confidence further is to try out adding more controls and pages yourself You may want to try to implement the whole of the Wrox United application in Visual Studio NET – this would give you experience of working with many different controls in the Visual Studio NET environment, as well as programming with many... allowed on the files contained within that directory Let's have a look at what each of these options means: ❑ Script source access: This permission enables users to access the source code of an ASP.NET page It's only possible to allow this permission if the Read or Write permission has already been assigned But we generally don't want our users to be able to view our ASP.NET source code, so we would... running IIS We'll describe a process for checking whether this is the case as part of the installation process Note that to install anything (not just ASP.NET, but literally anything) on Windows 2000, XP, and 2003 you need to be logged in as a user with administrative rights If you're uncertain about doing this, please consult your Windows documentation Right, let's get started! Try It Out Creating... machines, then you simply need to substitute the computer name of the appropriate Web server machine Managing Directories on Your Web Server Before installing ASP.NET, you need to make one last pit stop in IIS This is because when you run your ASP.NET pages, you need to understand where to place your pages, and how to make sure you have the permission to access them As this is governed by IIS, let's look... stands for the directory path C:\BegASPNET11 When executing ASP.NET pages, you can reduce the amount of typing you need to do in the URL, by using virtual directory names in your URL in place of the physical directory names We also set the permissions read and run – these must be set or the IIS security features will prevent you from running any ASP.NET pages The Execute checkbox is left unselected as allowing... see Visual Studio attempt to compile the project, but this process will now fail The blue squiggly underlines in Figure D-50 indicate method calls that VS.NET can no longer find: 792 Web Application Development Using Visual Studio NET Figure D-50 Add the following line of code directly above the Page_Load method: public DataAccessCode Data = new DataAccessCode(); Then prefix each underlined method with. .. that doesn't have the Read permission enabled, then the Web server will simply return an error message Note that when the folder has Read permission turned off, HTML files within the folder cannot be read; however, ASP.NET code within the folder can still be run Generally, directories containing information that you want to publish (such as HTML files, for example) should have the Read permission enabled,... environment, as well as programming with many different ASP.NET techniques Visual Studio NET is a large product, so if you really want to learn more about this tool, visit http://msdn.microsoft.com/vstudio/using/ 801 E Installing and Configuring IIS The installation processes for IIS on Windows 2000 Professional, Windows XP Professional, or Windows Server 2003 don't differ significantly The main difference... language-specific editions Working with Databases Using the Server Explorer If you click the Server Explorer tab (at the bottom of the Toolbox), you can see the database connectivity functionality of Visual Studio NET By default, this will look at little empty Click on the Connect to Database button to add a new connection, as shown in Figure D-52: 794 Web Application Development Using Visual Studio NET Figure . United application in Visual Studio .NET – this would give you experience of working with many different controls in the Visual Studio .NET environment, as well as programming with many different ASP. NET techniques. Visual. signatures look like. This is shown in Figure D- 51: 793 Web Application Development Using Visual Studio .NET Figure D- 51 Complete adding each method call with the Data object prefix, and you will. line to see what happens when each line of code is run by pressing F 11. When you reach a method call, you can either press F 11 to jump into the code where that method is defined and watch as the

Ngày đăng: 13/08/2014, 04:21

Tài liệu cùng người dùng

Tài liệu liên quan