1 / 15

Module 8: Creating User Controls

Module 8: Creating User Controls. Overview. Adding User Controls to an ASP.NET Web Form Creating User Controls. Lesson: Adding User Controls to an ASP.NET Web Form. What is a User Control? Why Use User Controls? Adding a User Control. What is a User Control?.

jalia
Download Presentation

Module 8: Creating User Controls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 8:Creating User Controls

  2. Overview • Adding User Controls to an ASP.NET Web Form • Creating User Controls

  3. Lesson: Adding User Controls to an ASP.NET Web Form • What is a User Control? • Why Use User Controls? • Adding a User Control

  4. What is a User Control? • User controls simplify the reuse of code and UI components within a Web application • A user control is a user-defined Web server control with an .ascx extension • Contains HTML, but not the <HTML>, <BODY>, or <FORM> tags or • Contains code to handle its own events (.ascx.vb or .ascx.cs ) <%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl" %> <%@ Control Language="c#" %>

  5. Control1.ascx Page1.aspx Page2.aspx Why Use User Controls? • Reuse user interface and code Application A Application B Page3.aspx

  6. Why Use User Controls? User controls offers many advantages when developing a Web application: • User controls are self contained User controls provides separate variable namespaces, which means that none of the methods and properties of user control conflict with any existing methods or properties of the hosting page. • User controls can be used more than once within the hosting page, without causing property and method conflicts. • User controls can be written in a different language from the main hosting page. Eg: A user control is written in C# and Web form is written in VB.NET.

  7. Why Use User Controls? • The examples where a user control simplifies Web page development: • Text box to only accept integers in a specific range. The user control would include validation controls. • Data grid control that always connects to the same data source, to display data from a stored procedure or table. • Grouping of text boxes and other controls to accept mailing information ( name, address, state, city, postcode) form user. • Grouping of text boxes and other controls to accept contact information (home telephone number, business telephone number, mobile telephone, e-mail address) form user.

  8. Adding a User Control • Use the @ Register directive to include a user control in an ASP.NET Page • Insert the user control in a Web Form <%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %> <uc1:WebUserControl ID="WebUserControl1" runat="server" />

  9. Adding a User Control • Use Get and Set properties of the user control to expose the values of text box to the host. or num1.pNum = 5 'uses Set x = num1.pNum 'uses Get num1.pNum = 5; //uses Set x = num1.pNum; //uses Get

  10. Lesson: Creating User Controls • Creating a User Control • Demonstration: Using a User Control

  11. Review • Adding User Controls to an ASP.NET Web Form • Creating User Controls

  12. Exercise • List TWO examples where a user control simplifies Web page development. • How do you access the properties of a user control’s UI elements from the host page? • How do you reference a user control from an ASP.NET Web form? • Can you use two different user controls with the same name in the same ASP.NET page? • How can you use a user control in two different Web applications?

  13. 3 lab execises this week.. 1: Data binding against object (use list-bound controls) 2: Using master pages and site navigation 3: Creating and using User Control

  14. List-Bound Controls • What is List-Bound Controls? Def 1 :- The controls that can be populated automatically with the data from a data source Def 2:- The controls that connect to a data source and display the data.

  15. Examples : • -CheckBoxList - Repeater • -DataGrid • -DataList • -DropDownList • -ListBox • -RadioButtonList

More Related