0

嗨,提前致谢,

我在 c# 中填充 asp:label 并显示一些纬度/经度范围边界,然后进行验证。

ASP:

<script type="text/javascript" language="javascript">
window.bound = "<%= this.javaSerial.Serialize(this.rangeLat_Bounds) %>";
//alert(bound);
</script>
.
.
.
<asp:TabPanel ID="TabPanelLatLongCoord" runat="server" HeaderText="Lat/Long" TabIndex="1">
<ContentTemplate>
<div class="coord-btn btnLat_Long">
<asp:Button ID="btnDd" runat="server" Text="D.d" CssClass="coordinate-btn black" OnClick="Dd_Click" TabIndex="1" />
<asp:Button ID="btnDMS" runat="server" Text="DMS" CssClass="coordinate-btn black" OnClick="DMS_Click" TabIndex="1" />
<asp:Button ID="btnDMm" runat="server" Text="DM.m" CssClass="coordinate-btn black" OnClick="DMm_Click" TabIndex="1" />
</div>
<br />
<ul>
<li><span class="container-label">
<asp:Label ID="lblLat" runat="server" Text="Latitude" TabIndex="1"></asp:Label>
                            </span>
<asp:TextBox ID="txtLat" runat="server" CssClass="container-input-field" SkinID="NumTextUpDown" TabIndex="1"></asp:TextBox>
<asp:Label ID="rangeSetUp_Lat" runat="server" Text=""></asp:Label>
<asp:CustomValidator ID="Lat_Validator" runat="server" ControlToValidate="txtLat"
                                    CssClass="errorMessage" ToolTip="Wrong Latitude Format" ClientValidationFunction="validate_Lat" TabIndex="1">
                                    <img alt="" src="images/no.png" />
</asp:CustomValidator>
<asp:CustomValidator ID="RangeLat_CustomValidator" runat="server" ControlToValidate="txtLat"
                                    CssClass="errorMessage" ToolTip="Latitude Out Of Range" ClientValidationFunction="rangeLat_validator" TabIndex="1">
<img alt="" src="images/no.png" />
</asp:CustomValidator>                      
</li>
<li><span class="container-label">
<asp:Label ID="lblLon" runat="server" Text="Longitude" TabIndex="1"></asp:Label>
</span>
<asp:TextBox ID="txtLon" runat="server" CssClass="container-input-field" SkinID="NumTextUpDown"></asp:TextBox>
<asp:Label ID="rangeSetUp_Lon" runat="server" Text="" TabIndex="1"></asp:Label>
<asp:CustomValidator ID="Lon_Validator" runat="server" ControlToValidate="txtLon"
                                    CssClass="errorMessage" ToolTip="Wrong Longtitude Format" ClientValidationFunction="validate_Lon">
<img alt="" src="images/no.png" />
</asp:CustomValidator>

</li>
</ul>
</ContentTemplate>
</asp:TabPanel>

C#

protected void Dd_Click(object sender, EventArgs e)
{

.
.
.
Ranges range = new Ranges(rangeId);

String lowLat = parseOut(getCompassLat(range.LowLat));

String highLat = parseOut(getCompassLat(range.HighLat));

String rangeLat_Bounds = lowLat + " - " + highLat;

rangeSetUp_Lat.Text = rangeLat_Bounds;

JavaScriptSerializer js_serializer = new JavaScriptSerializer();

.
.
.
}

外部JS:

function rangeLat_validator(oSrc, args) {

//var bound = <%= this.javaSerial.Serialize(this.rangeLat_Bounds) %>;  

var input = args.Value;

alert(window.bound);

}

当用户单击不同的按钮时,它会将 asp:label 重新格式化为纬度和经度的不同格式。问题是我无法在 JS 中获取 rangeSetUp_Lat ID 值。

4

1 回答 1

1

尝试将序列化值用引号括起来:

var bound = "<%= this.javaSerial.Serialize(this.rangeLat_Bounds) %>";

如果您在此之后仍然有问题,您是否在控制台中收到任何错误?

编辑:

看起来 op 正在从外部 js 文件调用序列化代码。服务器端代码仅在主 html 文件中有效,因此将 Serialize 调用移动到主 html 中,并将其绑定到全局变量(如果您的 var 有一个命名空间会更好,全局变量在 js 中不好):

window.bound = "<%= this.javaSerial.Serialize(this.rangeLat_Bounds) %>";

然后bound从 js 函数访问,只需调用window.bound. 只需确保在 html 中分配绑定变量后调用 js。

于 2013-11-06T11:58:51.630 回答