Solved

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

Posted on 2004-09-06
5
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 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