Chapter 8 User Controls.

Slides:



Advertisements
Similar presentations
Unit 02. ASP.NET Introduction HTML & Server controls Postbacks Page Lifecycle.
Advertisements

Master Pages, User Controls, Site Maps, Localization Svetlin Nakov Telerik Corporation
ASP.NET User Controls Make your own controls by grouping other controls 1ASP.NET User Controls.
Chapter 3 – Designing your web pages Dr. Stephanos Mavromoustakos.
11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
Asp.NET Core Server Controls. Slide 2 Lecture Overview Understanding the types of ASP.NET controls HTML controls ASP.NET (Web) controls.
Web Development in Microsoft Visual Studio Slide 2 Lecture Overview Introduce Visual Studio 2013 Create a first ASP.NET application.
11 ASP.NET Controls Beginning ASP.NET 4.0 in C# 2010 Chapter 6.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
Course Textbook: Build Your Own ASP.Net Website: Chapter 2
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Overview of Previous Lesson(s) Over View  Server controls are small building blocks of the graphical user interface, which includes  Text boxes  Buttons.
With your friendly Web Developer, Chris.. Terminology  HTML - > Hypertext Markup Language  CSS -> Cascading Style Sheet  open tag  close tag  HTTP->Hypertext.
.Net is a collection of libraries, templates and services designed to make programming applications of all kinds, easier, more flexible (multi platform),
February 16, Aaron Cuffman Andy Nagle Adam Schultz Web Site.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Beginning Web Site Development Module 1 – Dynamic Web Site Development Fundamentals of building dynamic Web sites with ASP.NET 2.0 and C# Version.
IT533 Lectures Session Management in ASP.NET. Session Tracking 2 Personalization Personalization makes it possible for e-businesses to communicate effectively.
ASP.NET Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours Digital.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
Chapter 4 – Working with ASP.NET Controls Dr. Stephanos Mavromoustakos.
ASP.NET Controls. Slide 2 Lecture Overview Identify the types of controls supported by ASP.NET and the differences between them.
CSCI 6962: Server-side Design and Programming Introduction to Active Server Pages.
ASP.NET.. ASP.NET Environment ASP.NET is Microsoft's programming framework that enables the development of Web applications and services. It is an easy.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
Tracing 1www.tech.findforinfo.com. Contents Why Tracing Why Tracing Tracing in ASP.NET Tracing in ASP.NET Page Level tracing Page Level tracing Application.
1 CS 3870/CS 5870 Note04 Session Variables and Post Back.
Web User Controls This presentation will cover the basics of defining, creating and using a web user control. Presented to Twin Cities.NET user group By.
Things like Textboxes, Lables, ‘n’at. ASPX page is not HTML Controls are rendered into markup that a browser can understand Some controls are rendered.
Module 1: Working with ASP.NET. Overview Introducing ASP.NET Creating Web Forms Adding ASP.NET Code to a Page Handling Page Events Discussion: ASP vs.
Chapter 4: Working with ASP.NET Server Controls OUTLINE  What ASP.NET Server Controls are  How the ASP.NET run time processes the server controls on.
Christopher M. Pascucci.NET Programming CodeBehind.
CSE 409 – Advanced Internet Technology Today you will learn  Styles  Themes  Master Pages Themes and Master Pages.
The Problem of State. We will look at… Sometimes web development is just plain weird! Internet / World Wide Web Aspects of their operation The role of.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Asp.NET Core Server Controls. Slide 2 Lecture Overview Understanding the types of ASP.NET controls HTML controls ASP.NET (Web) controls.
PostBack  When an initial request for a page (a Web Form) is received by ASP.NET, it locates and loads the requested Web Form (and if necessary compiles.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
STYLING WITH THEMES Active server pages (ASP.NET) 1 Chapter-5.
ASP.NET Architecture Mike Taulty Developer & Platform Group Microsoft Ltd
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Chapter 7 Navigation. Objectives How to move around in your site using server controls and plain HTML How to address pages and other resources like images.
Chapter 2 Creating Website with Web Forms. Objectives Understand the different project types and templates when building ASP.NET web sites. The different.
Creating Consistent Looking Websites
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
Computing with C# and the .NET Framework
ASP.NET Forms.
Working with ASP.NET Server Controls
Section 4.1 Section 4.2 Format HTML tags Identify HTML guidelines
ASP.NET Web Controls.
Working with ASP.NET Master Pages.
ASP MVP Web applications and Razor
Florida Gulf Coast University
XAML User Interface Creation in C#
Unit 27 - Web Server Scripting
Web Development in Microsoft Visual Studio 2013
Anatomy of an ASP.NET Page
Static and Dynamic Web Pages
ASP.NET.
Building ASP.NET Applications 2
Web Development Using ASP .NET
Document Structure & HTML
An Introduction to JavaScript
ASP.NET Imran Rashid CTO at ManiWeber Technologies.
ASP.NET 2.0 Overview Created By: Ajay Bahoriya.
Web Programming and Design
Presentation transcript:

Chapter 8 User Controls

Objectives What user controls are, how they look, and why they are useful How to create user controls How to consume (or use) user controls in your pages How you can improve the usefulness of user controls by adding coding logic to them

User Controls User controls are custom created controls that can essential group other asp controls. They are useful If you have a set of controls that are always used in multiple pages or need to work together. Supposed you want a control with 4 labels and 4 textboxes that have a specific functionality and look and feel. You can create this as one user control and then use the user control on all the pages needed. User controls are very similar to .aspx pages. They have a markup and a code behind.

User Controls User controls have the following similarities to normal ASPX pages: They have a markup section where you can add standard markup, server controls, and plain HTML. They can be created and designed with Visual Studio in Markup, Design, and Split View. They can contain programming logic, either inline or with a Code Behind file. They give you access to page-based information like Request.QueryString. They raise some (but not all) of the events that the Page class raises, including Init, Load, and PreRender. Keys Differences: extension is .ascx and they cannot be called by the browser directly and must be added to a page (.aspx).

Creating User Controls You can Added it the same as any other item Add -> New Item and choose type “Web User Control”

Creating User Controls Notice that the User Control get added to the website structure. It doesn't have a “Page” directive like the normal pages, but instead it has: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs“ Inherits="WebUserControl" %> Treat the User Control as you would an .aspx page. You should have most of the same functionality available…

Adding User Controls to a Content Page or Master Page To add a user control to a content page or master page, you need to take the following steps. 1) Register the control in your page by adding the @Register directive. A typical @ Register directive for a user control looks like this: <%@ Register Src="ControlName.ascx" TagName="ControlName" TagPrefix="uc1" %> 2) Add the tags of the user control to a page and set attributes as needed. The Register directive contains the following attributes:

