0

So I have a table which looks as below. The data is populating correctly for one class object.

enter image description here

What I am trying to do is create 5 lots of this table, and apart from the table name, have everything else the same. So I somehow need have the label names in a loop so that multiple objects can populate

lblDescription[0]

lblDescription1

And same with the other labels.

Here is my C# code which is populating the single table currently.

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

So each list object is populating one table. But I need to have my table configuration set up dynamically, that that it can work for any number of 'list' objects.

I hope I have explained this well,

Any help would be appreciated,

Thanks

1
  • You can construct & bind data in <tr> <td> to display. Check repeater, datalist also. Commented Aug 11, 2017 at 3:38

1 Answer 1

1

You can use Data Controls. According to your question, I recommend Reapter. https://msdn.microsoft.com/zh-tw/library/zzx23804(v=vs.85).aspx

Here is a sample designed for your conditions.

<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>

Then at the code behind you can simply just assign your list to the Repeater

Repeater_weatherReports.DataSource = weatherinfo.list;
Repeater_weatherReports.DataBind();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. The only issue I have is with the new label names for some reason, my .cs file cannot see the label IDs from the .aspx file. Before i could use <label_name>.Text in my .cs file, but now the label ID names are not recognised. Any idea why this is?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.