0

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"

7
  • why do you need to keep it in localstorage?? Is this some sort of attempt to implement caching? Commented Nov 4, 2019 at 9:02
  • @ADyson This company logo is placed in master page and i may have 100s of client pages. so on load of every client page this image will be read from sql server which i want to avoid Commented Nov 4, 2019 at 9:09
  • ok. But this approach will not work. You've misunderstood how the client-side and server-side programming lifecycle happens. if (hdfStorage.Value == "true") will never be true. RegisterStartupScript just 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 server Commented Nov 4, 2019 at 9:19
  • A more viable approach would be to make a URL available (e.g. as an ashx handler or a webmethod or a WCF endpoint) which you would call something like https://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 the src of 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). Commented Nov 4, 2019 at 9:22
  • And as long as you don't disallow caching on that URL, then the browser will automatically cache the image returned in the response and will not request it repeatedly every time a new page loads. That is a conventional approach to this kind of problem, using the caching features already provided as standard in all browsers and webservers. Commented Nov 4, 2019 at 9:23

1 Answer 1

1

I suggest to put it in memory as singleton or session, all people work like this

Sign up to request clarification or add additional context in comments.

2 Comments

do you mean to keep the image in session? will that be advisable to keep say 3 mb of file in session
Just one for all users, because all images that come from database they stored by default in memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.