• Save
ASP.NET User Controls - 20090828
Upcoming SlideShare
Loading in...5
×
 

ASP.NET User Controls - 20090828

on

  • 4,993 views

 

Statistics

Views

Total Views
4,993
Views on SlideShare
4,989
Embed Views
4

Actions

Likes
0
Downloads
1
Comments
0

2 Embeds 4

http://www.linkedin.com 2
https://www.linkedin.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    ASP.NET User Controls - 20090828 ASP.NET User Controls - 20090828 Presentation Transcript

    • 28-08-2009 DirectI ASP.NET User Controls Presenter: Viral Patel
    • ASP.NET Controls Types of Controls (1) HTML Server Controls (Traditional HTML Tags) (2) ASP.NET Server Controls (NEW ASP.NET Tags) (3) AJAX Server Controls (ASP.NET AJAX Controls) (4) Controls Created By Users
    • Controls created by Users ASP.NET User Control ASP.NET Custom Control • Derived Custom Control • Composite Custom Control • Fully Custom Control
    • ASP.NET User Controls1. Prerequisites2. User Control vs Web Page3. Create ASP.NET User Control4. Add User Control to an ASP.NET Web Page5. Define Properties of ASP.NET User Control6. Define Methods of ASP.NET User Control
    • 1. Prerequisites ASP.NET Web Page  Single Page Model  Code-Behind Page Model − Markup File - .aspx file (Design|Source) − Code File - .aspx.(vb|cs) file  ASPX file (Default.aspx) − <%@ Page Language=”C#” AutoEventWireUp=”true” CodeFile=”Default.aspx.cs” Inherits=”Default” %>
    • Demo 1 : Create ASP.NET Web Page Note <%@ Page Directive
    • 2.User Control vs Web Page ASP.NET Web Page ASP.NET User Control ASPX ASCX <%@ Page … %> <%@ Control … %> System.Web.UI.Page System.Web.UI.Controlcan run as stand-alone file cannot run as stand-alone file html, body, form html, body, form – not present
    • 3.Create ASP.NET User Control• ASP.NET User Control – ASCX file (MyUserContol.ascx) • <%@ Control Language=”C#” AutoEventWireUp=”true” CodeFile=”MyUserControl.ascx.cs” Inherits=”MyUserControl” %> • MyUserControl inherits UserControl Class• Add ASP.NET Server Control(s)• Add ASP.NET Code for the Control(s)
    • Demo 2 : Create ASP.NET User Control User Control Note <%@ Control Directive Web Page
    • 4. Add User Control to an ASP.NET Web Page User Control cannot run stand alone It must be added to ASP.NET web page (aka host page) (.aspx) STEP 1 : User Control needs to be registered to the host page − <%@ Register TagPrefix=”UC” TagName=”MyUserControl” Src=”~/Controls/MyUserControl.ascx” %> STEP 2 : add the user control to host page − <TagPrefix:TagName ID=”UserControlID” runat=”server” /> − <UC:MyUserControl ID=”MyUserControl1” runat=”server” /> ALTERNATIVE: − Drag User Control and Drop to Design View of Web Page
    • Demo 3 : Register & Add User Control to Web Page Registering User ControlThings to Note : 1. <%@ Register Directive 2. TagPrefix, TagName, Src - Attributes Add User Control Note User Control Declaration
    • 5. Define Properties of ASP.NET User Control User Control can have properties like ASP.NET Web Controls. To add a property to a User Control − Syntax: <Access_Specifier> <PropertyDataType> <PropertyName> { get { return <PropertyValue>; } [set { ViewState[“PropertyName”] = value; }] } − Example:  public ListItemCollection FinalItems { get { return TargetList.Items; } }  public Boolean AllowDuplicates { get { return (Boolean)ViewState[“allowDuplicates”]; } set { ViewState[“allowDuplicates”] = value; } } For readonly property we would have only get method For read+write property we would have both get & set methods Properties can be set declaratively (in attribute) or programmatically.
    • Demo 4 : Properies to User ControlThings to Note : 1. get & set syntax 2. ViewState has to be filled in manually for User Control
    • 6. Define Methods of ASP.NET User Control User Control can have methods like ASP.NET Web Controls. To add a method to a User Control − Syntax: <Access_Specifier> <MethodReturnDataType> <MethodName> { ...Method Definition... } − Example:  protected void AddItem(ListItem li) { TargetList.Items.Add(li); }  protected void ClearAll() { SourceList.Items.Clear(); TargetList.Items.Clear(); }
    • Demo 5 : Methods to User Control
    • Any Doubts ?