Surensiveaya
Surensiveaya

Reputation: 347

Calling a JS function from an ASP.Net MasterPage

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"

Upvotes: 0

Views: 261

Answers (1)

Ayoub Codes.
Ayoub Codes.

Reputation: 988

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

Upvotes: 1

Related Questions