Results 1 to 6 of 6

Thread: define a function in an external file

  1. #1

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727

    Resolved define a function in an external file

    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.
    Last edited by wildcat_2000; Nov 11th, 2004 at 01:06 PM.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  2. #2

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    replying to myself...
    VB Code:
    1. <Script Runat="Server" SRC="var_db.aspx"/>
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  3. #3
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by wildcat_2000
    replying to myself...
    VB Code:
    1. <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:

    VB Code:
    1. Dim f as Functions
    2. try
    3. f= new Functions
    4. Dim s as String
    5. s="blah blah"
    6.  
    7. s= f.blah(s)
    8. catch ex
    9.  Response.Write(An Error Occurred: " & ex.toString())
    10. finally
    11. f=nothing
    12. end try

    Jon

  4. #4

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    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.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  5. #5
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    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:

    VB Code:
    1. Imports System.Data
    2. Imports System.Data.SqlClient
    3. Imports System.Web.Security
    4. Imports System.Web.Mail
    5. Public Class Functions
    6.     Inherits System.Web.UI.Page
    7.     '/** Class Functions
    8.     '/** Class written for: Krause
    9.     '/** Purpose: To define global functions to be used across the
    10.     '/**          entire asp.net IMS application.  Any functions that
    11.     '/**          are reusable across various pages should be placed
    12.     '/**          in this class.  
    13.  
    14.     '/** Usage:
    15.     '/**                Dim f as New Functions
    16.     '/**                f.FunctionName(Parameters...)
    17.  
    18.     '/** Note:    Please document any global or reusable function you implement
    19.     '/**          so that other developers know how to use your code.
    20.     Public Function DefaultButton(ByRef Page As System.Web.UI.Page, ByRef objTextControl As TextBox, ByRef objDefaultButton As Button)
    21.         '/** Function: DefaultButton
    22.         '/** Purpose:  Allows developers to set a default button on a page.
    23.         '/**           Once the enter key is clicked inside a text box the
    24.         '/**           default button is triggered and executed.
    25.  
    26.         '/** Pre:      Page, Valid Text Box, Button (all by reference)
    27.         '/** Post:     Sets the default button
    28.         '/** Args:     Page as System.Web.UI.Page, Textbox, Button
    29.         '/** Returns:  N/A
    30.         Dim sScript As New System.Text.StringBuilder
    31.  
    32.         sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)
    33.         sScript.Append("function fnTrapKD(btn){" & vbCrLf)
    34.         sScript.Append(" if (document.all){" & vbCrLf)
    35.         sScript.Append("   if (event.keyCode == 13)" & vbCrLf)
    36.         sScript.Append("   { " & vbCrLf)
    37.         sScript.Append("     event.returnValue=false;" & vbCrLf)
    38.         sScript.Append("     event.cancel = true;" & vbCrLf)
    39.         sScript.Append("     btn.click();" & vbCrLf)
    40.         sScript.Append("   } " & vbCrLf)
    41.         sScript.Append(" } " & vbCrLf)
    42.         sScript.Append("}" & vbCrLf)
    43.         sScript.Append("</SCRIPT>" & vbCrLf)
    44.         objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ID & ")")
    45.         Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString)
    46.  
    47.     End Function
    48.     Function ValidatePassword(ByVal txtP As String, ByVal txtCP As String) As Boolean
    49.         '/** Function: ValidatePassword
    50.         '/** Purpose:  To validate a password with a confirmation password
    51.         '/** Pre:      Password / Confirmation Exist
    52.         '/** Post:     Confirms password
    53.         '/** Returns:  Boolean value
    54.  
    55.         'function validates whether a password is valid or not
    56.         If (txtP = txtCP) Then
    57.             ValidatePassword = True  'good
    58.         Else
    59.             ValidatePassword = False 'bad
    60.         End If
    61.     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 ?

  6. #6

    Thread Starter
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    thank you, i'll try this out.

    cheers,

    wc.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width