2310 b 08

342 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
342
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

2310 b 08

  1. 1. Mo dule 8: Creating User Controls
  2. 2. Overview <ul><li>Adding User Controls to an ASP.NET Web Form </li></ul><ul><li>Creating User Controls </li></ul>
  3. 3. Lesson: Adding User Controls to an ASP.NET Web Form <ul><li>What is a User Control? </li></ul><ul><li>Why Use User Controls? </li></ul><ul><li>Practice: Listing User Controls </li></ul><ul><li>Adding a User Control </li></ul><ul><li>Demonstration: Creating a User Control </li></ul>
  4. 4. What is a User Control? <ul><li>User controls simplify the reuse of code and UI components within a Web application </li></ul><ul><li>A user control is a user-defined Web server control with an .ascx extension </li></ul><ul><li>Contains HTML, but not the <HTML>, <BODY>, or <FORM> tags </li></ul><ul><li>or </li></ul><ul><li>Contains code to handle its own events </li></ul><%@ Control Language=&quot;vb&quot; %> <%@ Control Language=&quot;c#&quot; %>
  5. 5. Why Use User Controls? <ul><li>Reuse user interface and code </li></ul>Page3.aspx Application A Application B Page2.aspx Control1.ascx Page1.aspx
  6. 6. Practice: Listing User Controls <ul><li>Students will: </li></ul><ul><ul><li>List examples of when it is appropriate to use user controls </li></ul></ul><ul><li>Time: 5 minutes </li></ul>
  7. 7. Adding a User Control <ul><li>Use the @ Register directive to include a user control in an ASP.NET Page </li></ul><ul><li>Insert the user control in a Web Form </li></ul><ul><li>Use Get and Set properties of the user control </li></ul><ul><li>or </li></ul><%@ Register TagPrefix=&quot;demo&quot; TagName=&quot;validNum&quot; Src=&quot;numberbox.ascx&quot; %> <demo:validNum id=&quot;num1&quot; runat=&quot;server&quot;/> num1.pNum = 5 'uses Set x = num1.pNum 'uses Get num1.pNum = 5; //uses Set x = num1.pNum; //uses Get
  8. 8. Demonstration: Creating a User Control <ul><li>Create a new user control </li></ul><ul><li>Create a property </li></ul>
  9. 9. Lesson: Creating User Controls <ul><li>Creating a User Control </li></ul><ul><li>Demonstration: Using a User Control </li></ul>
  10. 10. Creating a User Control <ul><li>Two methods for user control creation: </li></ul><ul><ul><li>Create a new user control using Visual Studio .NET </li></ul></ul><ul><ul><li>Convert an existing ASP.NET page to a user control </li></ul></ul><ul><li>Host page interacts with the control using properties </li></ul><ul><li>Host page should use flow layout </li></ul>Public Property pNum() As Integer Get Return Convert.ToInt32(txtNum.Text) End Get Set (ByVal value As Integer) txtNum.Text = CStr(value) End Set End Property public int pNum { get { return Convert.ToInt32(txtNum.Text); } set { txtNum.Text = Convert.ToString(value); } }
  11. 11. Demonstration: Using a User Control <ul><li>Insert a user control on a page </li></ul><ul><li>Use the user controls in the code-behind page </li></ul>
  12. 12. Review <ul><li>Adding User Controls to an ASP.NET Web Form </li></ul><ul><li>Creating User Controls </li></ul>
  13. 13. Lab 8: Creating User Controls Medical Medical.aspx Benefits Home Page Default.aspx Life Insurance Life.aspx Retirement Retirement.aspx Dental Dental.aspx Dentists Doctors Doctors.aspx Doctors Logon Page Login.aspx Registration Register.aspx Coho Winery Prospectus Prospectus.aspx XML Web Service dentalService1.asmx Page Header Header.ascx ASPState tempdb Lab Web Application User Control namedate.ascx Menu Component Class1.vb or Class1.cs XML Files Web. config

×