Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

ASP.NET Website - How to access label ID names within a 'repeater', in my .cs file

I appreciate this is a common occurrence, however I've tried some of the solutions and none of worked thus far. My project is a 'Web Site' within Visual Studio, thus I don't have the option to 'Right Click and Crete new application'.

The labelID names from my aspx file cannot be read in the corresponding .cs file - flags up with syntax errors.

<body>

<form id="form1" runat="server">
    <asp:TextBox ID="txtCity" runat="server" Text=""  />
    <asp:Button Text="Get Weather Forecast" runat="server" OnClick="GetWeatherInfo" Width="207px" />
    <hr />
    <asp:Repeater ID="Repeater_weatherReports" runat="server">
        <ItemTemplate>
            <table id="tblWeather" runat="server" border="0" visible="false">
                <tr>
                    <th>
                        Weather Info
                    </th>
                </tr>
                <tr>
                    <td>
                        <asp:Label runat="server" ID="lblCity_Country" 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>


</form>

</body> </html>

c# code:

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

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

int i = 0; foreach (List list in weatherinfo.list) {

lblCity_Country = weatherinfo.city.name;
//lblDescription.Text = weatherinfo.list[0].weather[0].description;


Label_min = string.Format("{0}", Math.Round(weatherinfo.list[i].main.temp_min, 1));
Label_max = string.Format("{0}", Math.Round(weatherinfo.list[i].main.temp_max, 1));
Label_humidity = weatherinfo.list[i].main.humidity.ToString();
tblWeather.Visible = true;



i++;

}

So im trying to display the contents of the json class multiple times, using the same label names. I have my loop in the .cs code where im looping over the list class, but can't figure out how to deal with the label issues.

Any help would be much appreciated, thanks

1 Answer

James Churchill
STAFF
James Churchill
Treehouse Teacher

Derek,

As you've discovered, you can't address controls in your Repeater control template directly from code. Your choices are to use binding expressions or to handle one of the Repeater control events.

For more information about the Repeater server control, see: https://msdn.microsoft.com/en-us/library/x8f2zez5(v=vs.85).aspx

For more information about the Repeater ItemDataBound event, see: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.85).aspx

Thanks ~James

Thanks James, I did sort of get it working in the end, I'll be sure to read the links you provided.