Reputation: 21
I have a function written in C# that resides on 12 individual pages of an ASPX application. I already use cs files in App_Code, so that is not the problem.
Here is the call in HTML:
<asp:Image ID="Image1" runat ="server" ImageUrl='<%# (string) FormatImageUrl( (string) Eval("Image")) %>' />
and here is the method:
protected string FormatImageUrl(string url)
{
if (url != null && url.Length > 0)
return ("~/" + url);
else return null;
}
(A great find might I add.)
I want to move this method to App_Code and reference one instance from many pages.
I have looked into adding the namespace.function.method in the HTML code, but that throws an error.
I have also looked into using a DLL for the method, but I still can not reference it correctly in the HTML code.
Upvotes: 0
Views: 31
Reputation: 21
I spent some time in think tank mode over the past couple of days and finally came up with the solution.
I created an App_Code class file named ImageFormatter.cs and it looks like this:
/// <summary>
/// Summary description for ImageFormatter
/// </summary>
public class ImageFormatter
{
public ImageFormatter()
{
//
// TODO: Add constructor logic here
//
}
public static string FormatImageUrl(string url)
{
if (url != null && url.Length > 0)
return ("~/" + url);
else return null;
}
}
NOTE: I removed the NameSpace definitioin and the associated { and }.
I commented out the FormatImageUrl function in two pages of the app to test my theory.
I modified the HTML code to look like this in the same two files.
<asp:Image ID="Image1" runat ="server" ImageUrl='<%# (string) ImageFormatter.FormatImageUrl( (string) Eval("Image")) %>' />
So for those of you needing to move a function to a class:
Upvotes: 1