Skip to main content
 首页 » 编程设计

c#之为什么我不能在 .aspx 中绑定(bind)一个属性,而它在 .xaml 中是可行的

2024年02月23日15mfrbuaa

我得到的错误是:

The name 'strTitle'(name of propert) does not exist in the current context

为了清楚起见,我将展示代码:

银光:

  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 中绑定(bind)它:

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

尝试绑定(bind) HTML 控件:

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

编辑:@Prisoner 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(..) 
      { 
           ...... 
       } 
} 

那么我们的意思是我将直接在 _Default 类中创建属性或在默认类中创建 currentWeatherCondition 的对象,然后将其绑定(bind)/渲染到控件。

请您参考如下方法:

我必须查看您的其余代码,但我猜“strTitle”值仅存在于 RMSMedia block 的范围内。您必须(然后)以四种方式之一将该值公开为公共(public)属性(property):

  1. 来自页面的公共(public)属性。
  2. 来自 RMSMedia 对象的公共(public)属性。
  3. 为容器编写数据绑定(bind)函数。
  4. 直接设置值

以防万一你不知道那是什么意思......

(1) 来自页面的公共(public)属性。

public String Title { get; set; } 

然后将 strTitle 设置为 Title 和 <%=Title %>。

(2) 来自 RMSMedia 对象的公共(public)属性。

然后设置myData.strTitle <%=myData.strTitle %>。

(3) 为容器编写数据绑定(bind)函数。

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; 

更新:
有很多选择,所以实际上,您只需选择一个。尽管它与所讨论的控件无关,但我在上面添加了第三个,只是作为“额外”来显示在 ASP.NET 中绑定(bind)的其他方法。 虽然第 4 个选项是最明显的答案,因为您直接设置了 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 对象,但我通常将其保留用于对象集合(另外,人们告诉我避免使用它,因为它很慢……我猜它很慢是因为它必须在后期绑定(bind)的同时使用反射来暴露属性。)但是,您当然可以使用它。

最后,上面的答案都没有真正讨论哪个选项是正确的选择。老实说,无论何时您使用间接运算符 (<% %>),您都在使用后期绑定(bind)(即坏的)。现在人们会告诉你 ASP.NET(现在)针对后期绑定(bind)进行了优化,所以没关系......但显然它仍然是后期绑定(bind)......所以如果你“可以”这样做,直接设置值或使用数据-绑定(bind)选项。

希望对您有所帮助!