簡體   English   中英

為什么我不能在.aspx中綁定屬性,而在.xaml中是可行的

[英]Why can't I bind a property in .aspx while it is feasible in .xaml

我得到的錯誤是:

名稱'strTitle'(屬性名稱)在當前上下文中不存在

為了清楚說明,我將展示代碼:

Silverlight的:

  var myData =from xAudioinfo in xResponse.Descendants(ns + "DIDL-Lite").Elements(ns + "item")
  select new RMSMedia 
                      {
                         strTitle =((string)xFolderInfo.Element(dc + "title")).Trim(),                                                  
                         objectID = (int)xFolderInfo.Attribute("id")
                      };

現在我可以在我的XAML中綁定它,如下所示:

<TextBlock Text ="{Binding strTitle}" />

但是這在ASP.NET中不起作用。

ASP.NET

 var myData =from xAudioinfo in xResponse.Descendants(ns + "DIDL-Lite").Elements(ns + "item")
  select new RMSMedia 
                      {
                         strTitle =((string)xFolderInfo.Element(dc + "title")).Trim(),                                                  
                         objectID = (int)xFolderInfo.Attribute("id")
                      };

嘗試與HTML控件綁定:

<asp:Label ID="lbWindCondition" runat="server" Text=<%#strTitle %>></asp:Label>

編輯: @Prison ZERO

我的代碼結構如下:

public class currentWeatherCondition
        {
            public string condition { get; set; }
            public string temp { get; set; }
            public string imageURL { get; set; }
            public string humidity { get; set; }
            public string windCondition { get; set; }
         }

public partial class _Default : System.Web.UI.Page 
{
   protected void btnGetDetails_Click(object sender, EventArgs e)
    {
        try
        {
            var weatherXML = XDocument.Load(weatherURL);
            var weatherResult = from weatherDetail in weatherXML.Descendants("current_conditions")
                                select new currentWeatherCondition
                                {
                                    condition = ((string)weatherDetail.Element("condition").Attribute("data")).Trim(),
                                    temp = ((string)weatherDetail.Element("temp_c").Attribute("data")).Trim(),
                                    imageURL = ((string)weatherDetail.Element("icon").Attribute("data")).Trim(),
                                    humidity = ((string)weatherDetail.Element("humidity").Attribute("data")).Trim(),
                                    windCondition = ((string)weatherDetail.Element("wind_condition").Attribute("data")).Trim(),

                                };
            }
    catch(..)
      {
           ......
       }
}

我們這樣做的意思是我會直接建立在prpoerty _Default類或創建的對象currentWeatherCondition在默認類,然后綁定/使它對控制。

我必須看到你的其余代碼,但我猜測值“strTitle”僅存在於RMSMedia塊的范圍內。 您必須(然后)以四種方式之一將該值公開為公共屬性:

  1. 來自頁面的公共財產。
  2. 來自RMSMedia對象的公共屬性。
  3. 為容器編寫數據綁定函數。
  4. 直接設置值

萬一你不知道這意味着什么......

(1)來自頁面的公共財產。

public String Title { get; set; }

然后將strTitle設置為Title並<%= Title%>。

(2)來自RMSMedia對象的公共屬性。

然后設置myData.strTitle,因此<%= myData.strTitle%>。

(3)為容器編寫數據綁定功能。

myContainer.DataBinding += new EventHandler(myContainer_DataBinding);

protected void myContainer_DataBinding(object sender, EventArgs e)
{
     myContainer.DataSource = myData.strTitle;
}

要調用最后一個,您將使用以下內容: myContainer.DataBind();

(4)直接在函數中設置值

lbWindCondition.Text = myData.strTitle;

更新:
有很多選項,所以真的,你只需選擇一個。 盡管它與所討論的控件無關,但我在上面添加了第3個作為“額外”來展示在ASP.NET中綁定的其他方法。 第四個選項是最明顯的答案,因為您直接設置了Text值(意思是,沒有間接)。

那么我們是說我會直接在_Default類中創建屬性嗎?
是的......如果你喜歡那個選項。

<asp:Label ID="lbWindCondition" runat="server" Text=<%=Title %>></asp:Label>

或者我們在默認類中創建currentWeatherCondition的對象然后它到控件?
是的......如果你喜歡那個選項。

<asp:Label ID="lbWindCondition" runat="server" Text=<%#currentWeatherCondition.YourProperty %>></asp:Label>

你也可以使用DataBinder對象,但我通常會為對象集合保留它(加上人們告訴我要避免它,因為它很慢......我的猜測是它很慢,因為它必須使用反射和后期綁定來暴露財產。)。 但是,你當然可以使用它。

最后,上面的答案都沒有真正討論哪個選項是正確的選擇。 老實說,只要你使用間接操作符(<%%>),就可以使用后期綁定(這很糟糕)。 現在大家會告訴你ASP.NET已經(現在)針對后期綁定進行了優化,所以它沒關系...但顯然它仍然是后期綁定...所以如果你“可以”這樣做,直接設置值或使用數據 - 綁定選項。

我希望這有幫助!

Label ASP.NET控件沒有DataSource屬性,因此無法直接將其與匿名對象進行數據綁定。 你需要做的是

1)創建一個包含myData集合的頁面容器屬性2)將該Label綁定到該屬性。 =>

Text = '<%# DataBinder.Eval(ContainerObject, "strTitle"); %>'

我希望這個對你有用 :)

暫無
暫無

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

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