Solved

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

Posted on 2004-09-06
5
286 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Runtime Error 2 37
Displaying a web form as a modal popup dialog box. 4 30
Regex validation 2 24
List<PaisEntity> - show some 1 24
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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