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

ASP.NET 4 Unleased - p 145 ppt

10 86 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 659,33 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

ptg Using SQL Cache Dependencies Using Push SQL Cache Dependencies with Data Caching You can use Push SQL cache dependencies when working with the Cache object.. The page uses a data-bas

Trang 1

CHAPTER 29 Caching Application Pages and Data

</body>

</html>

In Listing 29.47, the SqlDataSource control includes a Selecting event handler Because

this event is raised when the data cannot be retrieved from the cache, you can use this

event to determine when the data is retrieved from the cache or the database server (see

Figure 29.17)

FIGURE 29.17 Using Push SQL cache dependencies with a DataSource control

WARNING

The page in Listing 29.47 connects to a Server database named MyMovies You should

not use Push dependencies with a Local SQL Express database The page uses a

data-base table named Movies, which was created with the following SQL command:

CREATE TABLE Movies

(

Id int IDENTITY NOT NULL,

Title nvarchar(100) NOT NULL,

Director nvarchar(50) NOT NULL,

EntryDate datetime NOT NULL DEFAULT GetDate()

)

Trang 2

ptg Using SQL Cache Dependencies

Using Push SQL Cache Dependencies with Data Caching

You can use Push SQL cache dependencies when working with the Cache object You

represent a Push SQL cache dependency with an instance of the SqlCacheDependency

class

For example, in the Page_Load() handler in Listing 29.48, a DataTable is added to the

cache that represents the contents of the Movies database table The DataTable displays in

a GridView control

LISTING 29.48 PushSQLDataCache.aspx

<%@ Page Language=”C#” Trace=”true” %>

<%@ Import Namespace=”System.Data” %>

<%@ Import Namespace=”System.Data.SqlClient” %>

<%@ Import Namespace=”System.Web.Configuration” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<script runat=”server”>

void Page_Load()

{

DataTable movies = (DataTable)Cache[“Movies”];

if (movies == null)

{

Trace.Warn(“Retrieving data from database”);

string conString =

WebConfigurationManager.ConnectionStrings[“MyMovies”].ConnectionString;

SqlDataAdapter dad =

new SqlDataAdapter(“SELECT Title,Director FROM dbo.Movies”,

➥conString);

SqlCacheDependency sqlDepend = new SqlCacheDependency(dad.SelectCommand);

movies = new DataTable();

dad.Fill(movies);

Cache.Insert(“Movies”, movies, sqlDepend);

}

grdMovies.DataSource = movies;

grdMovies.DataBind();

}

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Push SQL Data Cache</title>

</head>

<body>

<form id=”form1” runat=”server”>

Trang 3

CHAPTER 29 Caching Application Pages and Data

<div>

<asp:GridView

id=”grdMovies”

Runat=”server” />

</div>

</form>

</body>

</html>

An instance of the SqlCacheDependency class is created A SqlCommand object is passed to

the constructor for the SqlCacheDependency class If the results of the SqlCommand change,

the DataTable will be dropped automatically from the cache

The order of the commands here is important You need to create the SqlCacheDependency

object before you execute the command If you call the Fill() method before you create

the SqlCacheDependency object, the dependency is ignored

WARNING

The page in Listing 29.48 connects to a Server database named MyMovies You should

not use Push dependencies with a Local SQL Express database The page uses a

data-base table named Movies, which was created with the following SQL command:

CREATE TABLE Movies

(

Id int IDENTITY NOT NULL,

Title nvarchar(100) NOT NULL,

Director nvarchar(50) NOT NULL,

EntryDate datetime NOT NULL DEFAULT GetDate()

)

Summary

In this chapter, you learned how to improve the performance of your ASP.NET

applica-tions by taking advantage of caching In the first part of this chapter, you learned how to

use each of the different types of caching technologies supported by ASP.NET Framework

First, you learned how to use Page Output Caching to cache the entire rendered contents

of a page You learned how to create different cached versions of the same page when the

page is requested with different parameters, headers, and browsers You also learned how

Trang 4

ptg Summary

to remove pages programmatically from the Page Output Cache Finally, we discussed how

you can define Cache Profiles in a web configuration file

Next, you learned how to use Partial Page Caching to apply different caching policies to

different regions in a page You learned how to use post-cache substitution to dynamically

inject content into a page that has been output cached You also learned how to use User

Controls to cache different areas of a page

We also discussed how you can cache data by using the different DataSource controls You

learned how to enable caching when working with the SqlDataSource, ObjectDataSource,

and XmlDataSource controls

Next, you learned how to use the Cache object to cache items programmatically You

learned how to add items to the cache with different expiration policies and

dependen-cies You also learned how to configure the maximum size of the cache in the web

configuration file

Finally, we discussed SQL cache dependencies You learned how to use SQL cache

depen-dencies to reload database data in the cache automatically when the data in the

under-lying database changes You learned how to use both Polling and Push SQL cache

dependencies with Page Output Caching, DataSource Caching, and the Cache object

Trang 5

This page intentionally left blank

Trang 6

CHAPTER 30

Localizing Applications

for Multiple Languages

IN THIS CHAPTER

Setting the Current Culture Using the CultureInfo Class Creating Local Resources Creating Global Resources Using the Localize Control Summary

You can localize an ASP.NET website so that it supports

multiple languages and cultures For example, you might