Example of a User Control Suppose we add a user control to represent a banner. We create our user control like so… <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Banner.ascx.cs" Inherits="Controls_Banner" %> <asp:Panel ID="VerticalPanel" runat="server"> <a href="http://p2p.wrox.com" target="_blank"> <asp:Image ID="Image1" runat="server" AlternateText="This is a sample banner" ImageUrl="~/Images/Banner120x240.gif" /> </a> </asp:Panel>

Example of a User Control Create a page, master page or use an existing one and switch to design view. From the solution explorer, drag your user control into the desired position. Switch to the markup view once the control has been added and find the @Register directive and make sure it is pointing to the correct file. Notice the user control tag prefix as well. <%@ Register Src="~/Controls/Banner.ascx" TagName="Banner" TagPrefix="uc1" %> When the control is registered, you can add it to the page using the TagPrefix:TagName construct, similar to the way you add standard server controls to a page.

Adding a User Control to a Page Given the @ Register directive for the banner control, you need the following markup to add the control to your page: <uc1:Banner ID="Banner1" runat="server" /> This is the minimum code needed for a user control in a page. Note that the control is defined by a combination of the TagPrefix and the TagName. So suppose this is what you added another user control and declared it as such: <%@ Register Src="~/Controls/MainData.ascx" TagName=“MainData" TagPrefix="uc2" %> Then on the markup, you would add it to the content like this <uc2:Maindata ID=“myUCMainData1” runat=“server” />

Adding a User Control inside a page Our User Control resides in the page (Master page in this case)

Page Rendering of the User Control Even though you now page a user control inside a file, the rendering becomes contents inside a div. Note the Div Id. When user control gets rendered by the ASP.NET engine, it appears on the client side as <div id="Banner1_VerticalPanel"> <a href="http://p2p.wrox.com" target="_blank"> <img id="Banner1_Image1" src="Images/Banner120x240.gif" alt="This is a sample banner" /> </a> </div>

