Accessing variable declared in aspx.cs in aspx or variable declared in ascx.cs ascx page

放肆的年华 提交于 2019-12-01 16:12:30

问题


I was asked a question in an interview, weather we can access a publically declared variable which is been declared in aspx.cs or ascx.cs page in aspx or ascx page respectively.


回答1:


Yes, As far as it's Publicly declared at Page Level, you can access it . for writing <%= Variable %> for computation and <% variable %> for binding <%# variable %>




回答2:


Yes you can, In ASPX page you can do:

<%=yourVariable%>

If you have defined in your code behind file .cs file as:

public string yourVariable;

If you want to use it in a span in aspx page then:

<span> <%= yourVariable %> </span>

You may see: Accessing CodeBehind Variables or C# Variables, Methods in ASPX Page in ASP.Net




回答3:


in similar case, anyone helps

in aspx file:

 <%= SessionLengthMinutes %> minute(s), <%=Session["name"] %>

in 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.

thanks



来源:https://stackoverflow.com/questions/13852946/accessing-variable-declared-in-aspx-cs-in-aspx-or-variable-declared-in-ascx-cs-a

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