Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert function into class? 1

Status
Not open for further replies.

dswitzer

Technical User
Aug 2, 2002
298
US
Converting from Classic ASP -- using VB .NET

Ok - I have a function called checkNull in VBScript:
Code:
Function checkNull(fieldValue2,decimals)
  If isNull(fieldValue2) Then
    checkNull = 0
  Else
    checkNull = FormatNumber(fieldValue2,decimals)
  End If
End Function

I am lost as to how to integrate this into ASP .NET.

I can include it in my .aspx file and it works fine -- but I should be able to build a class with this function (and then a few others) -- and then import it into my aspx page shouldn't I?

I'm guessing the file will look something like this:
Code:
Public Class DataFormatting : Inherits Page

Function checkNull(fieldValue2 as double,decimals as integer)
	If len(fieldValue2)=0 Then
		checkNull = " "
	Else
		checkNull = FormatNumber(fieldValue2,decimals)
	End If
End Function

End Class

I call this file DataFormatFunctions.ascx (and I know I missed some important stuff in this file)....

I hate to ask like this but I am lost and becoming frustrated:
1. can someone glance at this and point me in the right direction -- that is all the text in my ascx file -- and I know I am missing something(s).
2. what do I need to add to my main aspx page to make these functions available to the main aspx page

Thanks.








 
looks like vb, not c
you need filname.aspx.vb, not ascx which is C# i think

in same dir(maybe) filename.aspx, filename.aspx.vb

aspx page...
Code:
<%@ Page Language="VB" debug="true" Inherits="myMain_cb" Src="filename.aspx.vb" %>
<script src="js\page.js" language="javascript" type="text/javascript"></script>
<html>
<head>
	<link rel=stylesheet type="text/css" href="css\page.css">
	<TITLE>My Main Page Title</TITLE>
</head>
<body>
    <form runat="server" id=frmRef>
      bla bla bla

aspx.vb code

Code:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HTMLControls
Imports Microsoft.VisualBasic

Public Class myMain_cb
	Inherits System.Web.UI.Page

Sub page_load()
    If Not Page.IsPostBack Then
        myField.Text = checkNull(whatyouneed)
    End If
End Sub

Function checkNull(fieldValue2 as double,decimals as integer)
    If len(fieldValue2)=0 Then
        checkNull = "&nbsp;"
    Else
        checkNull = FormatNumber(fieldValue2,decimals)
    End If
End Function

End Class
'You can add more classes down here on same aspx.vb file
 
Thanks adamroof.

Appreciate the assistance -- and I can get this to work -- Thanks.

I guess I am struggling with the concepts as much as anything. If I have 5 basic data formatting functions that I use on almost every page -- is there a way to write an include so I don't have to look at that code on every code-behind page that needs it? Of course I will not have a massive (all functions I have ever created) include -- but will have 2-3 for data formatting, a few for specific calculations not covered in the SQL, etc...

In classic asp -- I would just have a simple include and include those functions when needed -- how would I do this in asp.net -- or is it better design to include each function directly into the code-behind? I hate to do this because some functions need changes and I would like to change these in only one place for ease of maintenance.






 
save the file as filename.ascx and code-behind filename.ascx.vb. The ascx file is a user control. You can now place this in every page to get the same look, feel, and functionality for the user control.
 
Good link - Thanks.

For the record, I simply put my function into a page named
includetestfunctions.aspx.vb:
Code:
Function checkNull(fieldValue2 as double,decimals as integer)
If len(fieldValue2)=0 Then
checkNull = "&nbsp;"
Else
checkNull = FormatNumber(fieldValue2,decimals)
End If
End Function

and then used this "include" (since I'm not sure of the terminology yet) in my main aspx page as such:

<Script Runat="Server" SRC="includetestfunctions.aspx.vb"/>

Cool.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top