我需要从 SQL Server(公司 Logo )读取图像数据并将其保存在 localStorage 中。该公司 Logo 显示在母版页左侧边栏中,以便在每个客户端页面中都可见。
为此目的,我在母版页中使用了 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"
最佳答案
我建议将其作为单例或 session 放在内存中,所有人都这样工作
关于javascript - 从 ASP.Net MasterPage 调用 JS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58689650/