2017-08-10 30 views
0

因此,我有一張如下所示的表格。數據正在爲一個類對象正確填充。ASP.Net Web窗體 - 嘗試在.aspx文件中複製表格

enter image description here

我所試圖做的就是創建5手這個表,而除了表名,擁有一切相同。所以,我莫名其妙地需要有一個循環的標籤名稱,以便多個對象可以填充

lblDescription [0]

lblDescription 1

,並與其他標籤相同。

這是我的C#代碼,它是目前填充單個表。

 JavaScriptSerializer serializer = new JavaScriptSerializer(); 

     WeatherInfo weatherinfo = serializer.Deserialize<WeatherInfo>(json); 

     int i = 0; 
     foreach (List list in weatherinfo.list) 
     { 
      lblCity_Country.Text = weatherinfo.city.name; 
      //lblDescription.Text = weatherinfo.list[0].weather[0].description; 

      lblTempMin.Text = string.Format("{0}", Math.Round(weatherinfo.list[i].main.temp_min, 1)); 
      lblTempMax.Text = string.Format("{0}", Math.Round(weatherinfo.list[i].main.temp_max, 1)); 

      lblHumidity.Text = weatherinfo.list[i].main.humidity.ToString(); 
      tblWeather.Visible = true; 



      i++; 
     } 

因此,每個列表對象正在填充一個表。但我需要動態設置我的表格配置,以便它可以適用於任何數量的「列表」對象。

我希望我已經解釋了這口井,

任何幫助,將不勝感激,

感謝

+0

您可以構建和在​​顯示數據綁定。檢查中繼器,datalist也。 –

回答

1

您可以使用數據控件。根據你的問題,我建議Reapter。 https://msdn.microsoft.com/zh-tw/library/zzx23804(v=vs.85).aspx

以下是針對您的條件設計的樣本。

<asp:Repeater ID="Repeater_weatherReports" runat="server"> 
    <ItemTemplate> 
     <table> 
      <tr> 
       <th> 
        Weather Info 
       </th> 
      </tr> 
      <tr> 
       <td> 
        <asp:Label runat="server" ID="Label_city" Text='<%# Eval("city.name") %>' />&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> 

然後在後面的代碼,你可以簡單地只是你的列表分配到Repeater

Repeater_weatherReports.DataSource = weatherinfo.list; 
Repeater_weatherReports.DataBind(); 
+0

感謝您的幫助。我遇到的唯一問題是出於某種原因使用新的標籤名稱,我的.cs文件無法從.aspx文件中看到標籤ID。 在我可以在我的.cs文件中使用 .Text之前,現在標籤ID名稱無法識別。任何想法爲什麼這是? – Metzer