By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

accessing user defined utility functions

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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!
Nov 19 '05 #2

P: n/a
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/de...us/cpref/html/
frlrfmicrosoftcsharpcsharpcodeproviderclasstopic.a sp

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

/// M

"Chinmay" <Ch*****@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
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!

Nov 19 '05 #3

P: n/a
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" <nz******@gmail.com> wrote in message
news:1a**************************@posting.google.c om...
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

Nov 19 '05 #4

P: n/a
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

Nov 19 '05 #5

P: n/a

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

Regards,

Neil

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.