I have a requirement to read image data from SQL Server (company logo) and keep it in localStorage. This company logo is displayed in the master page left sidebar so that in every client page it will be visible.
I use 3 JS functions in the Master page for this purpose.
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')
}
In the code behind of the Master page I am calling these JS functions
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);
}
}
I have tried putting it in the Page_Load event but no use. The issue I am facing is these functions are not getting called and if called from the Page Load event the hiddenfield control wont get the value when I check using hdfStorage.Value == "true"
if (hdfStorage.Value == "true")will never be true.RegisterStartupScriptjust places a JavaScript command into the page which is going to be sent to the browser when asp.net finishes processing the request. The JavaScript cannot be executed until it reaches the browser. All of the ASP.NET code will be executed first, on the server, and then the result of that (a HTML page) is send to the browser at the end. You cannot check localstorage from the serverhttps://example.com/GetLogo?companyID=123. You can of course inject the correct company ID in your ASP.NET code when you render the URL. You can set that URL as thesrcof the logo image. When the browser calls that URL it will then run some ASP.NET code which will return the correct image data from the database (using the ID provided in the querystring).