2

我正在使用 Visual Studio 2010 制作一个 asp.net 应用程序。我有一个文本字段和一个按钮,可以调用文本字段中字符串上的方法。

Enter your address here: <asp:TextBox ID="tb_address" runat="server" ></asp:TextBox>
<asp:Button Text="Submit" runat="server" onclick="GetLatLong" ></asp:Button>

在按钮的 C# 文件中,我有我的 GetLatLong 方法:

protected void GetLatLong(object sender, EventArgs e)
{
    String address = tb_address.Text;
    String query = "http://maps.googleapis.com/maps/api/geocode/xml?address=";
    address = address.Replace(" ", "+");
    query += address + "&sensor=false";
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(query);
    String lat = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
    String lon = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;
}

如何让我的 lat 和 lon 字符串显示在我的 html 页面上?

4

4 回答 4

3

使用<asp:Label />s。

<asp:Label ID="lblLat" runat="server" />
<asp:Label ID="lblLong" runat="server" />

String lat = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
String lon = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;
lblLat.Text = lat;
lblLong.Text = lon;
于 2012-08-29T17:44:11.387 回答
3

您必须创建用于显示结果的控件(您可以在设计模式下将它们添加到表单中,也可以在单击事件处理程序上动态添加它们)。让我们说 asp:Labels,然后将结果值分配给这些标签。

Label result1 = new Label();
result1.Text = lat;
this.Controls.Add(result1);

或者

在你的代码中有这个

<asp:Label ID='result1' runat='server' />

然后直接从后面的代码中赋值。

result1.Text = lat;
于 2012-08-29T17:45:19.623 回答
2

您可以包含一些Literal控件(或Label控件,或任意数量的其他页面元素)来保存值。控件如下所示:

<asp:Literal runat="server" ID="LatitudeOutput" />
<asp:Literal runat="server" ID="LongitudeOutput" />

您可以在代码隐藏中设置它们的值:

String lat = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
String lon = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;
LatitudeOutput.Text = lat;
LatitudeOutput.Text = lon;

在许多情况下,我个人更喜欢Literal控件,因为它们不会带来任何额外的标记。 Label例如,控件被包装在span标签中。但是,与许多事情一样,有很多方法可以做到这一点。

于 2012-08-29T17:46:48.023 回答
1

您必须在您的 aspx 页面上创建一个标签控件。将此添加到您要显示 lng 和 lat 的 aspx 页面

<asp:Label ID="lblLat" runat="server" />
<asp:Label ID="lblLng" runat="server" />

然后在你的代码后面

lblLat.Text = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
lblLng.Text = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;

您正在将标签的 Text 设置为通过 SelectSingleNode 调用获得的值。

于 2012-08-29T17:51:23.397 回答