1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

Styles, HTML 3.2, and Down-Level Browsers

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.78 MB, 77 trang )


Simpo PDF MergePage 668 Split Unregistered Version - http://www.simpopdf.com

Cameron_865-2C13.fm and Thursday, February 21, 2008 2:22 PM



668



CHAPTER 13 ■ PAC KA GIN G AN D DEPLOYMEN T



// Searched for:

section.Append(

String.Format(

rm.GetString("ResultStatusTemplate.SearchFor"),

resultControl.Query));

section.Append("
");

// Result - of about

// 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("  ");

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

{

///



/// Default ResultItemTemplate implementation used by a

/// Stock LiveSearch Lib Result control without a ItemTemplate

///


public class ResultItemTemplate : ITemplate

{

///

/// Method puts template controls into container control

///


/// Outside control container to template items



Simpo PDF MergePage 669 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



public void InstantiateIn(Control container)

{

HyperLink link = new HyperLink();

link.DataBinding += new EventHandler(BindLink);

container.Controls.Add(link);

container.Controls.Add(new LiteralControl("
"));

Label snippet = new Label();

snippet.DataBinding += new EventHandler(BindSnippet);

container.Controls.Add(snippet);

container.Controls.Add(new LiteralControl("
"));

Label url = new Label();

url.DataBinding += new EventHandler(BindUrl);

container.Controls.Add(url);

container.Controls.Add(new LiteralControl("
"));

container.Controls.Add(new LiteralControl("
"));

}

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;

}

}

}



669



Simpo PDF MergePage 670 Split Unregistered Version - http://www.simpopdf.com

Cameron_865-2C13.fm and Thursday, February 21, 2008 2:22 PM



670



CHAPTER 13 ■ PAC KA GIN G AN D DEPLOYMEN T



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



Simpo PDF MergePage 671 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



In the next section, we put all the Live Search controls to work in a couple of demonstration 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.



671



Simpo PDF MergePage 672 Split Unregistered Version - http://www.simpopdf.com

Cameron_865-2C13.fm and Thursday, February 21, 2008 2:22 PM



672



CHAPTER 13 ■ PAC KA GIN G AN D DEPLOYMEN T



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.



Simpo PDF MergePage 673 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



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" %>








Width="14px">12
  
ID="ChapterTitleLabel" runat="server" Width="360px">

Building a Complex Control







Ch12 Live Search




Width="426px"

ResultControl="Result">









PagerBarRange="4"

PageSize="8" PageNumber="1" PagerLinkStyle="Text">









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)

{

}

}

}



673



Simpo PDF MergePage 674 Split Unregistered Version - http://www.simpopdf.com

Cameron_865-2C13.fm and Thursday, February 21, 2008 2:22 PM



674



CHAPTER 13 ■ PAC KA GIN G AN D DEPLOYMEN T



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.



Simpo PDF MergePage 675 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



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" %>








Width="14px">12
  
ID="ChapterTitleLabel" runat="server" Width="360px">

Building a Complex Control







Ch12 Custom Live Search




RedirectToLiveSearch="false"

OnLiveSearchSearched="search_LiveSearchSearched"

onlivesearchsearchedeventhandler="search_LiveSearchSearched">










OnItemCreated="Result_ItemCreated"

OnLiveSearchSearched="Result_LiveSearchSearched"

onlivesearchsearchedeventhandler="Result_LiveSearchSearched"

onresultitemeventhandler="Result_ItemCreated" PagerLinkStyle="Text">





<%# ((LiveSearchService.Result)Container.DataItem).Url%>






<%# ((LiveSearchService.Result)Container.DataItem).Description%>

















Font-Size="X-Small" />



675



Simpo PDF MergePage 676 Split Unregistered Version - http://www.simpopdf.com

Cameron_865-2C13.fm and Thursday, February 21, 2008 2:22 PM



676



CHAPTER 13 ■ PAC KA GIN G AN D DEPLOYMEN T





Search Results













Simpo PDF MergePage 677 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



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.



677



Simpo PDF MergePage 678 Split Unregistered Version - 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 check.

The only check it performs is for the presence of a .lic file with a text string in it, but it serves

as a good starting point. We improve on this simple scheme in the following sections by writing

a custom implementation of LicenseProvider and other related licensing classes using more

advanced cryptographic techniques.



The RsaLicense License

The License class from the System.ComponentModel namespace represents the information

used to direct the behavior of the license validation system and control feature enablement.

For our licensing system, we rely on the following information stored in our custom license class:

• System.Type value of the control to which the license applies

• Globally unique identifier (GUID) for the particular build of the control for licensing

purposes

• Expiration date for the license

• Full key string from the license file

The resulting class is simple because it is primarily a structure for information transport.

Listing 13-10 shows the full code for our RsaLicense class.



Xem Thêm
Tải bản đầy đủ (.pdf) (77 trang)

×