Solved

ASP.NET_NewbieHowTo: Import external class files in .aspx file

Posted on 2004-09-06
5
285 Views
Last Modified: 2011-09-20
Question: How do I "USE" a class I created in my webform?

The book i'm reading (Teach yourself ASP.NET in 21 days) works from the premise that everyone is using NOTEPAD. As such in day 03 it talks about using creating classes which I understand. Only problem is...i'm using Visual Studio.net (2003) and as such, i'm using "code-behind". THE BOOK IS NO HELP WITH CODE-BEHIND.

I know, from reading code that a NameSpace is important (naming collisions, etc.) by i do not know how to embedd the directive in the page.

the class code follows...thanks in advance!

the_sleeper

=======[simple exercise]==========

Namespace day03
    Public Class clsHuman
        Private m_height As String
        Private m_weight As String
        Private m_haircolor As String
        Private m_eyecolor As String
        Private m_birthdate As Date

        Public Property Height() As String
            Get
                Return m_height
            End Get
            Set(ByVal Value As String)
                m_height = Value
            End Set
        End Property

        Public Property Weight() As String
            Get
                Return m_weight
            End Get
            Set(ByVal Value As String)
                m_weight = Value
            End Set
        End Property

        Public Property HairColor() As String
            Get
                Return m_haircolor
            End Get
            Set(ByVal Value As String)
                m_haircolor = Value
            End Set
        End Property

        Public Property EyeColor() As String
            Get
                Return m_eyecolor
            End Get
            Set(ByVal Value As String)
                m_eyecolor = Value
            End Set
        End Property

        Public Property BirthDate() As Date
            Get
                Return m_birthdate
            End Get
            Set(ByVal Value As Date)
                m_birthdate = Value
            End Set
        End Property
    End Class
End Namespace
 
0
Comment
Question by:the_sleeper
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 11993324
Ok. Lets simplify it first: I have a page called HomePage.aspx that  has a namespace ABCINC. I am using code behind. So I would have declared the code as follows:

Imports System
Imports System.Web
Namespace ABCInc
    Public Class MyHomePage
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        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

        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
            Dim clsHuman As New clsHuman
            clsHuman.BirthDate = CDate("1/1/1998")
            clsHuman.EyeColor = "Brown"
            clsHuman.HairColor = "Black"
            clsHuman.Weight = "140"

            Response.Write("Birth Date: " & clsHuman.BirthDate & "<br>")
            Response.Write("Eye Color: " & clsHuman.EyeColor & "<br>")
            Response.Write("Hair Color:" & clsHuman.HairColor)
            Response.Write("Weight: " & clsHuman.Weight & "<br>")

        End Sub
    End Class
End Namespace


Now if you have created your page using VS.Net, it will automatically add the following statement at the top of the page:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyHomePage.aspx.vb" Inherits="VBWebApplication.ABCInc.MyHomePage"%>

So now compile and run your code and you will see the output. But hey clsHuman is missing a very important property: Name :)

HTH, Nauman.

0
 
LVL 25

Accepted Solution

by:
nauman_ahmed earned 500 total points
ID: 11993326
Here is the complete code:

Imports System
Imports System.Web
Namespace ABCInc
    Public Class MyHomePage
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        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

        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
            Dim clsHuman As New clsHuman
            clsHuman.BirthDate = CDate("1/1/1998")
            clsHuman.EyeColor = "Brown"
            clsHuman.HairColor = "Black"
            clsHuman.Weight = "140"

            Response.Write("Birth Date: " & clsHuman.BirthDate & "<br>")
            Response.Write("Eye Color: " & clsHuman.EyeColor & "<br>")
            Response.Write("Hair Color:" & clsHuman.HairColor)
            Response.Write("Weight: " & clsHuman.Weight & "<br>")

        End Sub

    End Class
    Public Class clsHuman
        Private m_height As String
        Private m_weight As String
        Private m_haircolor As String
        Private m_eyecolor As String
        Private m_birthdate As Date
        Private m_Name As String

        Public Property Height() As String
            Get
                Return m_height
            End Get
            Set(ByVal Value As String)
                m_height = Value
            End Set
        End Property

        Public Property Weight() As String
            Get
                Return m_weight
            End Get
            Set(ByVal Value As String)
                m_weight = Value
            End Set
        End Property

        Public Property HairColor() As String
            Get
                Return m_haircolor
            End Get
            Set(ByVal Value As String)
                m_haircolor = Value
            End Set
        End Property

        Public Property EyeColor() As String
            Get
                Return m_eyecolor
            End Get
            Set(ByVal Value As String)
                m_eyecolor = Value
            End Set
        End Property

        Public Property BirthDate() As Date
            Get
                Return m_birthdate
            End Get
            Set(ByVal Value As Date)
                m_birthdate = Value
            End Set
        End Property
        Public Property Name() As String
            Get
                Return m_Name
            End Get
 
            Set(ByVal Value As String)
                m_Name = Value
            End Set
        End Property
    End Class

End Namespace
0
 

Author Comment

by:the_sleeper
ID: 11993638
@nauman_ahmed>>Ok. Lets simplify it first: I have a page called HomePage.aspx that  has a namespace ABCINC. I am using code behind. So I would have declared the code as follows...


 ? Which page are you adding this code to?
    a.  the .aspx
    b. the code behind?

@nauman_ahmed >>But hey clsHuman is missing a very important property: Name :)

wow. my first asp.net bug-a-boo. good catch...



0
 

Author Comment

by:the_sleeper
ID: 12000938
i guess no responses....any takers?
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12001214
You have to add the code in the .aspx.vb file and the following in .aspx page:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyHomePage.aspx.vb" Inherits="VBWebApplication.ABCInc.MyHomePage"%>

Sorry for the late response.

Best, Nauman.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now