[英]Calling a JS function from an ASP.Net MasterPage
我需要從 SQL 服務器(公司徽標)讀取圖像數據並將其保存在 localStorage 中。 該公司徽標顯示在母版頁左側邊欄中,以便在每個客戶頁面中都可見。
為此,我在母版頁中使用了 3 個 JS 函數。
function storageCheck() {
if (localStorage.getItem("imgData") === null) //check availability
document.getElementById('<%=hdfStorage.ClientID %>').value = "true"; //assign true to a hidden field
else
document.getElementById('<%=hdfStorage.ClientID %>').value = "false";
}
function storelogo(imgData) { //store image data
localStorage.setItem("imgData", imgData);
}
function getImg() { //get data
var dataImage = localStorage.getItem('imgData');
document.getElementById('imgCompanyLogo').src = dataImage;
alert('got')
}
在母版頁后面的代碼中,我調用了這些 JS 函數
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (!IsPostBack)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "checkfn", "storageCheck()", true);
if (hdfStorage.Value == "true")
ScriptManager.RegisterStartupScript(this, this.GetType(), "storelogoo", "storelogo('" + <Base64String img data from database> + "');", true);
else if (hdfStorage.Value == "false")
ScriptManager.RegisterStartupScript(this, this.GetType(), "getlogo", "getImg();", true);
}
}
我試過把它放在Page_Load
事件中,但沒有用。 我面臨的問題是這些函數沒有被調用,如果從頁面加載事件中調用,當我使用hdfStorage.Value == "true"
進行檢查時,隱藏字段控件將不會獲得值
我建議把它放在 memory 作為 singleton 或 session,所有人都這樣工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.