簡體   English   中英

如何在按鈕按下事件期間獲取我創建的信息以顯示在我的html頁面上?

[英]How can I get information I have created during the button press event to display on my html page?

我正在使用visual studio 2010創建一個asp.net應用程序。我有一個文本字段和按鈕,在文本字段中調用String上的方法。

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 Strings顯示在我的html頁面上?

使用<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;

您必須創建用於顯示結果的控件(您可以在設計模式下將它們添加到表單中,也可以在click事件處理程序中動態添加它們)。 讓我們說asp:標簽,然后將結果值分配給那些標簽。

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

要么

在你的代碼中有這個

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

然后直接從后面的代碼中分配值。

result1.Text = lat;

您可以包含一些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標簽中。 但是,與許多事情一樣,有很多方法可以做到這一點。

您必須在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;

您正在將標簽的文本設置為使用SelectSingleNode調用獲得的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM