我如何从后面的代码更改转发器的标签值
how can i change the repeater's label value from code behind
我有以下代码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();
}
}
从上面的代码我可以获取纬度和经度但是之后当我从纬度和经度获取地址时它会获取所有不同纬度和经度的地址但它是最后一个经纬度的设置地址地址。
所以我在所有记录上看到相同的地址,而不是不同的地址。
如何根据经纬度设置地址?
你应该这样修改你的代码:
之前
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();
我有以下代码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();
}
}
从上面的代码我可以获取纬度和经度但是之后当我从纬度和经度获取地址时它会获取所有不同纬度和经度的地址但它是最后一个经纬度的设置地址地址。
所以我在所有记录上看到相同的地址,而不是不同的地址。
如何根据经纬度设置地址?
你应该这样修改你的代码:
之前
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();