这是我的 ASP 网页控件
<a id="geoLocation" class="geolocate box-outline active full">
<div>
<div class="icon"></div>
<div class="header">
Locate Me Now
<asp:Label id="latitude" runat="server" ClientIdMode="Static"></asp:Label><br />
<asp:Label id="longitude" runat="server" ClientIdMode="Static"></asp:Label>
<div style="display:none">
<asp:Button ID="Button2" runat="server" Text="Button" ClientIdMode="Static" />
</div>
</div>
</div>
</a>
我正在尝试执行回传,以便能够在我打算隐藏的代码中访问“经度”和“纬度”的值。
这是我正在使用的 Javascipt 中的回发...我做错了什么?
function showPosition(position) {
// x.innerHTML = "Latitude: " + position.coords.latitude +
// "<br>Longitude: " + position.coords.longitude;
document.getElementById('latitude').innerHTML = position.coords.latitude;
document.getElementById('longitude').innerHTML = position.coords.longitude;
_doPostBack('#geoLocation', '');
}
请您参考如下方法:
首先,确保在 __doPostBack 的开头有两个下划线。
其次,__doPostBack 函数只有在页面上至少有一个控件调用 Page.RegisterPostBackScript() 时才可用。不幸的是,此方法是 System.Web 的内部方法。
作为解决方法,您的控件可以覆盖 Render 并调用 GetPostBackEventReference ,它将为您调用 Page.RegisterPostBackScript() 并返回一些可以忽略的 JavaScript:
protected override void Render(HtmlTextWriter writer)
{
this.Page.ClientScript.GetPostBackEventReference(new PostBackOptions(this));
base.Render(writer);
}
第三(回答您还没有问过的问题),设置 Label 控件的 innerHTML 属性不会使值对服务器可用回发。您应该改为使用 HiddenField 控件并在 JavaScript 中设置其 value 属性。
