简体   繁体   中英

Get variable value present in sub class from code behind and use in aspx page

** code from.aspx.cs file **

namespace namespace1
{
    public partial class name1 : System.Web.UI.Page
    {
        public string variable1;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
    public class name2
    {
        public string variable2;
    }
}

I want to use variable variable2 in.aspx page as

if(<%#variable2%> == "Some Value"){
  // Do something
}
else{
  // Do something
}

Tried using

  • <%#variable2%>
  • this.variable2

But getting errors

The class name2 is floating. If you have an instance of it class name1 you can define a property and use that variable.

namespace namespace1
{
    public partial class name1 : System.Web.UI.Page
    {
      public string variable1;
      private name2 _instance = null;

      protected void Page_Load(object sender, EventArgs e)
     {
        _instance = new name2 ();
     }


    public string MyProperty {
      get{return _instance.variable2;}
    }
   }

  public class name2
  {
      public string variable2;
  }
 }

Then you can use it like this in your view <%= MyProperty%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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