Here is a sample for custom control/composite control development 
Author Message
 Here is a sample for custom control/composite control development

Hi,

I noticed that many people asking for custom control/composite control
sample code.  I developed a sample as I am learning VB/ASP.NET and please
find the code below.

Any suggestions on the code are welcome.

Thank you,

Vasu

================Test1.aspx


Codebehind="test1.aspx.vb" Inherits="FT.test1"%>


Assembly="FT.Controls.FTPerson" %>

<html>
          <head>
                    <title></title>
          </head>
          <body MS_POSITIONING="GridLayout">
                    <form id="Form1" method="post" action="test1.aspx"
runat="server">
                             <asp:PlaceHolder ID="MyPlaceHolder"
Runat="server" />
                    </form>
          </body>
</html>
================END

================ Test1.aspx.vb
Imports System
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections

Imports System.Collections.Specialized

Imports FT.Controls

Namespace FT

    Public Class test1
        Inherits System.Web.UI.Page : Implements IPostBackDataHandler

        Public MyPlaceHolder As PlaceHolder
        Private MyPerson As FT.Controls.FTPerson = New
FT.Controls.FTPerson()

        Public Function LoadPostData(ByVal PostDataKey As String, ByVal
Values As NameValueCollection) As Boolean Implements
IPostBackDataHandler.LoadPostData
            MyPerson.MyFormVars = Values
            Return False
        End Function

        Public Sub RaisePostDataChangedEvent() Implements
IPostBackDataHandler.RaisePostDataChangedEvent
        End Sub

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            MyPerson.ID = "Person"
            MyPerson.MyFormVars = Request.Form
            MyPlaceHolder.Controls.Add(MyPerson)
        End Sub

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.

        <System.Diagnostics.De{*filter*}StepThrough()> Private Sub
InitializeComponent()
        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
#End Region
    End Class
End Namespace
================END

================FTPerson.vb

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections
Imports System.Collections.Specialized

Namespace FT.Controls

    Public Class FTPerson : Inherits Control : Implements INamingContainer

        Private WithEvents Submit As FT.Controls.FTButton = New
FT.Controls.FTButton()
        Private MyFirstName As FT.Controls.FTTextbox = New
FT.Controls.FTTextbox()
        Private MyLastName As FT.Controls.FTTextbox = New
FT.Controls.FTTextbox()

        Public MyFormVars As NameValueCollection

        Private strFirstName As String
        Private strLastName As String

        Protected Overrides Sub OnPreRender(ByVal E As EventArgs)
            Dim MyControl As FT.Controls.FTTextbox
            MyControl = Me.FindControl("FirstName")
            MyControl.TextboxValue = strFirstName
            MyControl = Me.FindControl("LastName")
            MyControl.TextboxValue = strLastName
        End Sub

        Public Sub PersonSubmit(ByVal Sender As Object, ByVal E As
EventArgs) Handles Submit.Click
            strFirstName = "My First Name " & MyFormVars(Me.UniqueID &
":FirstName:Ctrl1")
            strLastName = "My Last Name " & MyFormVars(Me.UniqueID &
":LastName:Ctrl1")
        End Sub

        Protected Overrides Sub CreateChildControls()
            MyFirstName.TextboxValue = strFirstName
            MyFirstName.LabelValue = "First Name"
            MyFirstName.ID = "FirstName"

            MyLastName.TextboxValue = strLastName
            MyLastName.LabelValue = "Last Name"
            MyLastName.ID = "LastName"

            Submit.ButtonLabel = "Submit"
            Submit.ID = "PersonSubmit"

            Me.Controls.Add(New LiteralControl("<table
border='1'><tr><td>"))
            Me.Controls.Add(MyFirstName)
            Me.Controls.Add(New LiteralControl("</td></tr><tr><td>"))
            Me.Controls.Add(MyLastName)
            Me.Controls.Add(New LiteralControl("</td></tr>"))
            Me.Controls.Add(New LiteralControl("<tr><td colspan='2'>"))
            Me.Controls.Add(Submit)
            Me.Controls.Add(New LiteralControl("</td></tr>"))
            Me.Controls.Add(New LiteralControl("</table>"))
        End Sub
    End Class
End Namespace

================END

================FTTextbox.vb
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections
Imports System.Collections.Specialized

Namespace FT.Controls
    Public Class FTTextbox : Inherits Control : Implements INamingContainer
        Private MyTextbox As New System.Web.UI.WebControls.TextBox()
        Private MyLabel As New System.Web.UI.WebControls.Label()

        Private strTextboxValue As String
        Private strLabelValue As String

        Public Property TextboxValue() As String
            Get
                Return strTextboxValue
            End Get

            Set(ByVal Value As String)
                strTextboxValue = Value
            End Set
        End Property

        Public Property LabelValue() As String
            Get
                Return strLabelValue
            End Get

            Set(ByVal Value As String)
                strLabelValue = Value
            End Set
        End Property

        Protected Overrides Sub CreateChildControls()
            Me.Controls.Add(New LiteralControl("<table><tr><td>"))
            Me.Controls.Add(MyLabel)
            Me.Controls.Add(New LiteralControl("</td><td align='right'>"))
            Me.Controls.Add(MyTextbox)
            Me.Controls.Add(New LiteralControl("</td></tr>"))
            Me.Controls.Add(New LiteralControl("<tr><td
colspan='2'align='right'>"))
            Me.Controls.Add(New LiteralControl("?"))
            Me.Controls.Add(New LiteralControl("</td></tr></table>"))
        End Sub

        Protected Overrides Sub OnPreRender(ByVal E As EventArgs)
            MyTextbox.Text = strTextboxValue
            MyLabel.Text = strLabelValue
        End Sub
    End Class

End Namespace

================END

================FTButton.vb
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections
Imports System.Collections.Specialized

Namespace FT.Controls

    Public Class FTButton : Inherits Control : Implements INamingContainer

        Private strButtonLabel As String  'Triggers the controls' Click
event in the Parent program   - Event Step 4
        Public Event Click(ByVal Sender As Object, ByVal E As EventArgs)
        Protected Overrides Sub CreateChildControls()

            Dim MyButton As New System.Web.UI.WebControls.Button()

            'Redirects user's click action to MyButton_Click sub    - Event
Step 1
            AddHandler MyButton.Click, AddressOf MyButton_Click
            MyButton.Text = strButtonLabel
            Me.Controls.Add(MyButton)
        End Sub

        'Call the OnClick sub to raise the Click event  -   Event Step 2
        Private Sub MyButton_Click(ByVal Sender As Object, ByVal E As
EventArgs)
            OnClick(EventArgs.Empty)
        End Sub

        'Click event will be raised to be captured in the Parentgram    -
Event Step 3

        Protected Sub OnClick(ByVal E As EventArgs)
            RaiseEvent Click(Me, E)
        End Sub

        Public Property ButtonLabel() As String
            Get
                Return strButtonLabel
            End Get

            Set(ByVal Value As String)
                strButtonLabel = Value
            End Set
        End Property
    End Class
End Namespace

================END



Sat, 01 May 2004 13:06:57 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Validating a Custom Composite Control

2. REQUEST:Sample of custom Web Control with Collection Property

3. Sample for Graph Custom Control???

4. I am trying to update a record, i am not using data control

5. Composite Controls

6. Using Reflection to load Composite Control

7. Composite Control Question...

8. Composite Control...

9. scripting - font - ATL Composite Activex Controls

10. Composite Control Collection Beta 4 Release

11. How does lookup controls work with composite keys

12. composite control not displayed in ie5

 

 
Powered by phpBB® Forum Software