开发者

asp.net In a repeater is it possible to call a public function from another class?

开发者 https://www.devze.com 2022-12-12 10:27 出处:网络
Say I have this repeater which makes use of a public function called Test in the code-behind. <asp:RepeaterID=\"Repeater1\" runat=\"server\">

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.ToStr开发者_如何学运维ing();
}

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?


Create a Base class for your pages and put your common function there. So it will be available for all inherited child pages.


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?


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">

<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">

<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>


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]); 


The accepted answer, and other answers here, suggest that inheritance is necessary to solve this problem. This confused me, so I asked my own question - How to call functions in a Class file, when inside a Repeater, without inheritance - and thanks to @Walsharoo I found out I merely needed to add the Project Name and I could call a static function (in a Class) from inside a repeater:

<td><%# [ProjectName].ClassName.MyStaticFunction(Eval("MyColumnName"))%></td>

This may prove helpful to others, I hope, though I'm not suggesting you're all as daft as me for forgetting this simple trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号