ASP.Net - Repeater / DataBinding的问题

时间:2017-08-11 12:12:03

标签: c# asp.net

我试图在我的应用程序中绑定labelIDs。我无法让它发挥作用。我确定该名称是否需要与.cs文件中的名称相匹配?

.aspx文件中的代码:

    <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") %>' />&nbsp;
                        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>

我的C#代码:

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

我收到的错误消息是

  

&#39;类型&#39; System.Web.HttpException&#39;的例外情况发生在   System.Web.dll但未在用户代码中处理   信息:DataBinding:&#39; _Default + List&#39;不包含属性   名为&#39; city&#39;。&#39;

这是在行

上发生的
<asp:Label runat="server" ID="lblCity_Country" Text='<%# Eval("city") %>' />&nbsp;

我以前没有处理数据绑定/使用Repeater。任何帮助将不胜感激,

由于

1 个答案:

答案 0 :(得分:2)

看起来形成了你的Eval不正确的错误。

 <asp:Label runat="server" ID="lblCity_Country" Text='<%# Eval("city") %>' />

您正在使用weatherinfo.list

绑定数据
     Repeater_weatherReports.DataSource = weatherinfo.list;

该列表不包含城市属性,但包含天气信息。