1. Trang chủ
  2. » Công Nghệ Thông Tin

Pro Server Controls and AJAX Components phần 10 ppsx

73 389 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 73
Dung lượng 3,81 MB

Nội dung

668 CHAPTER 13 ■ PACKAGING AND DEPLOYMENT // Searched for: <searchQuery> section.Append( String.Format( rm.GetString("ResultStatusTemplate.SearchFor"), resultControl.Query)); section.Append("<br>"); // Result <StartIndex+1> - <EndIndex+1> of about // <TotalResultsCount> records // (accounting for zero based index) section.Append( String.Format( rm.GetString("ResultStatusTemplate.ResultAbout"), ((resultControl.PageNumber - 1) * (resultControl.PageSize)) + 1, resultControl.PageNumber * (resultControl.PageSize), resultControl.TotalResultsCount)); section.Append("&nbsp&nbsp"); header.Text = section.ToString(); } } } ResultItemTemplate provides the default display for each item from the search results. The control content added inside the container includes a hyperlink displaying the title field Live Search Result and providing a hyperlink to the value of its URL field. It also adds a label to display the snippet field and a label to display the URL field. It uses three separate data-binding routines to accomplish the data loading: BindLink, BindSnippet, and BindUrl. Listing 13-5 presents the full source code. Listing 13-5. The ResultItemTemplate.cs Class File using System; using System.Web.UI; using System.Web.UI.WebControls; namespace ControlsBook2Lib.CH12.LiveSearchControls { /// <summary> /// Default ResultItemTemplate implementation used by a /// Stock LiveSearch Lib Result control without a ItemTemplate /// </summary> public class ResultItemTemplate : ITemplate { /// <summary> /// Method puts template controls into container control /// </summary> /// <param name="container">Outside control container to template items</param> Cameron_865-2C13.fm Page 668 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 13 ■ PACKAGING AND DEPLOYMENT 669 public void InstantiateIn(Control container) { HyperLink link = new HyperLink(); link.DataBinding += new EventHandler(BindLink); container.Controls.Add(link); container.Controls.Add(new LiteralControl("<br>")); Label snippet = new Label(); snippet.DataBinding += new EventHandler(BindSnippet); container.Controls.Add(snippet); container.Controls.Add(new LiteralControl("<br>")); Label url = new Label(); url.DataBinding += new EventHandler(BindUrl); container.Controls.Add(url); container.Controls.Add(new LiteralControl("<br>")); container.Controls.Add(new LiteralControl("<br>")); } private LiveSearchService.Result GetResultElement(Control container) { ResultItem item = (ResultItem)container; return (LiveSearchService.Result)item.DataItem; } private void BindLink(object source, EventArgs e) { HyperLink link = (HyperLink)source; LiveSearchService.Result elem = GetResultElement(link.NamingContainer); link.Text = elem.Title; link.NavigateUrl = elem.Url; } private void BindSnippet(object source, EventArgs e) { Label snippet = (Label)source; LiveSearchService.Result elem = GetResultElement(snippet.NamingContainer); snippet.Text = elem.Description; } private void BindUrl(object source, EventArgs e) { Label url = (Label)source; LiveSearchService.Result elem = GetResultElement(url.NamingContainer); url.Text = elem.Url; } } } Cameron_865-2C13.fm Page 669 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 670 CHAPTER 13 ■ PACKAGING AND DEPLOYMENT Toolbox Image Icons After the controls are built, we can ensure a nice experience in the Toolbox used by Visual Studio web forms when in design mode by adding Toolbox image icons. This task is accomplished by putting a 16×16 bitmap file with the same name as the control and settings its Build Action property in the Visual Studio Properties window to Embedded Resource. Once this is complete and the DLL representing the control library is built, you can add the controls in the DLL into the Toolbox via the Visual Studio Tools menu’s Customize Toolbox dialog box, as shown in Figure 13-2. Figure 13-2. The Customize Toolbox dialog box The end result of adding the new controls is a Toolbox tab like the one shown in Figure 13-3. Figure 13-3. Toolbox icons for the Live Search controls library Cameron_865-2C13.fm Page 670 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 13 ■ PACKAGING AND DEPLOYMENT 671 In the next section, we put all the Live Search controls to work in a couple of demonstra- tion web forms. Testing the Live Search Controls The default look and feel of the Live Search controls displays if you drag and drop the controls onto a web form. Both the Search and Result controls require little configuration effort to provide a pleasing display in the Visual Studio Control Designer, as shown in Figure 13-4. Figure 13-4. The stock Search and Result controls in the Visual Studio Control Designer The Default Look and Feel The same default look and feel shown at design time is generated in the browser. Figure 13-5 shows the initial page with just the Search control rendering its output. Type in a search query, and you can see the results in Figure 13-6. ■Tip Remember to replace the settings in the web.config file of the web application project that relate to the Live Search Application ID as well as add the license file in order to get the samples shown in Figures 13-5 and 13-6 working. Cameron_865-2C13.fm Page 671 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 672 CHAPTER 13 ■ PACKAGING AND DEPLOYMENT Figure 13-5. A blank Live Search search page Figure 13-6. The result of a Live Search search query Listings 13-6 and 13-7 contain the web form and the code-behind file, respectively. Cameron_865-2C13.fm Page 672 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 13 ■ PACKAGING AND DEPLOYMENT 673 Listing 13-6. The LiveSearchSearch.aspx Page File <%@ Page Language="C#" MasterPageFile="~/MasterPage/ControlsBook2MasterPage.Master" AutoEventWireup="true" CodeBehind="LiveSearch.aspx.cs" Inherits="ControlsBook2Web.Ch12.LiveSearch" Title="Live Search Demo" %> <%@ Register TagPrefix="ApressLive" Namespace="ControlsBook2Lib.CH12.LiveSearchControls" Assembly="ControlsBook2Lib.CH12.LiveSearchControls" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadSection" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ChapterNumAndTitle" runat="server"> <asp:Label ID="ChapterNumberLabel" runat="server" Width="14px">12</asp:Label>&nbsp;&nbsp;<asp:Label ID="ChapterTitleLabel" runat="server" Width="360px"> Building a Complex Control</asp:Label> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="PrimaryContent" runat="server"> <h3> Ch12 Live Search</h3> <ApressLive:Search ID="search" runat="server" RedirectToLiveSearch="false" Width="426px" ResultControl="Result"></ApressLive:Search> <br /> <br /> <ApressLive:Result ID="Result" runat="server" PagerStyle="TextWithDHTML" PagerBarRange="4" PageSize="8" PageNumber="1" PagerLinkStyle="Text"> <HeaderStyle Font-Bold="True" ForeColor="Blue" BorderColor="Blue"></HeaderStyle> <StatusStyle Font-Bold="True" ForeColor="Blue"></StatusStyle> </ApressLive:Result> </asp:Content> Listing 13-7. The LiveSearchSearch.cs Code-Behind Class File using System; namespace ControlsBook2Web.Ch12 { public partial class LiveSearch : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } } } Cameron_865-2C13.fm Page 673 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 674 CHAPTER 13 ■ PACKAGING AND DEPLOYMENT Customizing the Live Search Controls’ Appearance The Live Search controls we produced provide extensive support for customization through styles, templates, and data-binding overrides. The next web form demonstration takes advantage of all three features. The CustomLiveSearch web form implements its own version of ItemTemplate, AlternatingItemTemplate, and StatusTemplate to show a numbered list of the search results on the left side and a different color for each alternating row. The work of keeping the item index is performed in the code-behind class file that links up to events exposed by the Search and Result controls. It resets the resultIndex variable when either Search or Result raises the LiveSearchSearched event. Then, on each ItemCreated event raised by the Result control, it increments its counter and inserts the number at the head of the ResultItem content for each row as part of the user interface. Figure 13-7 shows the result. Figure 13-7. The result of the LiveSearch.aspx search query Listings 13-8 and 13-9 show the web form and the code-behind file, respectively. Cameron_865-2C13.fm Page 674 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 13 ■ PACKAGING AND DEPLOYMENT 675 Listing 13-8. The CustomLiveSearch.aspx Page File <%@ Page Language="C#" MasterPageFile="~/MasterPage/ControlsBook2MasterPage.Master" AutoEventWireup="true" CodeBehind="CustomLiveSearch.aspx.cs" Inherits="ControlsBook2Web.Ch12.CustomLiveSearch" Title="Custom live Search Demo" %> <%@ Register TagPrefix="ApressLive" Namespace="ControlsBook2Lib.CH12.LiveSearchControls" Assembly="ControlsBook2Lib.CH12.LiveSearchControls" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadSection" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ChapterNumAndTitle" runat="server"> <asp:Label ID="ChapterNumberLabel" runat="server" Width="14px">12</asp:Label>&nbsp;&nbsp;<asp:Label ID="ChapterTitleLabel" runat="server" Width="360px"> Building a Complex Control</asp:Label> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="PrimaryContent" runat="server"> <h3> Ch12 Custom Live Search</h3> <ApressLive:Search ID="search" runat="server" ResultControl="Result" RedirectToLiveSearch="false" OnLiveSearchSearched="search_LiveSearchSearched" onlivesearchsearchedeventhandler="search_LiveSearchSearched"> </ApressLive:Search> <br /> <br /> <ApressLive:Result ID="Result" runat="server" DisplayPager="true" OnItemCreated="Result_ItemCreated" OnLiveSearchSearched="Result_LiveSearchSearched" onlivesearchsearchedeventhandler="Result_LiveSearchSearched" onresultitemeventhandler="Result_ItemCreated" PagerLinkStyle="Text"> <ItemTemplate> <a href="<%# ((LiveSearchService.Result)Container.DataItem).Url %>"> <%# ((LiveSearchService.Result)Container.DataItem).Url%> </a> <br /> <%# ((LiveSearchService.Result)Container.DataItem).Description%> <br /> </ItemTemplate> <ItemStyle Font-Size="X-Small" Font-Names="Arial" Font-Italic="True"> </ItemStyle> <FooterStyle Font-Italic="True" Font-Names="Arial" Font-Size="X-Small"> </FooterStyle> <PagerStyle Font-Bold="True" ForeColor="Red"></PagerStyle> <AlternatingItemStyle Font-Italic="True" Font-Names="Arial" Font-Size="X-Small" /> Cameron_865-2C13.fm Page 675 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 676 CHAPTER 13 ■ PACKAGING AND DEPLOYMENT <HeaderTemplate> Search Results </HeaderTemplate> <StatusStyle Font-Bold="True" ForeColor="#CC9900"></StatusStyle> <HeaderStyle Font-Names="Arial" ForeColor="#339933" /> <AlternatingItemTemplate> <a href=" <%# ((LiveSearchService.Result)Container.DataItem).Url %>"> <%#((LiveSearchService.Result)Container.DataItem).Url %> </a> <br /> <%# ((LiveSearchService.Result)Container.DataItem).Description%> <br /> </AlternatingItemTemplate> <StatusTemplate> Displaying entries <%# ((Result.PageNumber - 1) * (Result.PageSize)) + 1%> - <%# Result.PageNumber * (Result.PageSize)%> of about <%# Result.TotalResultsCount%>.<br /> </StatusTemplate> <SeparatorTemplate> <hr /> </SeparatorTemplate> </ApressLive:Result> </asp:Content> Listing 13-9. The CustomLiveSearch.cs Code-Behind Class File using System; using System.Web.UI; using ControlsBook2Lib.CH12.LiveSearchControls; namespace ControlsBook2Web.Ch12 { public partial class CustomLiveSearch : System.Web.UI.Page { private int resultIndex; protected void Page_Load(object sender, EventArgs e) { } Cameron_865-2C13.fm Page 676 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 13 ■ PACKAGING AND DEPLOYMENT 677 protected void search_LiveSearchSearched(object sender, LiveSearchSearchedEventArgs lse) { resultIndex = lse.Result.Responses[0].Offset; } protected void Result_LiveSearchSearched(object sender, LiveSearchSearchedEventArgs lse) { resultIndex = lse.Result.Responses[0].Offset; } protected void Result_ItemCreated(object sender, ResultItemEventArgs e) { ResultItem item = e.Item; if (item.ItemType == ResultItemType.Item || item.ItemType == ResultItemType.AlternatingItem) { item.Controls.AddAt(0, new LiteralControl ((((Result.PageNumber - 1) * Result.PageSize) + item.ItemIndex + 1).ToString() + ".")); resultIndex++; } } } } Now that we have demonstrated the fully functioning search and result server controls, in the next section, we discuss how to add licensing support to a custom server control. Licensing Support We ignored two key aspects of the source code for the Search and Result controls to streamline our discussion: globalization and licensing. We start the process by drilling down into licensing. The licensing system that we provide for the Live Search control is based on the licensing framework that is already in place for .NET. Several core classes provide the architecture and base foundation for adding licensing to components in the .NET Framework environment, as shown in Figure 13-8. The primary class is the abstract LicenseProvider class, which ensures that a particular component has the necessary licensing information. To do its job, the LicenseProvider class relies on another abstract base class, the License class, to physically represent the licensing information. LicenseProvider has a key abstract method called GetLicense that validates and returns a License instance if it passes inspection. To link LicenseProvider to a component that needs license validation, the LicenseProviderAttribute attribute is provided to attach at the class level of the component. Once this is done, code is also manually added to the constructor to kick off the license validation process through the Validate method of the LicenseManager class. Cameron_865-2C13.fm Page 677 Thursday, February 21, 2008 2:22 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... appropriate values to both the Search and Result controls to provide the means of accessing the GUID and public key for validation Simpo PDF MergePage 687 Split Unregistered Version - http://www.simpopdf.com Cameron_865-2C13.fm and Thursday, February 21, 2008 2:22 PM C HA PT ER 1 3 ■ P A CK AGI NG A ND D EP LO YM ENT We add LicenseProviderAttribute to link in our custom license provider, RsaLicenseProvider,... Guid.NewGuid().ToString(); byte[] clear = ASCIIEncoding.ASCII.GetBytes(GetLicenseText()); SHA1Managed provSHA1 = new SHA1Managed(); byte[] hash = provSHA1.ComputeHash(clear); RSACryptoServiceProvider provRSA = new RSACryptoServiceProvider(); PublicKey.Text = provRSA.ToXmlString(false); PrivateKey.Text = provRSA.ToXmlString(true); byte[] signature = provRSA.SignHash(hash, CryptoConfig.MapNameToOID("SHA1")); License.Text = GetLicenseText()... ASCIIEncoding.ASCII.GetBytes(guid + "#" + expires + "#"); SHA1Managed provSHA1 = new SHA1Managed(); byte[] hash = provSHA1.ComputeHash(clear); // reload the RSA provider based on the public key only CspParameters paramsCsp = new CspParameters(); paramsCsp.Flags = CspProviderFlags.UseMachineKeyStore; RSACryptoServiceProvider provRSA = new RSACryptoServiceProvider(paramsCsp); provRSA.FromXmlString(publicKey); // verify the... http://www.simpopdf.com Cameron_865-2C13.fm and Thursday, February 21, 2008 2:22 PM 678 CHAPTER 13 ■ PAC KA GIN G AN D DEPLOYMEN T Control LicenseProviderAttribute LicenseManager License LicenseProvider License File Figure 13-8 The NET licensing architecture The NET Framework provides a trivial implementation of the abstract LicenseProvider class named LicFileLicenseProvider that provides a minimal licensing enforcement... the LiveSearchControls project in the Visual Studio Solution Explorer, right-clicking, and selecting the Properties menu item to bring up the project Properties dialog box On the Build tab, look for the “Conditional compilation symbols” constants section Figure 13-12 shows what the dialog box for this build setting looks like Remove LICENSED from the list, and the code between #if and #endif will be ignored... representing specific build of server control type /// public string Guid { get { return guid; } } /// /// Public key representing specific build of server control type /// public string PublicKey { get { return publicKey; } } } } Next, we discuss how to apply licensing to the Search and Result custom server controls Adding Licensing to the Search and Result Controls The RsaLicenseDataAttribute... CspParameters(); paramsCsp.Flags = CspProviderFlags.UseMachineKeyStore; RSACryptoServiceProvider provRSA = new RSACryptoServiceProvider(paramsCsp); provRSA.FromXmlString(publicKey); // verify the signature on the hash byte[] sigBytes = Convert.FromBase64String(signature); bool result = provRSA.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), sigBytes); return result; } } } Globalization and Localization In this... Results Page:&nbsp; The controls that store values inside LocalStrings include the Search control and its button’s Text property, and the Pager control and its Next/Previous buttons and Results text The ResultStatusTemplate template also uses LocalStrings to build its content for search results Culture Types and Localizing Resource Files The LocalStrings.resx resource... to RSACryptoServiceProvider, which is initialized shortly afterward in the code Notice how we use its ToXmlString methods to easily grab the newly generated public and private keys that are created when RSACryptoServiceProvider is initialized in a convenient-to-handle format The SignHash method on RSACryptoServiceProvider creates the digital signature needed to ensure integrity and validate the license... compile process Figure 13-12 Conditional compilation constant for licensing 687 Simpo PDF MergePage 688 Split Unregistered Version - http://www.simpopdf.com Cameron_865-2C13.fm and Thursday, February 21, 2008 2:22 PM 688 CHAPTER 13 ■ PAC KA GIN G AN D DEPLOYMEN T The RsaLicenseProvider Class The heart of the validation process exists inside the RsaLicenseProvider class It inherits from the base LicenseProvider . forms. Testing the Live Search Controls The default look and feel of the Live Search controls displays if you drag and drop the controls onto a web form. Both the Search and Result controls require little. Search and Result custom server controls. Adding Licensing to the Search and Result Controls The RsaLicenseDataAttribute attribute is applied with the appropriate values to both the Search and. ASCIIEncoding.ASCII.GetBytes(GetLicenseText()); SHA1Managed provSHA1 = new SHA1Managed(); byte[] hash = provSHA1.ComputeHash(clear); RSACryptoServiceProvider provRSA = new RSACryptoServiceProvider(); PublicKey.Text = provRSA.ToXmlString(false);

Ngày đăng: 12/08/2014, 23:20

TỪ KHÓA LIÊN QUAN