11 asp.net session16

21
Developing Web Applications Using ASP.NET In this session, you will learn to: Explain dynamic control creation in Microsoft ASP.NET 2.0 Add and configure controls dynamically Explain how to incorporate globalization and localization features into Web applications Add localization features to a Web application Describe when and how to implement dynamic master pages Describe dynamic Web configuration scenarios Apply master pages dynamically Dynamically configure Web applications Objectives

Upload: vivek-chan

Post on 14-Apr-2017

144 views

Category:

Education


1 download

TRANSCRIPT

Page 1: 11 asp.net session16

Developing Web Applications Using ASP.NET

In this session, you will learn to:Explain dynamic control creation in Microsoft ASP.NET 2.0Add and configure controls dynamicallyExplain how to incorporate globalization and localization features into Web applicationsAdd localization features to a Web applicationDescribe when and how to implement dynamic master pagesDescribe dynamic Web configuration scenariosApply master pages dynamicallyDynamically configure Web applications

Objectives

Page 2: 11 asp.net session16

Developing Web Applications Using ASP.NET

Appearance of some controls on the Web page depends on run-time conditions. Code can be written to add, remove, hide, or show controls in response to conditions.Each ASP.NET page has a Controls collection.Container controls, such as Panel and PlaceHolder, also have a Controls collection.Controls can be added to a page or container controls by using the Controls.Add method.Location of the controls on a page cannot be specified by using the Controls.Add method.The layout of the page at runtime can be controlled by adding the controls to a Panel or PlaceHolder control instead of adding them to a page.

Dynamic Control Creation

Page 3: 11 asp.net session16

Developing Web Applications Using ASP.NET

To add a control to a Web forms page programmatically, you need to:

Create an instance of the control and set its properties:Label myLabel = new Label();mylabel.Text = “Simple label”;

Add the control to the page. This is typically done during the Page’s load stage:

private void Page_Load (object sender, System.EventArgs e)

{this.Controls.Add(myLabel);

}

A new control can added to the Controls collection of a container already on the page as:

Panel1.Controls.Add(myLabel);

Dynamic Control Creation (Contd.)

Page 4: 11 asp.net session16

Developing Web Applications Using ASP.NET

When using Tables and TreeViews, you may require to add subcontrols at run time.Dynamic control creation is necessary when the result must be displayed from a user query. In addition to adding controls at run time, you can also remove, hide, or show controls at run time.

Dynamic Control Creation (Contd.)

Page 5: 11 asp.net session16

Developing Web Applications Using ASP.NET

You can create templates dynamically in code.Dynamically created templates are used when you do not know until run time which templates to use and what text or controls to include in them.You can create templates in code for all controls that use templates. These controls include:

DataListRepeaterDataGrid

To create a dynamic template, you need to:1. Create a template class that implements the ITemplate

interface and defines the InstantiateIn method of the ITemplate interface.

2. Instantiate the dynamic template

Dynamic Control Creation (Contd.)

Page 6: 11 asp.net session16

Developing Web Applications Using ASP.NET

Web pages can be served automatically in the language that the user prefers by using ASP.NET.Preferred language passed by the browser to the server is used to determine how the page should be localized.Microsoft .NET Framework applications use resource files to store data to be displayed for different controls.For each culture, a different resource file can be created.An ASP.NET control can be configured to check a resource file while the page is rendering. If a control finds a resource file that matches the culture of the user and if a value is specified for the control in the resource file:

The value will be substituted for the value of the control on the page

Localization and Globalization

Page 7: 11 asp.net session16

Developing Web Applications Using ASP.NET

Implicit Localization:This type of localization enables you to specify that a control should read the values of its properties from a resource file.If properties are found in the resource file for the control at the run time, they are used automatically.Every ASP.NET page can have a default resource file and additional resource files for each supported language and culture.Resources files are stored in App_LocalResources folder.For implicit localization to work, automatic culture determination must be enabled for the page by adding uiculture=“auto” to the <%@page%> directive.

Localization and Globalization (Contd.)

Page 8: 11 asp.net session16

Developing Web Applications Using ASP.NET

Explicit Localization:This type of localization enables you to use an expression to direct a control to take the value of a specific property from a specific resource file.A single set of resource files can be used for many pages in the application.Naming convention for resource files in explicit localization is similar to the naming convention in implicit localization. However, the names do not have to begin with the file name of the page.Resource files are usually placed in App_GlobalResources directory.

Localization and Globalization (Contd.)

Page 9: 11 asp.net session16

Developing Web Applications Using ASP.NET

A master page can be made dynamic just as ordinary ASP.NET pages.Code in a content page can alter the properties or controls specified by the master page.Public methods and properties on the master page can be accessed from code in the content page. To do this:1. Assign a class name to the master page in the <@master>

directive: <@Master ClassName=“myMasterPage”%>

2. Access the methods and properties in the master page from the content page: myMasterPage masterPage= (myMasterPage)this.Master;CompanyName.Text = masterPage.CompanyName;

Dynamic Master Pages

Page 10: 11 asp.net session16

Developing Web Applications Using ASP.NET

An object returned by Page.Master must be explicitly type casted to the type specified for the master page variable.The type casting requirement can be removed in the by using the <%@ MasterType%> directive in the content page:<%@ page masterPageFile=“~/MasterPage.master”%><%@ MasterTypevirtualPath=“~/MasterPage.master”%>

