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

那年仲夏 提交于 2019-12-01 22:32:04
Saar

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!