PDA

Click to See Complete Forum and Search --> : define a function in an external file


wildcat_2000
Nov 2nd, 2004, 10:51 AM
hi all,

i would like to define a function in a generic file such as myfunction.aspx and then be able to call it from within .aspx files. can someone opint me in a right direction on how to do this in .net?

thank you,

wc.

wildcat_2000
Nov 3rd, 2004, 05:50 AM
replying to myself...<Script Runat="Server" SRC="var_db.aspx"/>
:)

jhermiz
Nov 5th, 2004, 09:16 PM
Originally posted by wildcat_2000
replying to myself...<Script Runat="Server" SRC="var_db.aspx"/>
:)

Thats bad practice.

Define a new class and create functions of that class.
This class can be reusable in ANY aspx page. Dont create functions in an aspx page. Functions need to be called from this class.

Example

class Functions
public function blah(strS as String) as string
blah = strS & " foo bar"
end function
end class

In ANY aspx page:


Dim f as Functions
try
f= new Functions
Dim s as String
s="blah blah"

s= f.blah(s)
catch ex
Response.Write(An Error Occurred: " & ex.toString())
finally
f=nothing
end try


Jon

wildcat_2000
Nov 8th, 2004, 07:02 AM
hi jhermiz,

thank for this suggestion. however, as .net newbie i'm not sure where to start from to create classes. can these be incorporated in a very same .ascx or .aspx file? and how?

thank you,

wc.

jhermiz
Nov 8th, 2004, 07:06 AM
In VS.net

right click your project:
-> Select Class

It will create a class file for you not an .ascx, or an .aspx, it will be a .vb file.

Now you just create a class...

Let me post you one of my classes:


Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Security
Imports System.Web.Mail
Public Class Functions
Inherits System.Web.UI.Page
'/** Class Functions
'/** Class written for: Krause
'/** Purpose: To define global functions to be used across the
'/** entire asp.net IMS application. Any functions that
'/** are reusable across various pages should be placed
'/** in this class.

'/** Usage:
'/** Dim f as New Functions
'/** f.FunctionName(Parameters...)

'/** Note: Please document any global or reusable function you implement
'/** so that other developers know how to use your code.
Public Function DefaultButton(ByRef Page As System.Web.UI.Page, ByRef objTextControl As TextBox, ByRef objDefaultButton As Button)
'/** Function: DefaultButton
'/** Purpose: Allows developers to set a default button on a page.
'/** Once the enter key is clicked inside a text box the
'/** default button is triggered and executed.

'/** Pre: Page, Valid Text Box, Button (all by reference)
'/** Post: Sets the default button
'/** Args: Page as System.Web.UI.Page, Textbox, Button
'/** Returns: N/A
Dim sScript As New System.Text.StringBuilder

sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)
sScript.Append("function fnTrapKD(btn){" & vbCrLf)
sScript.Append(" if (document.all){" & vbCrLf)
sScript.Append(" if (event.keyCode == 13)" & vbCrLf)
sScript.Append(" { " & vbCrLf)
sScript.Append(" event.returnValue=false;" & vbCrLf)
sScript.Append(" event.cancel = true;" & vbCrLf)
sScript.Append(" btn.click();" & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append("}" & vbCrLf)
sScript.Append("</SCRIPT>" & vbCrLf)
objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ID & ")")
Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString)

End Function
Function ValidatePassword(ByVal txtP As String, ByVal txtCP As String) As Boolean
'/** Function: ValidatePassword
'/** Purpose: To validate a password with a confirmation password
'/** Pre: Password / Confirmation Exist
'/** Post: Confirms password
'/** Returns: Boolean value

'function validates whether a password is valid or not
If (txtP = txtCP) Then
ValidatePassword = True 'good
Else
ValidatePassword = False 'bad
End If
End Function


There are about 100 more functions but I cannot post them because of the length...

But now anytime I need to call a function that is reusable in ANY .aspx page I do this:

Dim f as New Functions

f.CallYourFunction()
f=nothing

Make sense ?

wildcat_2000
Nov 11th, 2004, 12:06 PM
thank you, i'll try this out.

cheers,

wc.