Click here to Skip to main content
15,884,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have these JavaScript function that runs every time on every pageload and postback. How do I change the settings of this function to make it run only during the 1st load of the webpage and not every postback?

Do I change $(window).ready(function ()? OR I set it in my aspx.cs file page load?

JavaScript
$(window).ready(function () {
    $("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
    $("#Checkboxlist1  :checkbox[value='Select All']").removeAttr('disabled');
    $("#Checkboxlist1  :checkbox[value='Select All']").prop("checked", true);
    });


Another way I tried doing:
HTML
<input type="hidden" id="hidden" value=""/>

JavaScript
$(function () {
    if ($("#hidden").val() == "") {
        $("#Checkboxlist1  :checkbox").attr('disabled', 'disabled');
        $("#Checkboxlist1  :checkbox[value='Select All']").removeAttr('disabled');
        $("#Checkboxlist1  :checkbox[value='Select All']").prop("checked", true);
        $("#hidden").val("set");
    }
});
Posted
Updated 24-Sep-15 22:36pm
v5

if aspx.cs file

:pageload()
{
if(!ispostback)
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

}
 
Share this answer
 
Comments
waynetan123 25-Sep-15 3:08am    
Do I need to change $(window).ready(function () ?
Member 12003400 25-Sep-15 3:27am    
Yes,Make a function and write your code in that function and call that function from aspx.cs page
like
function FUNCTIONNAME() {
$("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
$("#Checkboxlist1 :checkbox[value='Select All']").removeAttr('disabled');
$("#Checkboxlist1 :checkbox[value='Select All']").prop("checked", true);
});
Member 12003400 25-Sep-15 3:31am    
OR TRY
$(window).load() or javascript's window.onload() or <body onload=""
waynetan123 25-Sep-15 3:52am    
It still reloads during postback e.g when I click on submit button
You can use Local Storage[^] feature of HTML5.
JavaScript
$(window).ready(function () {
    localStorage.setItem("isDataLoaded", false);
    
    if(!localStorage.getItem("isDataLoaded"))
    {
        $("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
        $("#Checkboxlist1  :checkbox[value='Select All']").removeAttr('disabled');
        $("#Checkboxlist1  :checkbox[value='Select All']").prop("checked", true);
        
        localStorage.setItem("isDataLoaded", true);
    }
});


Haven't tested but it should work ;)

-KR
 
Share this answer
 
Comments
waynetan123 25-Sep-15 3:18am    
Does not work:(
In aspx code inside the page load event you can store the IsPostBack value like:

C#
ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack",
         String.Format("var IsPostBack = {0};", Page.IsPostBack.ToString().ToLower()), true);


And then in js code you can check it like

JavaScript
$(document).ready(function () {

    // For debugging purpose you can add a alert here like
    alert(IsPostback);

    if (!IsPostback) {

        // If not a post back then execute your logic here..
        $("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
        $("#Checkboxlist1  :checkbox[value='Select All']").removeAttr('disabled');
        $("#Checkboxlist1  :checkbox[value='Select All']").prop("checked", true);
    }
});


Let me know if you are still facing any issues.

UPDATE:

In aspx:

Add a hidden field like

ASP.NET
<asp:HiddenField ID="hfServerValue" runat="server" />


In cs (inside Page_Load):

C#
hfServerValue.Value = Page.IsPostBack.ToString().ToLower();


In js:

JavaScript
$(document).ready(function () {

    // For debugging purpose you can add a alert here like
    var IsPostback = $("[id$='hfServerValue']").val();
    IsPostback = (IsPostback == 'true');
    alert(IsPostback);

    if (!IsPostback) {

        // If not a post back then execute your logic here..
        $("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
        $("#Checkboxlist1  :checkbox[value='Select All']").removeAttr('disabled');
        $("#Checkboxlist1  :checkbox[value='Select All']").prop("checked", true);
    }
});


Let me know if it works!
 
Share this answer
 
v4
Comments
waynetan123 25-Sep-15 3:25am    
Hi! Here the error I received-0x800a1391 - JavaScript runtime error: 'IsPostback' is undefined @ if (!IsPostback)
Palash Mondal_ 25-Sep-15 3:35am    
Hope you have added the first code in code behind. Also try replacing that with:


ScriptManager.RegisterStartupScript(this, this.GetType(), "IsPostBack", String.Format("var IsPostBack = {0};", Page.IsPostBack.ToString().ToLower()), true);


once.
waynetan123 25-Sep-15 3:44am    
Hi I'm still receiving the same error
Palash Mondal_ 25-Sep-15 3:47am    
ok, try this one:


ClientScript.RegisterStartupScript(GetType(), "IsPostBack", String.Format("var IsPostBack = {0};", Page.IsPostBack.ToString().ToLower()), true);
waynetan123 25-Sep-15 3:48am    
sorry still the same error. From my research I roughly found out that if (postback) cant be used in javascript

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900