![](/img/trans.png)
[英]SET Parent page (aspx) label text from User Control (ascx) using JavaScript and without PostBack()
[英]set the value for aspx label element from javascript
我正在使用 javascript 調用 Web API,我有一個 ID 為“blbtest”的 aspx label 元素。
<asp:Label ID="blbtest" Text="" runat="server ClientIDMode="Static">
我想將 name 變量的值存儲在此 label 中,但出現錯誤
這是js代碼:
var request = new XMLHttpRequest;
request.open('GET', "https://hplussport.com/api/products?qty=2");
request.onload = function () {
var response = request.response;
var parsedData = JSON.parse(response);
console.log(parsedData);
var name = parsedData[0].name;
var products = document.createElement('li');
//products.innerHTML = name;
//document.body.appendChild(products);
var lbl = document.getElementById('<%=blbtest.ClientID%>');
lbl.innerText = name;
};
request.send();
錯誤:未捕獲類型錯誤:無法在 XMLHttpRequest.request.onload (script.js:14:19) 處設置 null 的屬性(設置“innerText”)
注釋行有效。 但是我想在頁面加載時在 label 元素中顯示產品名稱。
所以,我嘗試了這個並且成功了:)
document.getElementById('blbtest').innerHTML = name;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.