need to create both an English language and Spanish

language version of the same website

One approach to localization is to simply create multiple

copies of the same website and translate each copy into a

different language This was a common approach when

building ASP Classic (or even ASP.NET 1.1) websites The

problem with this approach is it creates a website

mainte-nance nightmare Whenever you need to make a change to

the website—no matter how simple—you must make the

change in each copy of the website

When building ASP.NET applications, you do not need to

create multiple copies of a website to support multiple

languages Instead, you can take advantage of resource files

A resource file contains language-specific content For

example, one resource file might contain a Spanish version

of all the text in your website, and a second resource file

might contain the Indonesian version of all the text in

your website

In this chapter, you learn how to localize ASP.NET

applica-tions First, you learn how to set the culture of the current

page You learn how to use both the Culture and UICulture

properties You also learn how to detect users’ preferred

languages automatically through their browser settings

Next, local resources are explored A local resource contains

content scoped to a particular file such as an ASP.NET page

You learn how to use both implicit and explicit resource

expressions

Trang 7

CHAPTER 30 Localizing Applications for Multiple Languages

This chapter also examines global resources, which contain content that can be used in

any page within an application For example, you can place the title of your website in a

global resource file

Finally, the ASP.NET Localize control is discussed You learn how to use this control in

your pages to localize big chunks of page text

Setting the Current Culture

Two main properties of the Page class have an effect on localization:

UICulture

Culture

The UICulture property specifies which resource files are loaded for the page The resource

files can contain all the text content of your pages translated into a particular language

You can set this property to any standard culture name This property is discussed in detail

during the discussion of using local and global resources later in this chapter

The Culture property, on the other hand, determines how strings such as dates, numerals,

and currency amounts are formatted It also determines how values are compared and

sorted For example, by modifying the Culture property, you can display dates with

language-specific month names such as January (English), Januar (German), or Enero

(Spanish)

Both the UICulture and Culture properties accept standard culture names for their values

Culture names follow the RFC 1766 and RFC 3066 standards maintained by the Internet

Engineering Task Force (IETF) The IETF website is located at www.IETF.org

Following are some common culture names:

de-DE = German (Germany)

en-US = English (United States)

en-GB = English (United Kingdom)

es-MX = Spanish (Mexico)

id-ID = Indonesian (Indonesia)

zh-CN = Chinese (China)

Each culture name consists of two parts The first part represents the language code, and

the second part represents the country/region code If you specify a culture name and do

not provide a country/region code—for example, en—you have specified something called

a neutral culture If you provide both a language code and a country/region code—for

example, en-US—you have specified something called a specific culture.

Trang 8

Setting the Current Culture

NOTE

You can view the culture names supported by NET Framework by looking up the entry

for the CultureInfo class in the Microsoft NET Framework SDK documentation It’s a

really long list

The Culture property must always be set to a specific culture This makes sense because,

for example, different English speakers use different currency symbols The UICulture

property, on the other hand, can be set to either a neutral or specific culture name Text

written in Canadian English is pretty much the same as text written in U.S English

You can set the UICulture and Culture properties to the same value or different values For

example, if you create an online store, you might want to set the UICulture property to the

value de-DE to display German product descriptions However, you might want to set the

Culture property to the value en-US to display product prices in U.S currency amounts

Setting a Culture Manually

You can set either the UICulture or Culture properties by using the <%@ Page %>

direc-tive For example, the page in Listing 30.1 sets both properties to the value id-ID

(Indonesian)

LISTING 30.1 Bagus.aspx

<%@ Page Language=”C#” Culture=”id-ID” UICulture=”id-ID” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

void Page_Load()

{

lblDate.Text = DateTime.Now.ToString(“D”);

lblPrice.Text = (512.33m).ToString(“c”);

}

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Bagus</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

Today’s date is:

<br />

<asp:Label

Trang 9

ptg CHAPTER 30 Localizing Applications for Multiple Languages

FIGURE 30.1 Displaying a localized date and price

id=”lblDate”

Runat=”server” />

<hr />

The price of the product is:

<br />

<asp:Label

id=”lblPrice”

Runat=”server” />

</div>

</form>

</body>

</html>

The page in Listing 30.1 displays a date and a currency amount Because the Culture

property is set to the value id-ID in the <%@ Page %> directive, both the date and

currency amount are formatted with Indonesian cultural conventions (see Figure 30.1)

The date is displayed like this:

05 March 2010

Trang 10

ptg Setting the Current Culture

The currency amount is displayed as an Indonesian Rupiah amount like this:

Rp512

NOTE

Setting the Culture does not actually convert a currency amount Setting a particular

culture formats only the currency as appropriate for a particular culture If you need to

convert currency amounts, you need to use a Web service: Conversion rates change

minute by minute See, for example, www.xmethods.com

Instead of using the <%@ Page %> directive to set the Culture or UICulture properties, you

can set these properties programmatically For example, the page in Listing 30.2 enables

you to select a particular culture from a drop-down list of cultures (see Figure 30.2)

LISTING 30.2 SelectCulture.aspx

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

protected void btnSelect_Click(object sender, EventArgs e)

{

Culture = ddlCulture.SelectedValue;

FIGURE 30.2 Selecting a culture from a DropDownList control

Ngày đăng: 06/07/2014, 18:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN