Asp.net In a repeater, can a public function be called from another class?

Say I have this repeater that uses the public Test function in code.

<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, I have this function

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

This works great, but on my site I will have similar repeaters on different pages, and most of them will need to call the testing function. Instead of having it in the code for each web page, is it possible to put it in an open static class and call it directly from the relay? Something like this (which doesn't work):

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

The only solution I came across was to change the function in the code to:

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

But that would be more accurate if I didn’t have to put the code in the code for each web page (i.e. I would prefer the relay to call the static function directly).

?

+3
5

. , .

+2

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

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

?

+2

/ -. .

clsGlobal.cs

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

            return localInt + value;
        }
    }
}

Default.aspx 1

aspx, , . "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 2

, , . "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>
+2

? , .

- :

, , :

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

.

, :

Master.Test([your int value]); 
0

, . , - , , - @Walsharoo , static ( ) :

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

, , , , , .

0

Source: https://habr.com/ru/post/1723648/


All Articles