Metzer
Metzer

Reputation: 241

ASP.NET Website - .cs file cannot read the labels from .aspx 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.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    </head>


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

A strange thing is, I can't even see the labelIDs if I click on the 'Design' view. See screenshot. Am I supposed to see the labelIDs here?

enter image description here

Any help will be much appreciated, Thanks

Edit - heres my c# code. Must I loop over RepeatItems somehow?

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

Upvotes: 0

Views: 1192

Answers (2)

Amit Kumar Singh
Amit Kumar Singh

Reputation: 4475

For starters, you can mark table visible="true". Then, you will be able to see the repeater atleast in design view.

protected void Repeater_weatherReports_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
{
Label lblmin = (Label)item.FindControl("Label_min");
Label lblmax = (Label)item.FindControl("Label_max");
//.........and you can set of get values here.
}
}

In your aspx page, in the line where asp:Repeater is defined, you can add OnItemDataBound="Repeater_weatherReports_ItemDataBound", it will start working.

Upvotes: 1

Ali Reza Pooneh
Ali Reza Pooneh

Reputation: 126

You can not access direct to label that is inside of repeater or gridview or other data binding controls. You can use events of repeater to access them. e.x:

<asp:repeater onitemcreated="methodnameToCall1" onitemdatabind="methodnameToCall2" >

and find labels by findcontrol method in c#:

void methodnameToCall1(object s,typeofitemcreatedEventsArgs e){

if(e.itemtype == dataitemtype){
var lbl = e.item.findcontrol("lblId") as Label;

}
}

It is semi code, because wrote in stack android app! Excuseme!

Upvotes: 1

Related Questions