1 / 24

User Controls

User Controls. Server controls, authored like pages Enables full encapsulation Supports nested controls Separate code language Easy way to reuse work across multiple pages and applications (page navigation) Replace include files with user controls. User Control Steps.

gudrun
Télécharger la présentation

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. User Controls • Server controls, authored like pages • Enables full encapsulation • Supports nested controls • Separate code language • Easy way to reuse work across multiple pages and applications (page navigation) • Replace include files with user controls

  2. User Control Steps • Use Register directive at top of page to reference name and location of user control <%@ Register TagPrefix="uc1" TagName="nav_ctrl" Src="nav_ctrl.ascx" %> • Add tag within page for location of User Control <uc1:nav_ctrl id="Nav_ctrl1" runat="server">

  3. Create a Simple User Control

  4. Using a User Control <P> <b>Hello World!<b> </P> <%@ Register TagPrefix="MyTag" TagName="Hello" Src="Hello.ascx" %> <HTML><HEAD></HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <P> <MyTag:Hello id="Hello1" Runat="Server"> </MyTag:Hello> </P> </form> </body> </HTML> Rendered as

  5. GetName.ascx <%@ Control Language="vb" AutoEventWireup="false" Codebehind="GetName.ascx.vb" Inherits="usercontrol.GetName" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> Enter your name: <asp:TextBox id="TextBoxName" runat="server"></asp:TextBox>

  6. GetName.ascx.vb Public MustInherit Class GetName Inherits System.Web.UI.UserControl Protected WithEvents TextBoxName As System.Web.UI.WebControls.TextBox Private mName As String Public Property Name() As String Get Return mName End Get Set(ByVal Value As String) mName = Value End Set End Property Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer ' For testing purpose x = 1 End Sub Private Sub TextBoxName_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxName.Load If IsPostBack Then mName = TextBoxName.Text End If End Sub End Class

  7. GetNameText.asp <%@ Page Language="vb" AutoEventWireup="false" Codebehind="GetNameTest.aspx.vb" Inherits="usercontrol.GetNameTest"%> <%@ Register TagPrefix="uc1" TagName="GetName" Src="GetName.ascx" %> <HTML><HEAD><title>GetNameTest</title></HEAD> <body><formid="Form1"method="post"runat="server"> <P> <uc1:GetNameid="GetName1" runat="server"></uc1:GetName> </P> <P><asp:Buttonid="Button1"runat="server"Text="Submit "></asp:Button></P> <P><asp:Labelid="Label1"runat="server"></asp:Label></P> </form> </body></HTML>

  8. GetName.asp.vb Public Class GetNameTest Inherits System.Web.UI.Page Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents GetName1 As GetName Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer x = 1 End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = GetName1.Name End Sub End Class You have to declare this by yourself!

  9. Event Sequence GetNameTest.aspx GetName.ascx Page_Load() First Time Page_Load() Timeline TextBoxName_Load() Page_Load() Page_Load() PostBack TextBoxName_Load() Button1_Click()

  10. Dynamic Properties Development App Production Database Database <appSettings> <add key="db" value="dev"/> </appSettings> <appSettings> <add key="db" value="prod"/> </appSettings> • Property values can be changed after the application is built and deployed • Great for database connection strings, Web Service URLs, machine names • RAD designer support to “bind” to these configurable settings

  11. Web.config File <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="connectionString"value='Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=C:\Minder\dotnet\ADO\database\Northwind.mdb;Mode=Share Deny None;' /> </appSettings> <system.web> …. <configuration>

  12. CategoryDDLB.ascx.vb Imports System.Data.OleDb Namespace CustomTag Public MustInherit Class CategoryDDLB Inherits System.Web.UI.UserControl Protected WithEvents DropDownListCategory As System.Web.UI.WebControls.DropDownList Private m_catId As Integer = 0 Public Property CatID() Get Return m_catId End Get Set(ByVal Value) m_catId = Value End Set End Property #Region " Web Form Designer Generated Code " ' …… #End Region Your cannot use ViewState to pass data between a page and its user controls

  13. Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then Dim connString As String Dim daCat As OleDbDataAdapter Dim dsCat As New DataSet() Dim catCmd As OleDbCommand Dim conn As OleDb.OleDbConnection connString = ConfigurationSettings.AppSettings("connectionString") conn = New OleDb.OleDbConnection(connString) conn.Open() catCmd = New OleDbCommand() catCmd.CommandText = "select categoryID, categoryName from Categories" catCmd.CommandType = CommandType.Text catCmd.Connection = conn daCat = New OleDbDataAdapter(catCmd) daCat.Fill(dsCat, "Categories") DropDownListCategory.DataSource = dsCat DropDownListCategory.DataMember = "Categories" DropDownListCategory.DataValueField = "CategoryID" DropDownListCategory.DataTextField = "CategoryName" DataBind() Else CatID = DropDownListCategory.Items(DropDownListCategory.SelectedIndex).Value End If End Sub End Class End Namespace Continued…

  14. WebForm1.aspx

  15. WebForm1.aspx <%@ Register TagPrefix="Category" TagName="CategoryDDLB" Src="CategoryDDLB.ascx" %> <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="usercontrol.WebForm1"%> <HTML><HEAD><title>WebForm1</title> </HEAD><body> <form id="Form1" method="post" runat="server"> <P> <Category:CategoryDDLBID="DDLBCat" Runat="Server"> </Category:CategoryDDLB></P> <P> <asp:Button id="Button1" runat="server" Text="Select a Category"></asp:Button></P> <P> <asp:Label id="Label1" runat="server"></asp:Label></P> </form></body></HTML>

  16. WebForm1.aspx.vb Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents DDLBCat As CustomTag.CategoryDDLB #Region " Web Form Designer Generated Code " ' …. #End Region Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = "You have selected category ID: " & DDLBCat.CatID End Sub End Class

  17. Using User Defined Classes in ASP.NET

  18. Salary Class: Salary.vb Namespace HR Public Class Salary Protected m_Hours As Double Protected m_empName As String = "" Protected m_Rate As Double Public Sub New() End Sub Public Property EmpName() As String Get Return m_empName End Get Set(ByVal Value As String) m_empName = Value End Set End Property Public Property Rate() As Double Get Return m_Rate End Get Set(ByVal Value As Double) If Value <= 500 And Value >= 10 Then m_Rate = Value Else End If End Set End Property

  19. Continued… Public Property Hours() As Double Get Return m_Hours End Get Set(ByVal Value As Double) m_Hours = Value End Set End Property Public ReadOnly Property Pay() As Double Get Dim total As Double If (m_Hours <= 40) Then total = m_Hours * m_Rate Else total = 40 * m_Rate + 1.5 * (m_Hours - 40) * m_Rate End If Return total End Get End Property End Class End Namespace

  20. HRSalary.aspx.vb Public Class HRSalary Inherits System.Web.UI.Page Protected WithEvents TextBoxName As System.Web.UI.WebControls.TextBox Protected WithEvents TextBoxRate As System.Web.UI.WebControls.TextBox Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents ButtonCalculate As System.Web.UI.WebControls.Button Protected WithEvents TextBoxHours As System.Web.UI.WebControls.TextBox Private salary1 As HR.Salary Private Sub ButtonCalculate_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ButtonCalculate.Click Try salary1 = New HR.Salary() salary1.Hours = Val(TextBoxHours.Text) salary1.Rate = Val(TextBoxRate.Text) Label1.Text = TextBoxName.Text & _ ", your weekly wage is: " & _ Format(salary1.Pay, "$#,###.##") Catch ex As Exception Label1.Text = _ "You did not enter the proper hours or rate. Try again!" End Try End Sub End Class

  21. HRSalary.aspx <%@ Page Language="vb" AutoEventWireup="false" Codebehind="HRSalary.aspx.vb" Inherits="exwage.HRSalary"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><title>HRSalary</title></HEAD> <body> <form id="Form1" method="post" runat="server"> <P> <br> <TABLE id="Table1" style="WIDTH: 339px; HEIGHT: 128px" cellSpacing="1" cellPadding="1" width="339" border="1"> <TR> <TD style="WIDTH: 154px"> <P align="right"><STRONG>Employee Name:</STRONG></P> </TD> <TD> <asp:TextBox id="TextBoxName" runat="server"></asp:TextBox></TD> </TR> <TR> <TD style="WIDTH: 154px"> <P align="right"><STRONG>Hourly Rate:</STRONG></P> </TD> <TD> <asp:TextBox id="TextBoxRate" runat="server"></asp:TextBox></TD> </TR>

  22. Continued… <TR> <TD style="WIDTH: 154px; HEIGHT: 29px"> <P align="right"><STRONG>Number of Hours:&nbsp; </STRONG> </P> </TD> <TD style="HEIGHT: 29px"> <asp:TextBox id="TextBoxHours" runat="server"></asp:TextBox></TD> </TR> <TR> <TD style="WIDTH: 154px"><STRONG> <P align="right">&nbsp;</P> </STRONG> </TD> <TD> <P align="right"> <asp:Button id="ButtonCalculate" runat="server" Text="Calculate Salary" Font-Bold="True"></asp:Button></P> </TD> </TR> </TABLE> <br> &nbsp;&nbsp; <asp:Label id="Label1" runat="server" Font-Bold="True"></asp:Label></P> </form> </FORM> </body> </HTML>

  23. Separation of Presentation and Business Logic Zero degree of separation .ASPX = HTML Code + Event Procedure Script 1st degree of separation .ASPX (presentation tier) + Code Behind (Event Procedures) 1.5 degree of separation .ASPX (presentation tier) + Code Behind (Event Procedures + Functions)

  24. Continued… 2nd degree of separation .ASPX (presentation tier) + Code Behind (Event Procedures + Functions) + Business Objects 3rd degree of separation .ASPX (presentation tier) + Code Behind (Event Procedures + Functions) + Business Objects + Web Services (Remote Procedures) Achieving Zero degrees of separation from your business client.

More Related