Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-06
5
Medium Priority
?
293 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

661 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