简体   繁体   English

C#ASP.Net-在我的.cs代码中尝试在中继器中绑定标签时出现的问题

[英]C# ASP.Net - Issue when trying to bind labels within a Repeater, in my .cs code

I am trying to get the labels in my .aspx file to work in my .cs file. 我试图在我的.aspx文件中获取标签以在我的.cs文件中工作。 I have created a method which I thought would bind the labelID s into string variables, as using onitemcommand in my repeater. 我创建了一个方法,我认为可以将labelID绑定到字符串变量中,就像在转发器中使用onitemcommand

Repeater code: 中继器代码:

    <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("main.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# Code: C#代码:

public partial class _Default : System.Web.UI.Page
{



     string city_name;
        string temp_min;
        string temp_max;
        string humidity;


        protected void Page_Load(object sender, EventArgs e)
        {

        }



        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.list[i].main.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++;
                }
    }

When my code runs, I get a NullReferenceException on the line 当我的代码运行时,我在行上得到一个NullReferenceException

city_name = weatherinfo.list[i].main.city.name;

It seems like my reptrData_ItemCommand method is not being invoked from the .aspx file (from the OnItemCommand ) 这似乎是我的reptrData_ItemCommand方法没有从.aspx文件调用(从OnItemCommand

What am I doing wrong? 我究竟做错了什么?

Thank you 谢谢

As Koby explained, You can use a foreach element to get details 正如Koby解释的那样,您可以使用foreach元素获取详细信息

But in your case, you are getting null reference exception in below line 但是在您的情况下,您在下面的行中得到空引用异常

city_name = weatherinfo.list[i].main.city.name;

This error will also come because on the list at index 0 has either main is null or city is null or name is null 因为在索引0的列表上main为null或city为null或name为null,也会出现此错误。

Use the below condition will null check 使用以下条件将使检查无效

foreach (List list in weatherinfo.list)
{
    if (list.main != null && list.main.city != null && list.main.city.name != null ) 
    {
       city_name = list.main.city.name;
    }
    ....
}

Youv'e got foreach and for loop usage mixed up. 您将foreachfor循环用法混为一谈。 When you use foreach , you don't use the i iterator, you use the current list as an iterator: 使用foreach ,不使用i迭代器,而是将当前list用作迭代器:

foreach (List list in weatherinfo.list)
{
    city_name = list.main.city.name; 
    ....
}

And you don't have to use the i iterator. 而且您不必使用i迭代器。 You can remove it from your code. 您可以将其从代码中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM