简体   繁体   English

如何在ASPX页面中引用另一个对象属性

[英]How to reference another objects property in aspx page

I have a class, which is not present in the code behind, I have declared a property, say "Name". 我有一个类,该类在后面的代码中不存在,我声明了一个属性,例如“ Name”。 Now I want to access this property "Name" in the aspx page. 现在,我要在aspx页面中访问此属性“名称”。

I know we can do that by creating another property or a string variable or a hidden field in the code behind page and setting the "Name" properties value to it and accessing this new property/variable/ hidden field using <%= %> in the aspx page. 我知道我们可以做到这一点,方法是在页面后面的代码中创建另一个属性,字符串变量或隐藏字段,并为其设置“名称”属性值,然后在其中使用<%=%>访问此新属性/变量/隐藏字段aspx页面。

But I don't want to go by the above approach. 但我不想采用上述方法。

So is there any other way. 还有其他方法吗?

You could add a property on your code behind which will hold a reference to your class: 您可以在代码后添加一个属性,该属性将包含对您的类的引用:

public SomeClass MyClass 
{
    get
    {
        // return an instance of SomeClass here
        return new SomeClass();
    }
}

and then in the aspx page you could access this property: 然后在aspx页面中,您可以访问此属性:

<%= MyClass.SomeProperty %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM