开发者

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

The Error I am getting is:

The name 'strTitle'(name of propert) does not exist i开发者_开发问答n the current context

To make it very clear I will show the code:

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

Now I am able to bind this in my XAML like below:

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

But this is not working in 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")
                      };

Trying to bind with HTML Control:

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

Edit:@Prisoner ZERO

My code structure is as below:

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(..)
      {
           ......
       }
}

So do we mean I will directly create the prpoerty in _Default class or create object of currentWeatherCondition in default class and then bind/render it to the control.


I would have to see the rest of your code, but I would guess the value "strTitle" exists solely within the scope of the RMSMedia block. You must (then) expose that value as a public property in one of four ways:

  1. From a public property of the page.
  2. From a public property of the RMSMedia object.
  3. Write a databinding function for the container.
  4. Set the value directly

Just in case you don't know what that means...

(1) From a public property of the page.

public String Title { get; set; }

Then set strTitle to Title and <%=Title %>.

(2) From a public property of the RMSMedia object.

Then set myData.strTitle thusly <%=myData.strTitle %>.

(3) Write a databinding function for the container.

myContainer.DataBinding += new EventHandler(myContainer_DataBinding);

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

To call this last one you would use the following: myContainer.DataBind();

(4) Set the value directly within your function

lbWindCondition.Text = myData.strTitle;

UPDATE:
There are a bunch of options, so really, you just choose one. Even though it has nothing to do with the control in question, I added the 3rd one above just as an "extra" to show other ways to bind in ASP.NET. The 4th option is the most obvious answer though because you set the Text value directly (meaning, without indirection).

So do we mean I will directly create the property in _Default class?
Yes...if you like that option.

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

Or do we create object of currentWeatherCondition in default class and then it to the control?
Yes...if you like that option.

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

You can also use the DataBinder object, but I usually reserve that for collections of objects (plus folks tell me to avoid it because it is slow...my guess is that it is slow because it must use reflection alongside late-binding to expose the property.). However, you certainly can use it.

Lastly, none of the answers above really discuss which option is the RIGHT choice. In all honesty, anytime you use the indirection operators (<% %>) you utilize late-binding (which is bad). Now folks will tell you that ASP.NET is (now) optimized for late-binding so it's okay...but obviously it's still late binding...so if you "can" do so, set the value directly or use the data-binding option.

I hope this helps!


The Label ASP.NET Control does not have DataSource Property so that it is not possible to directly databind it against anonymous object. What you need to do is

1) Create a page container property which would hold the myData collection 2) Bind the Label against that property. =>

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

I hope that this helps :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