Sitewide Registration of User Controls User controls that are going to be used sitewide can be registered through the web.config file. Once a control is configure site wide, you no longer need the @Register page directive. Within the <pages> tag, create a <controls> tag and create a new tag prefix and source to locate that control <pages theme="Monochrome"> <controls> <add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx" /> </controls> </pages> Make sure you find the instances of the control and change the tag prefix to your new site tag prefix (in his case “Wrox”

Understanding and managing client IDs By design, all elements in an HTML page need to be unique. Giving them an id attribute is not required, but if you do so, you have to make sure they are unique. To avoid conflicts in the final HTML code of the page, ASP.NET ensures that each server-side element gets a unique client id by prefixing them with the name of their naming container. Within a naming container all elements should have unique IDs. Example: If you have an <asp:panel> with id of “VerticalPanel” within a user control that has an id of “Banner1”, then the panel (which gets rendered as a div) will have the following Id: <div id="Banner1_VerticalPanel"> ... </div>

Managing IDs with ClientIDMode Introduced with ASP 4, the ClientIDMode determines how Client Id is rendered. This can be setup in the web.config on the <pages> tag <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>

Adding Logic to your User Controls You can add properties to user controls to make them more useful. For useful properties, you can also use enums in C#. The .NET Framework supplies a nice way to solve this by enabling you to create your own data type in the form of an enumeration. With an enumeration, or enum for short, you assign numbers to human-friendly text strings. You can use class file in the app_code folder to declare these values. public enum Direction { Horizontal = 0, Vertical = 1 }

Adding Logic to your User Controls You can program against a user control like an .aspx page. Example: Adding a property Outside the page load on the user control, add: //Direction is of an enum type that we need to have previously created. public Direction DisplayDirection { get; set; } We can set the DisplayDirection of our Enum type by adding this property to the user control. Now that we have a property we can program against it in the user control…

Adding Logic to our User Control We can add logic in our User Control’s page load event to check on the enum protected void Page_Load(object sender, EventArgs e) { switch (DisplayDirection) case Direction.Horizontal: HorizontalPanel.Visible = true; VerticalPanel.Visible = false; break; case Direction.Vertical: VerticalPanel.Visible = true; HorizontalPanel.Visible = false; } We can pass the “DisplayDirection” variable when defining the user control on the page <Wrox:Banner ID="Banner1" runat="server" DisplayDirection="Horizontal" />

Checking For First load of Page On the page’s code behind, you can use Page.IsPostBack to see if its the first time a page loads. If Page.IsPostBack = false, that is the first time the page has been loaded to the user. If Page.IsPostBack = true the user has initiated an action on the client side that has caused the page to be posted back to the server. //This would only occur the first time a page is loaded if (!Page.IsPostBack) { Banner2.DisplayDirection = Direction.Vertical; }

Implementing a View State Property https://msdn.microsoft.com/en-us/library/ms178198(v=vs.85).aspx We can add values to be stored in the View State and they will persist across sessions. The View State is represent as a Dictionary collection in the .NET Framework that can be accessed programmatically. We add something to the viewstate with the following syntax: ViewState[key] = value; Where key is the identifier and value is what we want to keep between sessions. For example, supposed you want to keep the variable across postbacks for a navigation URL. ViewState["NavigateUrl"] = value;

Implementing a View State Property Be careful with what you add to the ViewState. It increases the page’s footprint on every transfer. Never store large objects such as database records in the ViewState. It is often fast to load the data on the fly. ViewState information is transferred on every request and even though it is serialized, you shouldn’t store any sensitive information.

Recommendation for User Controls Don’t overuse user controls. User controls are great for encapsulating repeating content, but they also make it a little harder to manage your site because code and logic is contained in multiple files. Keep user controls focused on a single task. Don’t create a user control that is able to display five different types of unrelated content with a property that determines what to display. When you create user controls that contain styled markup, don’t hardcode style information like the CssClass for the server controls contained in the user control. Instead, consider creating separate CssClass properties on the user control, which are then used to set the CssClass of your server controls.

Try it out Pg. 275 Pg. 278 Pg. 281 Pg. 286 Pg. 290

Summary In this chapter we covered: Learned what user controls are, how they look, and why they are useful Learned how to create user controls Figured out wow to consume (or use) user controls in your pages Improved the usefulness of user controls by adding coding logic to them