views:

346

answers:

4

Say I have this repeater which makes use of a public function called Test in the code-behind.

<asp:Repeater  ID="Repeater1" runat="server">
<HeaderTemplate>
  <table>
</HeaderTemplate>

<ItemTemplate>
  <tr>
     <td><%# Eval("MyCol1")%></td>
     <td><%# Eval("MyCol2")%></td>
     <td><%# Test((int)Eval("MyCol1"))%></td>
  </tr>
</ItemTemplate>

<FooterTemplate>
  </table>
</FooterTemplate>
</asp:Repeater>

In my code-behind, I have this function

public string Test (int Value)
{
   return "Test"+Value.ToString();
}

This works fine but in my website I will have similar repeaters on various pages and most of them will need to call the Test function. Instead of having it in the code-behind of each web page would it be possible to put it in a public static class and call it directly from the repeater? Something like this (which does not work):

<td><%# MyStaticClass.Test((int)Eval("MyCol1"))%></td>

The only solution I've come up with would be to change the function in the code-behind to:

public string Test (int Value)
{
   return MyStaticClass.Test(Value);
}

But it would be neater if I didn't have to put any code in the code-behind of each webpage (ie I would prefer the repeater to call the static function directly).

Any ideas or suggestions?

+1  A: 

create Base class for your pages. Put your common function there. so it will be available for all inherited child pages.

Saar
A: 

Do you use a master page? You could drop it in there if you do.

I would do something like this:

On every content page that inherits from the master page you need:

<%@ MasterType VirtualPath="[path to your master page]" %>

This allows you to get to the public methods on the master.

On your content page, whenever you need to call the Test method:

Master.Test([your int value]);
adrianos
Yes I do. Can you give an example how I would call, from the repeater, a public function stored in the master page?
Anthony
updated the answer, let me know how you get on.
adrianos
Indeed this works. Thanks! The question now is: is it better to put those common function in the master page or in a base class? If I had multiple master pages then it would make sense to use the base class but in my case I only have one master page. Decisions, decisions...
Anthony
I use multiple master pages but they all inherit a common master.
adrianos
Most people seem to suggest to go for a base class for this kind of functionality instead of putting those common functions in the master page. Also if you have a webpage which doesn't depend on the master page then you're stuck. With a base class you can still use it. see this post for example:http://stackoverflow.com/questions/738777/base-page-or-base-master-page-or-nested-mastersand this article: http://dotnetslackers.com/articles/aspnet/Four-Helpful-Features-to-Add-to-Your-Base-Page-Class.aspxTechnically speaking both solutions work of course.
Anthony
Interesting. I always use master pages so it works for me, but it's good to know the options.
adrianos
A: 

Can't

<td><%# Test((int)Eval("MyCol1"))%></td>

Be represented by

<td><%# Eval("MyCol1", "Test: {0}") %></td>

Or is there more to your test function that that?

Mark Holland
Yes the Test function was just an example. There is more to it in reality.
Anthony
Fair enough. What about the using the OnItemDataBound event and calling the method from there instead?
Mark Holland
That would probably work but the idea is not to have any code in the code-behind. Ideally I'd like the aspx page to call the common function directly.
Anthony
A: 

I typically create a class for shared/static functions in my websites. This class can then be called from any page in the site and allows the code to be seperated from your UI or presentation code.

clsGlobal.cs

namespace testClass
{
    public class clsGlobal
        {
        public static int EvalNumber(int value){
            int localInt = 5;

            return localInt + value;
        }
    }
}

Default.aspx Option 1

This version imports the namespace of the global class into the aspx page so you can reference your static functions without using the namespace. "clsGlobal.EvelMethod()"

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="testClass._Default" %>
<%@ Import Namespace="testClass" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Static Class Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%= clsGlobal.EvalNumber(5).ToString() %>
    </div>
    </form>
</body>
</html>

Default.aspx Option 2

This version calls the static function using the fully qualified function name including the namespace. "testClass.clsGlobal.EvalNumber()"

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="testClass._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Static Class Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%= testClass.clsGlobal.EvalNumber(5).ToString() %>
    </div>
    </form>
</body>
</html>
Tinidian
Can you give an example of how you call one static function of your class directly from the aspx page?
Anthony
Updated code example above to show example.
Tinidian
Thanks. Very useful. I hadn't realised it was possible to do this.
Anthony