I have just recently moved from VBscript / ASP classic to C# ASP.NET 3.5 and wanted to reuse a function which displays and image every time it's called using <%moregif%>
Sub MoreGif
response.write("
End Sub
How should I go about doing this?
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Mar 10, 2011, 7:09 AM
If in Solution Explorer you right click on App_Code, select Add New Item, then select Class and at the bottom of the dialog give it a suitable name, say Utilities.cs, then press 'Add'.
You can then add your property like this:
public class Utilities
{
public Utilities()
{
//
// TODO: Add constructor logic here
//
}
public static string MoreGif
{
get { return @"
}
}
Now from any page you can do:
<%Response.Write(Utilities.MoreGif);%>
Max TenenbaumPosted Mar 10, 2011, 7:24 AM
Max TenenbaumPosted Mar 10, 2011, 6:18 AM
I encountered a protection level error with your script, which I fixed by adding public in front of void, but otherwise that worked great!
Now I need to reuse this function throughout the site, where I should I be storing and referencing the code from as using a web control doesn't seem the right way to go about it.
In addition I attempted to add this function to the master pages code behind page, but I get this error:
The name 'MoreGif' does not exist in the current context
Thanks
VulpesPosted Mar 10, 2011, 6:05 AM