Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 110 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
110
Dung lượng
6,83 MB
Nội dung
Cross-Page Posting The way in which Active Server Pages 2.0/3.0 (also called classic ASP) worked was that values from forms were usually posted to other pages. These pages were usually steps in a process that the end user worked through. With the introduction of ASP.NET on the other hand, pages in this environment posted back results to themselves in a step called a postback. One of the biggest requests of Web developers in the ASP.NET world has been the ability to do postbacks not only to the page from whence the values originated, but also the ability to do postbacks to other pages within the application. This new feature is something that has been provided with the release of ASP.NET 2.0. Cross-page posting (as it is referred) is an easy functionality to achieve now. It gives you the ability to post page values from one page ( Page1.aspx) to an entirely different page (Page2.aspx). Normally, when posting to the same page (as with ASP.NET 1.0/1.1), you could capture the postback in a postback event as shown here: If Page.IsPostBack Then ‘ do work here End If Now, let’s take a look at Page1.aspx and see how you accomplish cross-page posting with ASP.NET 2.0. <%@ Page Language=”VB” %> <script runat=”server”> Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Label1.Text = “Your name is: “ & TextBox1.Text & “<br>” & _ “Your appointment is on: “ & Calendar1.SelectedDate.ToLongDateString() End Sub </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”> <title>Cross-Page Posting</title> </head> <body> <form id=”form1” runat=”server”> <div> What is your name?<br /> <asp:TextBox ID=”TextBox1” Runat=”server”></asp:TextBox> <br /> <br /> When is your appointment?<br /> <asp:Calendar ID=”Calendar1” Runat=”server”> </asp:Calendar><br /> <asp:Button ID=”Button1” OnClick=”Button1_Click” Runat=”server” Text=”Do a PostBack to this Page” /> <br /> <br /> <asp:Button ID=”Button2” Runat=”server” Text=”Do a PostBack to Another Page” PostBackUrl=”~/page2.aspx” /> <br /> 628 Chapter 17 20_575368 ch17.qxd 10/7/05 11:07 PM Page 628 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com <br /> <asp:Label ID=”Label1” Runat=”server”></asp:Label> </div> </form> </body> </html> With Page1.aspx, you can see that there is nothing really different about this page— except for the Button2 server control. This page contains a new attribute which you will find with the Button, ImageButton, and LinkButton controls— the PostBackUrl attribute. The value of this attribute points to the location of the file that this page should post to. In this case, the PostBackUrl attribute states that this page should post to Page2.aspx. You can see that this is the only thing needed on the Page1.aspx to cause it to post back to another page. As for Button1, you can see that this is a simple button which will cause the page to post back to itself. This is nothing new as this has been the case even in ASP.NET 1.x. You can see the event handler for this postback in the OnClick attribute within the Button1 control. Pressing this button will cause the page to post back to itself and to populate the Label1 control that is at the bottom of the page. Clicking on the second button, though, will post to the second page, which is shown here: <%@ Page Language=”VB” %> <script runat=”server”> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim pp_TextBox1 As TextBox Dim pp_Calendar1 As Calendar pp_TextBox1 = CType(PreviousPage.FindControl(“TextBox1”), TextBox) pp_Calendar1 = CType(PreviousPage.FindControl(“Calendar1”), Calendar) Label1.Text = “Your name is: “ & pp_TextBox1.Text & “<br>” & _ “Your appointment is on: “ & _ pp_Calendar1.SelectedDate.ToLongDateString() End Sub </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”> <title>Second Page</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:Label ID=”Label1” Runat=”server”></asp:Label> </div> </form> </body> </html> In this page, the first step is that in the Page_Load event, instances of both the TextBox and Calendar controls are created. From here, these instances are populated with the values of these controls on the previous page ( Page1.aspx) by using the PreviousPage.FindControl() method. The String value 629 ASP.NET 2.0 Advanced Features 20_575368 ch17.qxd 10/7/05 11:07 PM Page 629 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com assigned to the FindControl method is the Id value of the ASP.NET server control from the originating page (in our case, TextBox1 and Calendar1). Once you have assigned the values to these control instances, you can then start working with the new controls and their values as if they were posted from the same page. You can also expose the server controls and other items as properties from Page1.aspx. This is illustrated here in this partial code sample: <%@ Page Language=”VB” %> <script runat=”server”> Public ReadOnly Property pp_TextBox1() As TextBox Get Return TextBox1 End Get End Property Public ReadOnly Property pp_Calendar1() As Calendar Get Return Calendar1 End Get End Property Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Label1.Text = “Your name is: “ & TextBox1.Text & “<br>” & _ “Your appointment is on: “ & Calendar1.SelectedDate.ToLongDateString() End Sub </script> Once you have exposed the properties you want from Page1.aspx, then you can easily get at these properties in the cross-page postback by then using the new PreviousPageType page directive. This is illustrated here in the following example: <%@ Page Language=”VB” %> <%@ PreviousPageType VirtualPath=”~/Page1.aspx” %> <script runat=”server”> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Label1.Text = “Your name is: “ & PreviousPage.pp_TextBox1.Text & “<br>” & _ “Your appointment is on: “ & _ PreviousPage.pp_Calendar1.SelectedDate.ToLongDateString() End Sub </script> After your properties on Page1.aspx, you can access them easily by strongly typing the PreviousPage property on Page2.aspx by the use of the PreviousPageType directive. The PreviousPageType directive specifies the page the post will come from. Using this directive allows you to specifically point at Page1.aspx. This is done using the VirtualPath attribute of the PreviousPageType directive. The VirtualPath attribute takes a String value whose value is the location of the directing page. 630 Chapter 17 20_575368 ch17.qxd 10/7/05 11:07 PM Page 630 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Once this association has been made, you can then use the PreviousPage property and you will see that the pp_TextBox1 and pp_Calendar1 properties that were created on Page1.aspx are now present in Visual Studio 2005’s IntelliSense. You will find that working with the PreviousPage property is a bit easier and is less error prone than using weak-typing. This is shown here in Figure 17-1. Figure 17-1 One thing to be careful of is to guard against browsers hitting a page that is expecting information from a cross-page post and this action causing errors if the information the second page is expecting isn’t there. Pages that were looking for postback information was something you always had to guard against before— even when dealing with ASP.NET pages (1.0/1.1) that performed postbacks to them- selves. With standard pages that aren’t cross-page posting, you would protect your code from this post- back behavior through the use of the Page.IsPostBack property as shown here: If Page.IsPostBack Then ‘ code here End If 631 ASP.NET 2.0 Advanced Features 20_575368 ch17.qxd 10/7/05 11:07 PM Page 631 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com When cross-page posting, you will want to use the Page.IsCrossPagePostBack property. <%@ Page Language=”VB” %> <%@ PreviousPageType VirtualPath=”~/Page1.aspx” %> <script runat=”server”> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) If Page.IsCrossPagePostBack Then Label1.Text = “Your name is: “ & PreviousPage.pp_TextBox1.Text & “<br>” & _ “Your appointment is on: “ & _ PreviousPage.pp_Calendar1.SelectedDate.ToLongDateString() Else Server.Transfer(“Page1.aspx”) End If End Sub </script> In this example, if someone hits this page without going to Page1.aspx first to get cross-posted to Page2.aspx, then the request will be checked to see if the request is a cross-post. If it is (checked using the Page.IsCrossPagePostBack property), then the code is run, otherwise the request is redirected to Page1.aspx. ASP.NET Advanced Compilation The last chapter, Chapter 16, covered how the compilation process works in ASP.NET. You can notice this compilation process and how it works when you hit one of the ASP.NET pages you have built for the first time in the fact that it takes a few seconds for the page to be generated. This is due to the fact that the ASP.NET application is being compiled into intermediate code when you first hit that page. One thing that makes this situation even less enjoyable is that each and every page will have this lag when that particular page is first requested. In a page’s first request, ASP.NET compiles the page class into a DLL and then this is written to the disk of the Web server. The great thing about ASP.NET is that on the second request, instead of need to com- pile the page again, the DLL is accessed instead—making the request for the page far quicker than oth- erwise. You will notice this yourself if you hit the refresh button on your browser to re-request the same page. You will notice a new snappiness to the page. Due to how the pages are compiled in ASP.NET, whenever you make changes to your pages within your application, the application is recompiled again and each and every page will again have this initial drag as it is compiled. This can be quite a pain if you are working with larger sites and you really can’t afford this kind of pause to page generation (even if it is only one time). ASP.NET 2.0 includes a couple of precompilation tools so that you don’t have to experience this cost of page-by-page compilation. Both of these processes precompile your entire application at once. The first precompilation option is to invoke precompile.axd directly in the browser as if it was a page of your application. If you are using the Web server that is built into Visual Studio 2005, your request would be structured in the following format: http://[host]:[port]/[application name]/precompile.axd 632 Chapter 17 20_575368 ch17.qxd 10/7/05 11:07 PM Page 632 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Though if you are using Microsoft’s Internet Information Server, your request would be structured in the following format: http://[host]/[application name]/precompile.axd Once run, and if successful, you will be notified in large bold text: The application was successfully precompiled. If there is an error on any of the pages of your application, you will be notified of this through this com- pilation process which will make note of the page and line of the error. If successful, this precompilation process will have gone through each element of your application and will have successfully compiled it all into a DLL, thereby removing the churn you would normally experience hitting each of your pages for the first time. The other method of precompilation is for when you are going to need to precompile your applications that are meant to be deployed. Contained within the .NET Framework 2.0, you will find a tool— aspnet_compiler. You will find this tool at C:\Windows\Microsoft.NET\Framework\v2.0.[xxxxx]\. This is a command-line tool and you will simply need to navigate the aforementioned location to use it. In the simplest case, you would use the following structure to precompile your ASP.NET application. aspnet_compiler –v [Application Name] –p[Physical Location] [Target] For an example of using this compiler, let’s suppose that you are compiling an application called Wrox which is located at C:\Websites\Wrox. For this, you would use the following construction: aspnet_compiler –v /Wrox –p C:\Websites\Wrox c:\Wrox If successful, the application will be compiled. The output of a successful compilation is shown here in Figure 17-2. Figure 17-2 The nice thing about this compilation process is that it hides your code for you by packaging it into a DLL where it will be quite hidden for casually prying eyes. If you look at the target location of the com- pilation process, you will still see the same structure and files as you had before, but if you look at the contents of the .aspx files, you will see the following: This is a marker file generated by the precompilation tool, And should not be deleted! 633 ASP.NET 2.0 Advanced Features 20_575368 ch17.qxd 10/7/05 11:07 PM Page 633 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com If you look at what was compiled by the aspnet_compiler tool, you will find a Code.dll in the bin folder. This is where all the code from the pages is located. To deploy this precompiled application, you will not only need to move the Code.dll file, but each folder and placer file which was generated by the compiler. Move everything that was generated by the compiler to the target server and the ASP.NET application will be able to run without any concerns. One important point about this second precompilation process is that it doesn’t precompile each and every file that is contained within your application. The files that are excluded from the precompilation process include: ❑ HTML files ❑ XML files ❑ XSD files ❑ Web.config files ❑ Text files If you want these types of files also precompiled along with the rest of your files, one trick is to change the file extensions of the files that allow for it to be an .aspx extension. Doing this will cause these files’ contents to also be batched in with the content from the other pages in the compilation process, thereby obfuscating their contents. Master Pages Many Web applications are built so that each of the pages of the application has some similarities. For instance, there might be a common header that is used on each and every page of your applications There also may be other common page elements including navigation sections, advertisements, footers, and more. It really isn’t so common to have your Web pages each have their own unique look and feel to them. What people are looking for in their applications is some kind of commonality to give the end user what works through a multipaged application. What is really needed for these types of applications is a way to provide a template that can be used by your pages — a sort of visual inheritance (as can be done with Windows Forms). With a new feature in ASP.NET 2.0 called master pages, you can now employ visual inheritance in your Web applications. The use of master pages means that you are working with a template file (the master page) which has a .master extension. Once a .master page is created, you can then take a content page, with an .aspx exten- sion, and make an association between the two files. Doing this will allow ASP.NET to take these two files and combine them into a single Web page to display in a browser. Figure 17-3 shows a diagram of how this works. 634 Chapter 17 20_575368 ch17.qxd 10/7/05 11:07 PM Page 634 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 17-3 Let’s now take a look at how we would make this work by first creating the master page. Creating a Master Page The first step is to create a template that will end up being our master page. You can build a master page using any text editor (such as Notepad), but you will find it far easier to use Visual Studio 2005 or Visual Web Developer, as I will show you here. Start within the Solution Explorer. Right-click on the solution and select Add New Item. In the Add New Item dialog, you will find the option to add a master page to the solution. This is illustrated here in Figure 17-4. Your master page options are quite similar to that of working with a standard .aspx page. You can either create master pages to be inline or you can have master pages which utilize the code-behind model. If you wish to use the code-behind model, make sure that you have the ‘Place code in separate file’ check box checked in the dialog — otherwise leave it blank. Creating an inline master page will produce a single .master file. Using the code-behind model produces a .master file in addition to a .master.vb or .master.cs file. Master Page MyMaster.master M Content Page Default.aspx C Combined Page Default.aspx MC 635 ASP.NET 2.0 Advanced Features 20_575368 ch17.qxd 10/7/05 11:07 PM Page 635 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 17-4 A master page should be built so that it contains one or more content regions that are utilized by the con- tent pages. The following master page example (named Wrox.master) contains two of these content areas: <%@ Master Language=”VB” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”> <title>Wrox</title> </head> <body> <form id=”form1” runat=”server”> <div> <table cellpadding=”3” border=”1”> <tr bgcolor=”silver”> <td colspan=”2”><h1>The Wrox Company Homepage</h1></td> </tr> <tr> <td> <asp:ContentPlaceHolder ID=”ContentPlaceHolder1” Runat=”server”> </asp:ContentPlaceHolder> </td> 636 Chapter 17 20_575368 ch17.qxd 10/7/05 11:07 PM Page 636 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com <td> <asp:ContentPlaceHolder ID=”ContentPlaceHolder2” Runat=”server”> </asp:ContentPlaceHolder> </td> </tr> <tr> <td colspan=”2”>Copyright 2006 - Wrox</td> </tr> </table> </div> </form> </body> </html> The first thing to notice is the <% Master %> directive at the top of the page instead of the standard <% Page %> directive. This specifies that this is a master page and cannot be generated without a content page associated with it. It isn’t a page that you can pull up in the browser. In this case, the Master direc- tive simply uses the Language attribute and nothing more, but you will find that it has a number of other attributes at its disposal to fine-tune the behavior of the page. The idea is to code the master page as you would any other .aspx page. This master page contains a simple table and two areas that are meant for the content pages. These areas are defined with the use of the ContentPlaceHolder server control. This page contains two ContentPlaceHolder controls. It will be only in these two specified areas where content pages will be allowed to interject content into the dynamically created page (as you will shortly see). The nice thing about working with master pages is that you don’t only have to work with them in the code view of the IDE, but Visual Studio 2005 also allows for you to work with them in the design view as well. This is illustrated here in Figure 17-5. You can see that in this view, you can work with the master page by simply dragging and dropping con- trols onto the design surface just as you would with any typical .aspx page. Creating the Content Page Now that there is a master page in your project that you can utilize, the next step is to create a content page which will do just that. To do this, again right-click on the solution from within the Solution Explorer of Visual Studio 2005 and select Add New Item. This time though, we are going to add a typical Web Form to the project. Though, before you hit the Add button, be sure that you check the Select a Master Page check box in the dialog. This informs VS2005 that we are going to be building a content page that will be associated with a master page. Doing this will then pull up a new dialog, which will allow you to select a master page to associate this new file with. This is shown here in Figure 17-6. 637 ASP.NET 2.0 Advanced Features 20_575368 ch17.qxd 10/7/05 11:07 PM Page 637 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... instance This is shown in Figure 1 7- 9 Figure 1 7- 9 Working through the configuration process for the SqlDataSource control, you must choose your data connection and then whether you want to store this connection in the web.config file (shown in Figure 1 7- 1 0) This is highly advisable 643 Chapter 17 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 7- 1 0 From this configuration... Figure 1 7- 7 Figure 1 7- 7 639 Chapter 17 Simpo This view shows you the entire template and the two content areas that this content page is allowed to deal with All the grayed-out areas are off limits and do not allow for any changes from the content page — while the lighted areas allow for you to deal with any type of content you wish For instance, not only can you and Split Unregistered Version - http://www.simpopdf.com... the screen in the Configure Select Statement dialog (see Figure 1 7- 1 4), click the Advanced Options button 648 ASP NET 2.0 Advanced Features Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 7- 1 4 This will pull up a new dialog titled Advanced SQL Generation Options as shown here in Figure 1 7- 1 5 Figure 1 7- 1 5 As shown in this dialog, make sure you select the Generate Insert,... as illustrated in Figure 1 7- 1 9 Figure 1 7- 1 9 The code for this version of the SiteMapPath control is as follows: ... Unregistered Version - http://www.simpopdf.com Figure 1 7- 1 1 Once you have configured the SqlDataSource control, the next step is to tie the GridView control to this SqlDataSource control instance This can be done through the GridView control’s smart tag as shown here in Figure 1 7- 1 2 You can also enable paging and sorting for the control in the same form Figure 1 7- 1 2 645 Chapter 17 The code generated... following as shown here in Figure 1 7- 1 3 6 47 Chapter 17 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 7- 1 3 Allowing for Editing and Deleting of Records with the GridView Now let’s expand upon the previous example by allowing for the editing and deleting of records that are displayed in the GridView If you are using the Visual Studio 2005 SqlDataSource configuration wizard... Visual Studio 2005 Solution Explorer or by clicking Build ➪ Configuration Manager in the Visual Studio menu From the tool, which will open up in the Document window, click on the Security tab Figure 1 7- 2 4 shows what this tab of the tool looks like Figure 1 7- 2 4 The first step is to click the link to start up the Security Setup Wizard This launched wizard is shown here in Figure 1 7- 2 5 661 Chapter 17 Simpo... for your content page This particular example uses a button-click event for when the end user submits the form Running this example would produce the following results as shown in Figure 1 7- 8 640 ASP NET 2.0 Advanced Features Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 7- 8 Declaring the Master Page Application-Wide As shown in our examples thus far, we have been declaring... SiteMapDataSource1 This simple construction produces the result as shown here in Figure 1 7- 2 2 Remember that by using the Auto Format link from the control’s smart tag, you can format the TreeView control with a wide variety of looks and feels 6 57 Chapter 17 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 7- 2 2 The TreeView is not only meant for site maps, but instead (as stated) it... look like the following as shown here in Figure 1 7- 1 7 Figure 1 7- 1 7 Don’t Stop There! This chapter has limited space, so there is only room to go through this one example, but it is important to realize that there are so many other DataSource controls at your disposal The ObjectDataSource control is rather powerful for those that wish to enforce a strict n-tier model and separate the data retrieval logic . Figure 1 7- 6 . 6 37 ASP.NET 2.0 Advanced Features 20_ 575 368 ch 17. qxd 10 /7/ 05 11: 07 PM Page 6 37 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1 7- 5 Figure 1 7- 6 638 Chapter. is shown here in Figure 1 7- 7 . Figure 1 7- 7 639 ASP.NET 2.0 Advanced Features 20_ 575 368 ch 17. qxd 10 /7/ 05 11: 07 PM Page 639 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com This. following as shown here in Figure 1 7- 1 3. 6 47 ASP.NET 2.0 Advanced Features 20_ 575 368 ch 17. qxd 10 /7/ 05 11: 07 PM Page 6 47 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com