【发布时间】:2020-05-12 04:22:42
【问题描述】:
我在页面加载时的最终脚本标记中设置了一个变量,因为这是访问 DOM 所必需的(使用第 3 方库)。我想访问我的变量,但是通过 C#(ASP Web 应用程序)访问它时我得到“未定义”。
我怎样才能访问它?
如果我从 C# 运行以下命令:
ClientScript.RegisterStartupScript(GetType(), "hwa", "hello();", true);
我收到以下错误: https://i.stack.imgur.com/veiLB.png
我的hello函数是:
function hello() {
alert(mapData);
}
这个mapData 变量设置在正文末尾的脚本标记中,我的原始代码是在站点加载后调用 JS 函数,但是它通过 C# 显示为未定义,但如果我使用网络控制台。
完整脚本:
function createMap() {
mapData = L.map('map', {
center: [20.0, 5.0],
minZoom: 2,
zoom: 5
});
L.mapData = mapData;
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
subdomains: ['a', 'b', 'c']
}).addTo(L.mapData);
}
function createMarker(long, lat, type) {
alert(L.mapData);
L.marker([long, lat], { icon: L.icon({ iconUrl: 'Content/img/' + type + '.png', iconSize: [50, 60], iconAnchor: [22, 94], popupAnchor: [-3, -76], }) }).addTo(L.mapData);
}
function hello() {
alert(mapData);
}
【问题讨论】:
-
谢谢@Clint,抱歉我没有说明变量是一个对象。
-
@Clint 确实如此,但我需要存储一个唯一的对象。
-
我不明白你的意思,请详细说明
-
@Clint 我正在使用具有地图相关数据的 JS 变量对象(使用传单 js),我需要能够通过 C# 访问它。 C# 将其显示为未定义,而 JS 控制台显示。
标签: javascript c# webforms