2017-08-11 14 views
0

我試圖讓我的.aspx文件中的標籤在我的.cs文件中工作。我創建了一個我認爲將labelID s綁定到字符串變量的方法,如在我的轉發器中使用onitemcommandC#ASP.Net - 嘗試在Repeater中綁定標籤時發生的問題,在我的.cs代碼中

直放站代碼:

<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#代碼:

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

當我的代碼運行,我上線獲得NullReferenceException

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

好像我reptrData_ItemCommand方法不從.aspx文件調用(從OnItemCommand

我在做什麼錯?

謝謝

+0

可能重複[什麼是NullReferenceException,以及如何解決它?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix - 它) –

回答

2

由於Koby解釋的,可以使用foreach元素來獲得細節

但在你的情況,你得到空引用異常下面一行

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

此錯誤也隨之到來,因爲在名單索引0已任主要爲空或城市爲null或者名稱爲空

使用以下條件將空檢查

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

嗨Yogesh謝謝你,得到它的工作:-) – Metzer

1

Youv'e了foreachfor循環使用混合起來。 當您使用foreach,您不使用i迭代器,你可以使用目前的list作爲一個迭代器:

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

而且你不必使用i迭代器。你可以從你的代碼中刪除它。

+0

嗨Koby是好點。感謝你會記得 – Metzer

相關問題