【问题标题】:ASP.Net - Issues with Repeater / DataBindingASP.Net - 中继器/数据绑定问题
【发布时间】:2017-08-11 12:12:02
【问题描述】:

我正在尝试在我的应用程序中绑定我的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++;
        }
}

我得到的错误信息是

'System.Web.HttpException' 类型的异常发生在 System.Web.dll 但未在用户代码中处理 附加 信息:DataBinding:'_Default+List' 不包含属性 名称为“城市”。'

这是发生就行了

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

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

谢谢

【问题讨论】:

  • 错误消息是不言自明的。可能您的DataSource 在这部分Eval("city") 中没有名为City 的列。可能是main.city 或其他。
  • 嗨 S.Akbari,你说得对,我错过了城市的实际“路径”。我已经添加了这是现在。但是,我在这些变量分配上得到了空值: city_name = weatherinfo.list[i].main.city.name; 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));如何调用我的“reptrData_ItemCommand”方法以便绑定每个标签?

标签: c# asp.net


【解决方案1】:

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

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

您正在将数据与 weatherinfo.list 绑定

     Repeater_weatherReports.DataSource = weatherinfo.list;

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

【讨论】:

  • 城市字段在我的 WeatherInfo 类中 public class WeatherInfo { public string cod { get;放; } 公共双重消息 { 获取;放; } 公共 int cnt { 获取;放; } 公共 IList 列表 { 获取;放; } 公共城市城市 { 得到;放;所以我需要绑定父类而不是绑定列表列表?对吗?
  • 您可以创建一个受保护的变量并将 td 值与之绑定。正如整个表格的样子,城市价值将保持不变。如果您有列表列表,那么最好与父对象绑定。
  • 感谢您提供的信息,我遇到的最后一个问题。实际绑定不起作用。我必须将变量“city_name”等传递到我的“reptrData_ItemCommand”方法中吗?
  • 请用您更新的代码发布另一个问题。需要看代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
相关资源
最近更新 更多