如何从后面的代码更改转发器的标签值

时间:2015-04-14 14:43:29

标签: c# asp.net google-maps repeater asprepeater

我有以下代码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();
            }
        }

从上面的代码我可以获取纬度和经度,但之后当我从纬度和经度获取地址时,它将获取不同纬度和经度的所有地址,但它是最后纬度和经度地址的设置地址。

所以我在所有记录上看到相同的地址而不是不同的地址。

如何根据纬度和经度设置地址。?

1 个答案:

答案 0 :(得分:2)

您应该以这种方式修改代码:

<强>之前

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();
}

<强>后

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();