accessing user defined utility functions

Discussion in 'ASP .Net' started by Neil Zanella, Jan 18, 2005.

  1. Neil Zanella

    Neil Zanella Guest

    Hello,

    I need to access some user defined utility functions from within
    my ASP.NET pages. I wonder whether there is a way to do this. I
    do not want to use inheritance. I just want to be able to call
    some code contained in a .cs file (C# file) from within several
    ..aspx and .ascx page without having to rewrite the code in each
    such page. I would like to know how this can be accomplished,
    including how I can ensure that ASP.NET will find the C#
    source file.

    Thanks,

    Neil
    Neil Zanella, Jan 18, 2005
    #1
    1. Advertising

  2. ASP.Net won't go and look for .cs files.... when you compile your project
    you'll get a DLL and that DLL will be used for all the functinalities not the
    ..cs. Morover, you can make a Custom Class in your project and add the code
    you want to call in every page. than create objects of that class and you are
    done!
    =?Utf-8?B?Q2hpbm1heQ==?=, Jan 18, 2005
    #2
    1. Advertising

  3. Neil Zanella

    MWells Guest

    By user-defined, I'm guessing you mean that your website user provides some
    C# source to work with. There actually is a way to compile cs at runtime;
    though it's not an ASP.NET specific feature.

    You'll probably want to provide your users with some kind of
    code-upload-and-maintenance UIs, and store the content in a database. One
    way or another, you need to get the cs code to your webserver in order to
    run it.

    For on-the-fly source compilation and execution, google;

    Microsoft.CSharp.CSharpCodeProvider

    Also see...

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfmicrosoftcsharpcsharpcodeproviderclasstopic.asp

    http://www.codeproject.com/csharp/RuntimeCompiling.asp

    /// M

    "Chinmay" <> wrote in message
    news:...
    > ASP.Net won't go and look for .cs files.... when you compile your project
    > you'll get a DLL and that DLL will be used for all the functinalities not

    the
    > .cs. Morover, you can make a Custom Class in your project and add the code
    > you want to call in every page. than create objects of that class and you

    are
    > done!
    MWells, Jan 18, 2005
    #3
  4. You will need to add a seperate code file for this code.

    Example:

    Add a class file "StringUtils.cs".

    //sealed keeps anything from deriving from this
    public sealed class StringUtils
    {
    public static string FormatMoney( decimal amount )
    {
    //return custom formatting of the amount
    }
    //private keeps the class from being instantiated.
    private StringUtils() { }
    }

    Now in all your pages in the project you can access this

    lblAmount.Text = StringUtils.FormatMoney( amount );

    HTH,

    bill

    "Neil Zanella" <> wrote in message
    news:...
    > Hello,
    >
    > I need to access some user defined utility functions from within
    > my ASP.NET pages. I wonder whether there is a way to do this. I
    > do not want to use inheritance. I just want to be able to call
    > some code contained in a .cs file (C# file) from within several
    > .aspx and .ascx page without having to rewrite the code in each
    > such page. I would like to know how this can be accomplished,
    > including how I can ensure that ASP.NET will find the C#
    > source file.
    >
    > Thanks,
    >
    > Neil
    William F. Robertson, Jr., Jan 18, 2005
    #4
  5. Neil Zanella

    Guest

    Thank you for your reply,

    I have tried your suggestion and received the following
    compilation error, which makes me think there is
    something else I should specify:

    Compiler Error Message: CS0246: The type or namespace name
    'StringUtils' could not be found (are you missing a using directive or
    an assembly reference?)

    Please note that I am not using Visual Studio and am hand
    coding the files by hand. I would be very thankful if someone
    please let me know what I should do to get rid of this
    compilation error.

    Thanks,

    Neil
    , Jan 20, 2005
    #5
  6. Neil Zanella

    Guest

    Here is how I solved the problem:
    <%@ Assembly Src="foo.inc" %>

    Regards,

    Neil
    , Jan 23, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anjali Lourda

    Accessing functions defined in Global.asax

    Anjali Lourda, Feb 4, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    9,012
    Ryan Walberg
    Feb 4, 2004
  2. apka
    Replies:
    0
    Views:
    356
  3. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  4. Paavo Helde
    Replies:
    3
    Views:
    273
    Pete Becker
    May 22, 2007
  5. Replies:
    1
    Views:
    494
    Sion Arrowsmith
    Jul 10, 2008
Loading...

Share This Page