【问题标题】:how can i change the repeater's label value from code behind我如何从后面的代码中更改中继器的标签值
【发布时间】:2015-04-14 23:07:45
【问题描述】:

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

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

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

如何根据他们的经纬度设置地址?

【问题讨论】:

  • 那是因为你已经将它编码为每次在 foreach 循环中你正在覆盖以前的值时都这样做。你需要做这样的事情 Label lab = new Label() 给它分配一个 Id ,然后分配它是透视文本.. 等等.. 使用调试器并单步执行代码,您将确切地看到这里发生了什么..
  • 我也试过这个Label lab = new Label(),但所有记录中的地址仍然相同。

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


【解决方案1】:

你应该这样修改你的代码:

之前

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多