11 Asp.net Session16

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 11 Asp.net Session16 as PDF for free.

More details

  • Words: 1,883
  • Pages: 21
Developing Web Applications Using ASP.NET Objectives 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

Ver. 1.0

Slide 1 of 21

Developing Web Applications Using ASP.NET Dynamic Control Creation 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. Ver. 1.0

Slide 2 of 21

Developing Web Applications Using ASP.NET Dynamic Control Creation (Contd.) 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); Ver. 1.0

Slide 3 of 21

Developing Web Applications Using ASP.NET Dynamic Control Creation (Contd.) 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.

Ver. 1.0

Slide 4 of 21

Developing Web Applications Using ASP.NET Dynamic Control Creation (Contd.) 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: DataList Repeater DataGrid

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 Ver. 1.0

Slide 5 of 21

Developing Web Applications Using ASP.NET Localization and Globalization 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 Ver. 1.0

Slide 6 of 21

Developing Web Applications Using ASP.NET Localization and Globalization (Contd.) 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.

Ver. 1.0

Slide 7 of 21

Developing Web Applications Using ASP.NET Localization and Globalization (Contd.) 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.

Ver. 1.0

Slide 8 of 21

Developing Web Applications Using ASP.NET Dynamic Master Pages 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”%> 1. Access the methods and properties in the master page from the content page: myMasterPage masterPage= (myMasterPage)this.Master; CompanyName.Text = masterPage.CompanyName;

Ver. 1.0

Slide 9 of 21

Developing Web Applications Using ASP.NET Dynamic Master Pages (Contd.) 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”%> <%@ MasterType virtualPath=“~/MasterPage.master”%>

Ver. 1.0

Slide 10 of 21

Developing Web Applications Using ASP.NET Dynamic Master Pages (Contd.) 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); }

Ver. 1.0

Slide 11 of 21

Developing Web Applications Using ASP.NET Dynamic Master Pages (Contd.) 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.

Ver. 1.0

Slide 12 of 21

Developing Web Applications Using ASP.NET Dynamic Web Configuration 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 site ASP.NET Microsoft Management Console (MMC) snap-in ASP.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.

Ver. 1.0

Slide 13 of 21

Developing Web Applications Using ASP.NET Dynamic Web Configuration (Contd.) 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.

Ver. 1.0

Slide 14 of 21

Developing Web Applications Using ASP.NET Dynamic Web Configuration (Contd.) 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(“~”);

Ver. 1.0

Slide 15 of 21

Developing Web Applications Using ASP.NET Dynamic Web Configuration (Contd.) 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 properties Modify configuration values Use 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(); Ver. 1.0

Slide 16 of 21

Developing Web Applications Using ASP.NET Demo: Building Dynamic Web Applications 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.

Ver. 1.0

Slide 17 of 21

Developing Web Applications Using ASP.NET Demo: Building Dynamic Web Applications (Contd.) Solution: You need to perform following tasks: 1. Add and Configure Controls Dynamically a. b. c. d. e. f.

Ver. 1.0

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

Slide 18 of 21

Developing Web Applications Using ASP.NET Demo: Building Dynamic Web Applications (Contd.) 2. Apply Master Pages Dynamically a. 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.

2. Add Localization Features a. b. c. d.

Add a Welcome label to the TopLevel.master page. Generate resource for the TopLevel.master page. Set the UICulture for the Default.aspx page. Configure the languages for the Internet Explorer and test the Web application.

2. 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.

Ver. 1.0

Slide 19 of 21

Developing Web Applications Using ASP.NET Summary 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.

Ver. 1.0

Slide 20 of 21

Developing Web Applications Using ASP.NET Summary (Contd.) 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.

Ver. 1.0

Slide 21 of 21

Related Documents

11 Asp.net Session16
June 2020 0
Aspnet
June 2020 25
Session16 Draft
November 2019 5
Session16 Combined
April 2020 6
Aspnet-statemgmt
October 2019 34
Vnamese Aspnet
October 2019 37