[英]Accessing variable declared in aspx.cs in aspx or variable declared in ascx.cs ascx page
我在一次采访中被问到一个问题,天气我们可以访问一个公开声明的变量,该变量分别在aspx或ascx页面的aspx.cs或ascx.cs页面中声明。
Yes you can, In ASPX page you can do: 是的,你可以,在ASPX页面你可以这样做:
<%=yourVariable%>
If you have defined in your code behind file .cs
file as: 如果您在文件
.cs
文件后面的代码中定义为:
public string yourVariable;
If you want to use it in a span in aspx page then: 如果你想在aspx页面的跨度中使用它,那么:
<span> <%= yourVariable %> </span>
You may see: Accessing CodeBehind Variables or C# Variables, Methods in ASPX Page in ASP.Net 您可能会看到: 在ASP.Net中访问ASPB中的CodeBehind变量或C#变量,方法
Yes, As far as it's Publicly declared at Page Level , you can access it . 是的,只要它在Page Level上公开声明,您就可以访问它。 for writing
<%= Variable %>
for computation and <% variable %>
for binding <%# variable %>
用于编写
<%= Variable %>
用于计算, <% variable %>
用于绑定<%# variable %>
in similar case, anyone helps 在类似情况下,任何人都有帮助
in aspx
file: 在
aspx
文件中:
<%= SessionLengthMinutes %> minute(s), <%=Session["name"] %>
in aspx.cs : 在aspx.cs中:
public int SessionLengthMinutes
{
get { return Session.Timeout; }
}
i have declared it as public, even though it was unable to access the aspx.cs
elements in aspx
file. 我已将其声明为public,即使它无法访问
aspx
文件中的aspx.cs
元素。
thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.