无法在Web Control的Page_Load事件中访问Public方法 [英] Cannot access Public method in Web Control's Page_Load event

查看:69
本文介绍了无法在Web Control的Page_Load事件中访问Public方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 .ascx Page_Load 事件中调用我的 Public String Function(...)方法.函数和包含类位于与Web控件相同的代码中.但是我无法使用该功能.我该如何解决这个问题?

I would like to call my Public String Function(...) method from my .ascx's Page_Load event. The function and containing class are located in the same code behind as the web control. But I am unable to access the function. How can I resolve this problem?

示例:

public class Controls_WebApp : System.Web.UI.UserControl, IAttributeAccessor, IUserControlDesignerAccessor
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Call Function
    }
}

public class A
{
    public string Function(string path)
    {

    }
}

推荐答案

按现状,您将需要 A 的实例来调用该方法

As it stands you will need an instance of A to invoke the method

using namespaceOfA;
...
string result = new A().Function("Hello world");

或者,如果函数不依赖于A的状态,则可以将函数设为静态(公共静态字符串Function )

Alternatively if the function has no reliance on the state of A, you can make the function static (public static string Function)

string result = A.Function("Hello world");

这篇关于无法在Web Control的Page_Load事件中访问Public方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