Reputation: 241
I'm trying to bind my labelIDs
in my application. I cannot get it to work. I'm sure sure if the name needs to match the name in the .cs file?
My code in the .aspx file:
<asp:Repeater ID="Repeater_weatherReports" runat="server" onitemcommand="reptrData_ItemCommand">
<ItemTemplate>
<table id="tblWeather" border="0" visible="true">
<tr>
<th>
Weather Info
</th>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblCity_Country" Text='<%# Eval("city") %>' />
humidity:<asp:Label runat="server" ID="Label_humidity" Text='<%# Eval("main.humidity") %>' />
</td>
</tr>
<tr>
<td>
min:<asp:Label runat="server" ID="Label_min" Text='<%# Eval("main.temp_min") %>' />
max:<asp:Label runat="server" ID="Label_max" Text='<%# Eval("main.temp_max") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
My C# Code:
protected void reptrData_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Label lblCity = e.Item.FindControl("lblCity_Country") as Label;
city_name = lblCity.Text;
Label lblHumidity = e.Item.FindControl("Label_humidity") as Label;
humidity = lblHumidity.Text;
Label LblMin = e.Item.FindControl("Label_min") as Label;
humidity = lblHumidity.Text;
Label LblMax = e.Item.FindControl("Label_max") as Label;
temp_max = lblHumidity.Text;
}
protected void GetWeatherInfo(object sender, EventArgs e)
{
string appID = "hidden";
//string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&units=metric&cnt=2&APPID={1}",txtCity.Text,appID);
string url = string.Format("http://api.openweathermap.org/data/2.5/forecast?q={0},us&units=metric&cnt=5&APPID={1}", txtCity.Text, appID);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
JavaScriptSerializer serializer = new JavaScriptSerializer();
WeatherInfo weatherinfo = serializer.Deserialize<WeatherInfo>(json);
Repeater_weatherReports.DataSource = weatherinfo.list;
Repeater_weatherReports.DataBind();
int i = 0;
foreach (List list in weatherinfo.list)
{
city_name = weatherinfo.city.name;
//lblDescription.Text = weatherinfo.list[0].weather[0].description;
temp_min = string.Format("{0}", Math.Round(weatherinfo.list[i].main.temp_min, 1));
temp_max = string.Format("{0}", Math.Round(weatherinfo.list[i].main.temp_max, 1));
humidity = weatherinfo.list[i].main.humidity.ToString();
// tblWeather.Visible = true;
i++;
}
}
The error message I am getting is
'An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: DataBinding: '_Default+List' does not contain a property with the name 'city'.'
This is occurring on the line
<asp:Label runat="server" ID="lblCity_Country" Text='<%# Eval("city") %>' />
I've not dealt with data binding / using the Repeater
before. Any help would be appreciated,
Thanks
Upvotes: 1
Views: 760
Reputation: 3751
Looks like form the error that your Eval is not right.
<asp:Label runat="server" ID="lblCity_Country" Text='<%# Eval("city") %>' />
You are binding the data with weatherinfo.list
Repeater_weatherReports.DataSource = weatherinfo.list;
The list does not contain city property, but the weather info.
Upvotes: 2