简体   繁体   中英

how can i change the repeater's label value from code behind

i have the following code Default.aspx

 <asp:Repeater ID="rpt" runat="server" >

 <ItemTemplate>


  <asp:Label ID="lbllat" runat="server" Text='<%#Eval("LAT")%>'></asp:Label>

  <asp:Label ID="lbllon" runat="server" Text='<%#Eval("LON")%>'></asp:Label>

  <asp:Label ID="lbladdress" runat="server"></asp:Label>

  </ItemTemplate>

   </asp:Repeater>

Default.aspx.cs

DataSet ds = new DataSet();
            ds = cls.ReturnDataSet("fetch_data)",
                 new SqlParameter("@Field", "*"),
                 new SqlParameter("@TblNm", "gps_data"));


            rpt.DataSource = ds;
            rpt.DataBind();


     for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            String address = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + ds.Tables[0].Rows[0]["LAT"].ToString() + "," + ds.Tables[0].Rows[0]["LON"].ToString() + "&sensor=false";
            var json = new WebClient().DownloadString(address);
            String formatted_address = Regex.Match(json, @"(?s)""formatted_address""\s*:\s*""(.+?)""").Groups[1].Value;

            foreach (RepeaterItem item in rpt.Items)
            {
                Label lab = item.FindControl("lbladdress") as Label;
                lab.Text = formatted_address.ToString();
            }
        }

From above code i am able to fetch latitude and longitude but after that when i fetch the address from latitude and longitude then it will fetch all the address of different latitude and longitude but it is set address of last latitude and longitude address.

so i am seeing the same address on all records instead of different addresses.

how can i set address according to their latitude and longitude .?

You should modify your code in this way:

Before

String address = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + ds.Tables[0].Rows[0]["LAT"].ToString() + "," + ds.Tables[0].Rows[0]["LON"].ToString() + "&sensor=false";

foreach (RepeaterItem item in rpt.Items)
{
    Label lab = item.FindControl("lbladdress") as Label;
    lab.Text = formatted_address.ToString();
}

After

String address = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + ds.Tables[0].Rows[i]["LAT"].ToString() + "," + ds.Tables[0].Rows[i]["LON"].ToString() + "&sensor=false";

RepeaterItem item = rpt.Items[i];
Label lab = item.FindControl("lbladdress") as Label;
lab.Text = formatted_address.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM