I have the following two pages:
我有以下两页:
- Default.aspx
- Default.aspx.cs
How do I access variables in the code-behind file (Default.aspx.cs) from my embedded code in (Default.aspx) with the <% %>
syntax?
如何使用<%%>语法从(Default.aspx)中的嵌入代码访问代码隐藏文件(Default.aspx.cs)中的变量?
3 个解决方案
#1
15
Any public
or protected
(but not private
, the "page" itself inherits from the code-behind Page
class) class-level member can be accessed in this way. For example, if your code-behind class has a property:
可以通过这种方式访问任何公共或受保护(但不是私有的,“页面”本身继承自代码隐藏的Page类)类级别成员。例如,如果您的代码隐藏类具有属性:
protected string SomeValue { get; set; }
Then in your aspx code you can refer to it:
然后在您的aspx代码中,您可以参考它:
<% =SomeValue %>
#2
0
Simply reference them as if they are part of the current class.
只需引用它们就好像它们是当前类的一部分一样。
<%= this.Foo %>
#3
0
If you don't specify the access modifier for the variable the default is private and hence you cannot access it inside your page. It works for public, protected and friend. I prefer to use protected variables than public ones.
如果没有为变量指定访问修饰符,则默认为私有,因此您无法在页面内访问它。它适用于公众,受保护和朋友。我更喜欢使用受保护变量而不是公共变量。
#1
15
Any public
or protected
(but not private
, the "page" itself inherits from the code-behind Page
class) class-level member can be accessed in this way. For example, if your code-behind class has a property:
可以通过这种方式访问任何公共或受保护(但不是私有的,“页面”本身继承自代码隐藏的Page类)类级别成员。例如,如果您的代码隐藏类具有属性:
protected string SomeValue { get; set; }
Then in your aspx code you can refer to it:
然后在您的aspx代码中,您可以参考它:
<% =SomeValue %>
#2
0
Simply reference them as if they are part of the current class.
只需引用它们就好像它们是当前类的一部分一样。
<%= this.Foo %>
#3
0
If you don't specify the access modifier for the variable the default is private and hence you cannot access it inside your page. It works for public, protected and friend. I prefer to use protected variables than public ones.
如果没有为变量指定访问修饰符,则默认为私有,因此您无法在页面内访问它。它适用于公众,受保护和朋友。我更喜欢使用受保护变量而不是公共变量。