locked
javascript with parameter on master page - call from default page RRS feed

  • Question

  • User-19399675 posted

    Hello

    I have the javascript function with parameter on the master page and all other pages are showing the result of that function on top of the screen. The problem, that I have - if I try to change the value of the parameter on the other page - it doesn't refresh the value on the master page and consequent pages. Not sure if I am calling it correctly or missing something. On a master page it is called with parameter value (default is 0)

    Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag""startTime('" + Application["Spanish"].ToString() + "')"true);
    

    The function to show the time is called on site.master with command:

    <body onLoad="setInterval(startTime(), 1000);"> 
    

    On the default page I call it with the parameter value 1 on page load - btnspanish.Attributes.Add("onclick""startTime('1'); ");

    I set Application["Spanish"] = 1 in btnspanish_click() function.

    While debugging I discovered that once I click on btnspanish - it goes to master page before the parameter is set to 1, therefore the command above on master page is called with 0, not 1 as I need. Then it goes back to default page and continues the steps from btnspanish_click function...

    Please advise on how I can fix it. Thanks!

    Friday, October 28, 2016 7:50 PM

All replies

  • User283571144 posted

    Hi asanders2552,

    avascript with parameter on master page - call from default page

    According to your description and codes, I couldn't understand your requirement clearly.

    I find in your master page body onload method, you called setInterval method.

    As far as I know, this method will always execute per second.

    Do you mean you want to change this function in the content page?

    Besides, I found this startTime function has no parameters in the body onload method.

    <body onLoad="setInterval(startTime(), 1000);"> 

    Why you could set the parameters in the content page?

    Do you mean your page have two startTime function?

    I suggest you could write a little demo about this error and post it.

    If you could post more details information and codes, it will be more easily for us to find the solution.

    Best Regards,

    Brando 

    Monday, October 31, 2016 5:56 AM
  • User-19399675 posted

    Hello again

    I had updated the code to call SetInterval with the parameter on master page load. Initially the parameter is 0 - which means that the default language is English and the function shows the date and time in English. The code is called on the master page:

    if (Application["Spanish"] == null)
               {
                   Application["Spanish"] = 0;
               }
              
    Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag""setInterval(startTime('" + Application["Spanish"].ToString() + "'), 1000)"true);

    On the default page I have the button to switch the language and once user clicks on it - need to switch the date and time to show in Spanish. The problem that I am having - once user presses the button to switch the language, the code goes to master page and calls RegisterStartupscript and calls it with Application["Spanish"]=0, not 1. I have the command setting it to 1 on default page on Click event, but the code goes to master page before it sets it to 1. How can I fix it?

    On default page load I tried to use attributes, but it's still not switching to Spanish...

    if (Application["Spanish"].ToString() == "1")
                   {
                       btnspanish.Attributes.Add("onclick""startTime('1'); ");
                   }
                   else
                   {
                       btnspanish.Attributes.Add("onclick""startTime('0'); ");
                   }
    Monday, October 31, 2016 12:54 PM
  • User283571144 posted

    Hi asanders2552,

    The problem that I am having - once user presses the button to switch the language, the code goes to master page and calls RegisterStartupscript and calls it with Application["Spanish"]=0, not 1. I have the command setting it to 1 on default page on Click event, but the code goes to master page before it sets it to 1. How can I fix it?

    According to your description and codes, I couldn't see any coeds about how you set the Application["Spanish"].

    I found in your master page's codes, if "Application["Spanish"] == null" the "Application["Spanish"]" is '0'.

    So I think you don't set "Application["Spanish"]" to '1' well.

    Could you please post the more relevant coeds about the button click event?

    As far as I know, if you set the "Application["Spanish"]" to '1' in your button click event.

    It will firstly execute the content page pageload event.

    Then it will execute the master page pageload event.

    At last it will execute the button click event.

    Since I couldn't see the relevant codes about your master page and content page, I couldn't reproduce the issue you have faced.

    I suggest you could use visual studio debugger tool to set a breakpoint to see how the codes works.

    Besides, if you could post more relevant codes, it will be more easily for us to reproduce the problem and find the solution.

    Best Regards,

    Brando

    Wednesday, November 2, 2016 7:53 AM
  • User-19399675 posted

    I set the Application["Spanish"] on the button click on the default page.

    Just to recap. On master page load:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (Application["Spanish"] == null)
                {
                    Application["Spanish"] = 0;
                }
               
                Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag", "setInterval(startTime('" + Application["Spanish"].ToString() + "'), 1000);init();", true);
    }

    On default page:

    protected void Page_Load(object sender, EventArgs e)
    {
     if (Application["Spanish"].ToString() == "1")
                {
                    btnspanish.Attributes.Add("onclick", "startTime('1'); ");
                }
                else
                {
                    btnspanish.Attributes.Add("onclick", "startTime('0'); ");
                }
    }

    On default page on btnspanish click:

     protected void btnspanish_Click(object sender, EventArgs e)
            {
    
                Application["Spanish"] = 1;        
            }

    When I debug and click on btnspanish - it goes to master page and runs the set time code with Application["Spanish"]=0, then comes back to btnspanish_Click and sets the parameter to 1, but keeps time in English, not Spanish. Sometimes I could see that it flickers real fast to Spanish but reverses it back to English. Please advise on how to fix it.

    The actual script with parameter:

    <script type="text/javascript">
    function startTime(flag_spanish) {
        var today = new Date();
       // debugger
        if (flag_spanish == '1')
        {      
            var days = ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'];
            var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
        }
        else
        {
            var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
            var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        }
        var mm = today.getMonth();
        var dd = today.getDate();    
        var day = days[today.getDay()];
        var yy = today.getFullYear();
        var mnthdesc = months[today.getMonth()];;
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        
        var ampm;
        if (h > 12)
        {
           h = h - 12;
           ampm = "PM";
        }
        else
           ampm = "AM";
        m = checkTime(m);
        s = checkTime(s);
     
        document.getElementById("timevalue").innerHTML = day + " " + mnthdesc + " " + dd + ", " + yy + "  " + h + ":" + m + " " + ampm;
            var t = setTimeout(startTime, 500);
      
    }
    function checkTime(i) {
        if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
        return i;
    }

    Wednesday, November 2, 2016 12:09 PM
  • User283571144 posted

    Hi asanders2552,

    When I debug and click on btnspanish - it goes to master page and runs the set time code with Application["Spanish"]=0, then comes back to btnspanish_Click and sets the parameter to 1, but keeps time in English, not Spanish. Sometimes I could see that it flickers real fast to Spanish but reverses it back to English. Please advise on how to fix it.

    As my previous post says, the page's event execution order is like below:

    If you click the button.

    It will firstly execute the content page pageload event.

    This codes:

    protected void Page_Load(object sender, EventArgs e)
    {
     if (Application["Spanish"].ToString() == "1")
                {
                    btnspanish.Attributes.Add("onclick", "startTime('1'); ");
                }
                else
                {
                    btnspanish.Attributes.Add("onclick", "startTime('0'); ");
                }
    }

    Then it will execute the master page pageload event.

     protected void Page_Load(object sender, EventArgs e)
            {
                if (Application["Spanish"] == null)
                {
                    Application["Spanish"] = 0;
                }
               
                Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag", "setInterval(startTime('" + Application["Spanish"].ToString() + "'), 1000);init();", true);
    }

    At last it will execute the button click event.

     protected void btnspanish_Click(object sender, EventArgs e)
            {
    
                Application["Spanish"] = 1;        
            }

    So you will find it doesn't changed.

    I suggest you could try to call RegisterStartupScript method and content page load in button click event.

    More details, you could refer to follow codes:

     protected void btnspanish_Click(object sender, EventArgs e)
            {
    
                Application["Spanish"] = 1;        
                Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag", "setInterval(startTime('" + Application["Spanish"].ToString() + "'), 1000);init();", true);
            }
    

    Best Regards,

    Brando

    Sunday, November 6, 2016 8:31 AM
  • User-19399675 posted

    Per your suggestion - added this command on the button click-

    Master.Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag""setInterval(startTime('" + Application["Spanish"].ToString() + "'), 1000);init();"true);
    

    I can see that the Application["Spanish"] is 1, yet it still shows text in English... I kept the other call on master page. What am I missing?

    Monday, November 7, 2016 3:01 PM
  • User283571144 posted

    Hi asanders2552,

    I can see that the Application["Spanish"] is 1, yet it still shows text in English... I kept the other call on master page. What am I missing?

    According to your javascript function codes, I find you reset the starttime again.

    So it will still show the text in English.

    More details, you could refer to follow codes:

    function startTime(flag_spanish) {
        var today = new Date();
       // debugger
        if (flag_spanish == '1')
        {      
            var days = ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'];
            var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
        }
        else
        {
            var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
            var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        }
        var mm = today.getMonth();
        var dd = today.getDate();    
        var day = days[today.getDay()];
        var yy = today.getFullYear();
        var mnthdesc = months[today.getMonth()];;
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        
        var ampm;
        if (h > 12)
        {
           h = h - 12;
           ampm = "PM";
        }
        else
           ampm = "AM";
        m = checkTime(m);
        s = checkTime(s);
     
        document.getElementById("timevalue").innerHTML = day + " " + mnthdesc + " " + dd + ", " + yy + "  " + h + ":" + m + " " + ampm;
            var t = setTimeout(startTime, 500);
      
    }

    Best Regards,

    Brando

    Wednesday, November 9, 2016 1:26 PM
  • User-19399675 posted

    Well, I'm at complete loss. If I remove that time reset command - it doesn't reset time, the date in Spanish or English is still not working correctly. I tried to use Cache instead of Application[".."] - same results.

    I changed the code to reset it with the flag - the same issue, except now when I press Spanish - it shows time in English, when I press English - it shows time in Spanish. Seems like there is a delay in interpreting the flag.

    var t = setTimeout(startTime(flag_spanish), 500);
    

    I tried to change the flag from 0 to 2 - same results. Tried to reverse the numbers and call English flag on the button that triggers Spanish - it didn't work either! So that tells me that it ignores the calling of the startTime() and script register on button click... What else can I try???

    protected void btnspanish_Click(object sender, EventArgs e)
           {
     
               HttpRuntime.Cache["Spanish"] = 1;
               Master.Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag""setInterval(startTime('" + HttpRuntime.Cache["Spanish"].ToString() +"'), 1000);init();"true);
             

    Wednesday, November 9, 2016 9:19 PM
  • User283571144 posted

    Hi asanders2552,

    I tried to change the flag from 0 to 2 - same results. Tried to reverse the numbers and call English flag on the button that triggers Spanish - it didn't work either! So that tells me that it ignores the calling of the startTime() and script register on button click... What else can I try???

    According to your description, I couldn't reproduce the issue you faced now.

    Could you please post more relevant codes or a little demo about your codes now?

    I will test it and reproduce the issue you faced now.

    Like below:

    Master Page:

    ....

    Code-behind:

    .....

    Content Page:

    ....

    Content page code-behind:

    ....

    Best Regards,

    Brando

    Thursday, November 10, 2016 1:40 AM
  • User-19399675 posted

    Master page:

    <script type="text/javascript">
    function startTime(flag_spanish) {
        var today = new Date();
       // debugger
        if (flag_spanish == '1')
        {      
            var days = ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'];
            var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
        }
        if (flag_spanish == '0')
        {
            var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
            var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        }
        var mm = today.getMonth();
        var dd = today.getDate();    
        var day = days[today.getDay()];
        var yy = today.getFullYear();
        var mnthdesc = months[today.getMonth()];;
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        
        var ampm;
        if (h > 12)
        {
           h = h - 12;
           ampm = "PM";
        }
        else
           ampm = "AM";
        m = checkTime(m);
        s = checkTime(s);
        //document.getElementById("timevalue").innerHTML = h + ":" + m + ":" + s + " " + ampm;
        document.getElementById("timevalue").innerHTML = day + " " + mnthdesc + " " + dd + ", " + yy + "  " + h + ":" + m + " " + ampm;
        //document.getElementById("dayvalue").innerHTML = day;
        //document.getElementById("monthvalue").innerHTML = mnthdesc;
        //document.getElementById("daynumvalue").innerHTML = dd;
        //document.getElementById("yearvalue").innerHTML = yy;
        //document.getElementById("timedatev").innerHTML = day1;
       // debugger
        var t = setTimeout(startTime(flag_spanish), 500);
      
    }
    function checkTime(i) {
        if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
        return i;
    }
    
    
    
    </script>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Application["Spanish"] == null)
                    {
                        Application["Spanish"] = "0";
                    }
                }
               
                Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag", "setInterval(startTime('" + Application["Spanish"].ToString() + "'), 1000);init();", true);
    }

    Default page:

    protected void Page_Load(object sender, EventArgs e)
            {
                Application["SubScreen"] = "Y";
    
    
                if (!IsPostBack)
                {
                    if (HttpRuntime.Cache["AdaVisible"] == null)
                    {
                        HttpRuntime.Cache["AdaVisible"] = "False";
                    }
                    if (Application["Spanish"] == null)
                    {
                            Application["Spanish"] = "0";
                    }
                 }
     if (Application["Spanish"].ToString() == "1")
                {
    //Spanish
                    btnspanish.Visible = false;
                    btnenglish.Visible = true;
                    scroll_Eng.Visible = false;
                    scroll_Sp.Visible = true;
                    btnada_sp_main.Visible = true;
                    btnada.Visible = false;
                    btneri.Text = "Ejercicio, Recreación, Imaginación";
                    btncomm.Text = "Comunidad en el Lugar";
                    btnspirit.Text = "Bienestar Espiritual";
                    btneritouch.Text = "Tocar Aquí";
                    btncommtouch.Text = "Tocar Aquí";
                    btnspirittouch.Text = "Tocar Aquí";
                    btnadacalendar.Text = "Ver Calendario de Eventos";
                    btncalendar.Font.Size = FontUnit.Medium;
                    facheader = "Eventos de hoy en ";
                    btnadaeri.Text = "Ejercicio,Recreación,Imaginación";
                    btnadacomm.Text = "Comunidad en el Lugar";
                    btnadaspirit.Text = "Bienestar Espiritual";
                    btncalendar.Text = "Toque para ver Calendario de Eventos";
                    btnada.Text = "Toque Aquí Para Menú Accesible";
                    btnadaclose.Text = "X  Cerca";
                }
                else
                {
                    btnspanish.Visible = true;
                    btnenglish.Visible = false;
                    scroll_Eng.Visible = true;
                    scroll_Sp.Visible = false;
                    btnada_sp_main.Visible = false;
                    btnada.Visible = true;
                    facheader = "Today's Events at ";
                    btneri.Text = "Exercise, Recreation, Imagination";
                    btncomm.Text = "Community in Place";
                    btnspirit.Text = "Spiritual Wellness";
                    btneritouch.Text = "Touch Here";
                    btncommtouch.Text = "Touch Here";
                    btnspirittouch.Text = "Touch Here";
                    btncalendar.Font.Size = FontUnit.Large;
                    btnadaeri.Text = "Exercise,Recreation,Imagination";
                    btnadacomm.Text = "Community in Place";
                    btnadaspirit.Text = "Spiritual Wellness";
                    btnadacalendar.Text = "View calendar of Events";
                    btncalendar.Text = "View Calendar of Events";
                    btnada.Text = "Touch Here for Accessible Menu";
                    btnadaclose.Text = "X  Close";
                }
              }
     protected void btnspanish_Click(object sender, EventArgs e)
            {
    
                Application["Spanish"] = "1";
                Master.Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag", "setInterval(startTime('" + Application["Spanish"].ToString() + "'), 1000);init();", true);
           
                scroll_Eng.Visible = false;
                scroll_Sp.Visible = true;
                btnspanish.Visible = false;
                btnenglish.Visible = true;
                btnada.Visible = false;
                btnada_sp_main.Visible = true;
                facheader = "Eventos de hoy en ";
                facilitytitle.Text = facheader + Application["FacName"];
                btneri.Text = "Ejercicio, Recreación, Imaginación";
                btncomm.Text = "Comunidad en el Lugar";
                btnspirit.Text = "Bienestar Espiritual";
                btneritouch.Text = "Tocar Aquí";
                btncommtouch.Text = "Tocar Aquí";
                btnspirittouch.Text = "Tocar Aquí";
                btncalendar.Text = "Toque para ver Calendario de Eventos";
                btncalendar.Font.Size = FontUnit.Medium;
                btnadacalendar.Text = "Ver Calendario de Eventos";
                btnada.Text = "Toque Aquí Para Menú Accesible";
                btnadaeri.Text = "Ejercicio,Recreación,Imaginación";
                btnadacomm.Text = "Comunidad en el Lugar";
                btnadaspirit.Text = "Bienestar Espiritual";
                btnadaclose.Text = "X  Cerca";
                List<EventInfo> events = GetEventInfo();
                theRepeater.DataSource = events;
                theRepeater.DataBind();
                 }
            protected void btnenglish_Click(object sender, EventArgs e)
            {
    
                Application["Spanish"] = "0";
                Master.Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag", "setInterval(startTime('0'), 1000);init();", true);
    
                btnspanish.Visible = true;
                btnenglish.Visible = false;
                scroll_Eng.Visible = true;
                scroll_Sp.Visible = false;
                btnada.Visible = true;
                btnada_sp_main.Visible = false;
                facheader = "Today's Events at ";
                facilitytitle.Text = facheader + Application["FacName"];
                btncalendar.Font.Size = FontUnit.Large;
                btneri.Text = "Exercise, Recreation, Imagination";
                btncomm.Text = "Community in Place";
                btnspirit.Text = "Spiritual Wellness";
                btneritouch.Text = "Touch Here";
                btncommtouch.Text = "Touch Here";
                btnspirittouch.Text = "Touch Here";
                btnadaeri.Text = "Exercise,Recreation,Imagination";
                btnadacomm.Text = "Community in Place";
                btnadaspirit.Text = "Spiritual Wellness";
                btnadacalendar.Text = "View Calendar of Events";
                btncalendar.Text = "View Calendar of Events";
                btnada.Text = "Touch Here for Accessible Menu";
                btnadaclose.Text = "X  Close";
                List<EventInfo> events = GetEventInfo();
                theRepeater.DataSource = events;
                theRepeater.DataBind();
            }
    
    

    I appreciate your help!!!

     
     
    Thursday, November 10, 2016 10:22 PM
  • User283571144 posted

    Hi asanders2552,

    I changed the code to reset it with the flag - the same issue, except now when I press Spanish - it shows time in English, when I press English - it shows time in Spanish. Seems like there is a delay in interpreting the flag.

    According to your description and codes, I have written a test demo on my computer.

    Since I don't have inint method and couldn't see your html makeup, I change a little code, it works well.

    I think your just want to change the language in your page.

    So I suggest you could refer to follow codes:

    Master Page JS:

        <script type="text/javascript">
            var temp;
      function startTime(flag_spanish) {
          temp = flag_spanish;
        var today = new Date();
       // debugger
        if (flag_spanish == '1')
        {      
            var days = ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'];
            var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
        }
        if (flag_spanish == '0')
        {
            var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
            var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        }
        var mm = today.getMonth();
        var dd = today.getDate();    
        var day = days[today.getDay()];
        var yy = today.getFullYear();
        var mnthdesc = months[today.getMonth()];;
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        
        var ampm;
        if (h > 12)
        {
           h = h - 12;
           ampm = "PM";
        }
        else
           ampm = "AM";
        m = checkTime(m);
        s = checkTime(s);
        //document.getElementById("timevalue").innerHTML = h + ":" + m + ":" + s + " " + ampm;
         document.getElementById("timevalue").innerHTML = day + " " + mnthdesc + " " + dd + ", " + yy + "  " + h + ":" + m + " " + s + " " + ampm;
        //document.getElementById("dayvalue").innerHTML = day;
        //document.getElementById("monthvalue").innerHTML = mnthdesc;
        //document.getElementById("daynumvalue").innerHTML = dd;
        //document.getElementById("yearvalue").innerHTML = yy;
        //document.getElementById("timedatev").innerHTML = day1;
          // debugger 
    
    }
    function checkTime(i) {
        if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
        return i;
    }
    
    window.onload = function() {
        if (temp == null) {
            temp = 0;
        }
        //refresh page
        setInterval(function () { startTime(temp);}, 1000);
    };   
    </script>

    Page load event:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Application["Spanish"] == null)
                    {
                        Application["Spanish"] = "0";
                    }
                }
    
                Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag", "startTime('" + Application["Spanish"].ToString() + "');", true);
            }

    Conent page button click event:

        protected void Button1_Click(object sender, EventArgs e)
            {
                Application["Spanish"] = "1";
             
                Master.Page.ClientScript.RegisterStartupScript(GetType(), "LanguageFlag", "startTime('" + Application["Spanish"].ToString() + "');", true);
            }

    Result:

    Best Regards,

    Brando

    Wednesday, November 16, 2016 3:09 AM