Dynamic Master Pages (Contd.)

Page 11: 11 asp.net session16

Developing Web Applications Using ASP.NET

Controls on a master page are protected. Therefore, you cannot refer to them directly from a content page.To access the methods and properties of a master page from code in a content page, you can use the FindControl method:Label labelOnMasterPage = (Label) (this.Master.FindControl (“masterPageLabel”));if(labelOnMasterPage != null){

Response.Write(“Master Page label = “ + labelOnMasterPage.Text);

}

Dynamic Master Pages (Contd.)

Page 12: 11 asp.net session16

Developing Web Applications Using ASP.NET

A content page is usually assigned a master page at design time by using the <%@ Page masterPageFile= “xxx”%> directive.A content page can refer a different master page at run time by using the Page.MasterPageFile property.MasterPageFile property is usually set in the Page_PreInit event handler.

Dynamic Master Pages (Contd.)

Page 13: 11 asp.net session16

Developing Web Applications Using ASP.NET

ASP.NET application can be configured by using:Machine.config file, the root Web.config file for the server, or the Web.config files on the Web siteASP.NET Microsoft Management Console (MMC) snap-inASP.NET Web Site Administration tool

In some cases, the preceding tools might not provide the required combination of configuration capabilities.ASP.NET Configuration Application Programming Interface (API) can be used to write the code to configure Web applications without editing the XML configuration files.

Dynamic Web Configuration

Page 14: 11 asp.net session16

Developing Web Applications Using ASP.NET

Generalized administration tools provide access to all the properties of a site. Therefore, these tools provide administrators full control over a site.You can create a customized administration tool that allows administrators to access only the configuration settings that they require.The interface of the customized administration tool can be designed according to the preferences and job responsibilities of the user.

Dynamic Web Configuration (Contd.)

Page 15: 11 asp.net session16

Developing Web Applications Using ASP.NET

To build a configuration tool, you first need to open the configuration file.Configuration API is located in System.Configuration and System.Web.Configuration namespaces.To access machine.config file, the OpenMachineConfiguration method can be used.To access Web.config file OpenWebConfiguration method can be used, as follows:Configuration config; Config = System.Web.Configuration. WebConfigurationManager. OpenWebConfiguration(“~”);

Dynamic Web Configuration (Contd.)

Page 16: 11 asp.net session16

Developing Web Applications Using ASP.NET

After a configuration file has been opened, you can perform the following operations on the file:

Query the various sections of the file and change their propertiesModify configuration valuesUse Save method to write changes back to the configuration file

The following example shows how to enable the debug feature by manipulating a configuration file:

System.Web.Configuration.CompilationSection compilation; compilation = config.GetSection(“system.web/compilation”)as System.Web.Configuration.CompilationSection; comilation.Debug = true; compilation.Save();

Dynamic Web Configuration (Contd.)

Page 17: 11 asp.net session16

Developing Web Applications Using ASP.NET

Problem Statement:You are a developer in the Adventure Works organization, a fictitious bicycle manufacturer. You have been asked to assist in the development of the Business-to-Consumer (B2C) Web application and a related Business-to-Employee (B2E) extranet portal.Decisions on the design of the application have already been made. You have been asked to carry out a number of specific tasks in order to implement various elements of this design. As part of the first phase of the B2C development, you have been asked to add various dynamic features to the Web application.

Demo: Building Dynamic Web Applications

Page 18: 11 asp.net session16

Developing Web Applications Using ASP.NET

Solution:You need to perform following tasks:

1. Add and Configure Controls Dynamically a. Open the Adventure Works Web site.b. Add code to determine the number of items in the shopping cart.c. Add code for creating and configuring Web server controls at run time.d. Add an event handler method for a Button Object.e. Add code for creating a control template.f. Add code to apply the template at run time.

Demo: Building Dynamic Web Applications (Contd.)

Page 19: 11 asp.net session16

Developing Web Applications Using ASP.NET

2. Apply Master Pages Dynamicallya. Determine run-time conditions in the Page_PreInit method.b. Apply a different master page if the shopping cart is empty.c. Test the Web application.

3. Add Localization Featuresa. Add a Welcome label to the TopLevel.master page.b. Generate resource for the TopLevel.master page.c. Set the UICulture for the Default.aspx page.d. Configure the languages for the Internet Explorer and test the Web

application.4. Configure Web Application Dynamically

a. Retrieve configuration settings at run time.b. Modify configuration settings at run time.c. Configure and test the Web application.

Demo: Building Dynamic Web Applications (Contd.)

Page 20: 11 asp.net session16

Developing Web Applications Using ASP.NET

In this session, you learned that:Appearance of some controls on the Web page depends on run-time conditions. Code can be written to add, remove, hide, or show controls in response to conditions.Resource files used by Microsoft .Net applications are useful for handling cultural differences.In implicit localization, specification can be given to a control to read the values of its properties from a resource file.In explicit localization, an expression can be used to direct a control to take the value of a specific property from a specific resource file. Public methods and properties on the master page can be called from code in the content page.

Summary

Page 21: 11 asp.net session16

Developing Web Applications Using ASP.NET

Controls on the master page cannot be referred directly from the content page. The FindControl method must be used for this reference.The Page.MasterPageFile property can be used by content page to refer a master page at run time.The ASP.NET Configuration API can be used to write code to configure the Web application without editing the XML configuration file.

Summary (Contd.)