2017-08-11 5 views
0

私の.aspxファイルのラベルを自分の.csファイルで使用しようとしています。私はlabelIDを文字列変数に束縛すると思っていたメソッドを作成しました。私のリピータではonitemcommandを使用しています。C#ASP.Net - リピーター内のラベルを.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++; 
       } 
    } 

reptrData_ItemCommand方法があるように私のコードが実行されると、私はNullReferenceException

ライン上
city_name = weatherinfo.list[i].main.city.name; 

を取得するに思え.aspxファイルから呼び出されない(OnItemCommandから)

私は間違っていますか?

+0

[NullReferenceExceptionとは何か、それを修正するにはどうすればいいですか?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix -it) –

答えて

2

Kobyが説明したように、あなたは詳細

を取得するためにforeach要素を使用することができますしかし、あなたの場合には、あなたは下の行にnull参照の例外に

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

を取得しているこのエラーは、上の理由来ますインデックス0のリストは、メインがnullの場合しているいずれか、または都市がnullであるか、名前が

使用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 (List list in weatherinfo.list) 
{ 
    city_name = list.main.city.name; 
    .... 
} 

そして、あなたはiイテレータを使用する必要はありません:あなたはforeachを使用する場合 、あなたはイテレータとして現在listを使用し、iイテレータを使用しないでください。コードから削除することができます。

+0

こんにちはKobyはい良いスポット。おかげで覚えています – Metzer

関連する問題