簡體   English   中英

從后面的代碼中獲取子 class 中存在的變量值並在 aspx 頁面中使用

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

** 來自.aspx.cs 文件的代碼 **

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;
    }
}

我想使用變量 variable2 in.aspx 頁面作為

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

嘗試使用

  • <%#變量2%>
  • this.variable2

但是出現錯誤

class name2 是浮動的。 如果您有它的實例 class name1,您可以定義一個屬性並使用該變量。

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;
  }
 }

然后你可以在你的視圖中像這樣使用它 <%= MyProperty%>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM