locked
how to call Jquery function in the page load cs web form asp.net RRS feed

  • Question

  • User858909527 posted

    I have an external Jquery file.

    How can I call a jquery in the page load function in the cs code? 

    I put in the page: 

    <script type="text/javascript" src="Jquery.js"></script>

    Note: fun() is a function that is inside the Jquery file

    and in the cs code --> in the page load function I put : Page.ClientScript.RegisterStartupScript(this.GetType(), "fun", "fun()", true);

    But it doesn't work with me. I think the call function in the page load is not correct.

    Please tell me how to call the function in the page load function.

    Monday, February 11, 2019 5:17 AM

All replies

  • User-2054057000 posted

    What are you trying to achieve? Basically you will put the script in the .aspx page

    <script type="text/javascript" src="Jquery.js"></script>

    Monday, February 11, 2019 11:53 AM
  • User-1174608757 posted

    Hi zalnaser,

    According to your description, I have made a sample here. I find there is nothing wrong with your code in page load function. Here is a demo, if you still couldn't find the problem, could you please post more detail about your code in front end? It will help us to solve your problem.

    in Aspx:

    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            function fun()
            {
                alert("This is a js function");
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
            </div>
        </form>
    </body>
    </html>
    

    in code behind:

     public partial class clientscript : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "fun", "fun()", true);
            }
        }


    it shows as below:

    Best Regards

    Wei Zhang

    Tuesday, February 12, 2019 1:46 AM
  • User858909527 posted

    The script that I have, it has a lot of lines. Can I write the name of the script in the html rather than putting the code of it all (call it)?  

    Also, I want the script to work only on the first page load. How can I reach to my requirement?

    Tuesday, February 12, 2019 5:07 AM
  • User858909527 posted

    Wei Zhang

    Hi zalnaser,

    According to your description, I have made a sample here. I find there is nothing wrong with your code in page load function. Here is a demo, if you still couldn't find the problem, could you please post more detail about your code in front end? It will help us to solve your problem.

    in Aspx:

    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            function fun()
            {
                alert("This is a js function");
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
            </div>
        </form>
    </body>
    </html>

    in code behind:

     public partial class clientscript : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "fun", "fun()", true);
            }
        }


    it shows as below:

    Best Regards

    Wei Zhang

    The script that I have, it has a lot of lines. Can I write the name of the script in the html rather than putting the code of it all (call it)?  

    Also, I want the script to work only on the first page load. How can I reach to my requirement?

    I am using Asp.net web form, when I put html in my code it gives me green and blue lines .

    Tuesday, February 12, 2019 5:08 AM
  • User-1174608757 posted

    Hi zalnaser,

     

    Can I write the name of the script in the html rather than putting the code of it all (call it)

    Of course, you could write the jquery function in a js file , then you could just import the js file in your page and it will  work well .

    I want the script to work only on the first page load

    You could put the  Page.ClientScript.RegisterStartupScript() in the if (!IsPostBack){  }  method  to judge whether it is the first page load. 

    Here is a demo, I hope it could help you.

    js file

    function aa() {
        $("#Button1").click(function () {
    
            alert("this function is in js file");
        });
    }

    aspx:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script src="aa.js"></script>  <%--import the js file--%>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <input id="Button1" type="button" value="call the function" />
    
                <asp:Button ID="Button2" runat="server" Text="refresh the page" />
            </div>
        </form>
    </body>
    </html>
    

    aspx.cs

    public partial class clientscript : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) //this means it is first pageload 
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), " ", "aa()", true);
    
                }
               
            }
        }

    It shows as below:

    Best Regards

    Wei Zhang

    Tuesday, February 12, 2019 8:28 AM
  • User858909527 posted

    Hi zalnaser,

     

    zalnaser

    Can I write the name of the script in the html rather than putting the code of it all (call it)

    Of course, you could write the jquery function in a js file , then you could just import the js file in your page and it will  work well .

    zalnaser

    I want the script to work only on the first page load

    You could put the  Page.ClientScript.RegisterStartupScript() in the if (!IsPostBack){  }  method  to judge whether it is the first page load. 

    Here is a demo, I hope it could help you.

    js file

    function aa() {
        $("#Button1").click(function () {
    
            alert("this function is in js file");
        });
    }

    aspx:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script src="aa.js"></script>  <%--import the js file--%>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <input id="Button1" type="button" value="call the function" />
    
                <asp:Button ID="Button2" runat="server" Text="refresh the page" />
            </div>
        </form>
    </body>
    </html>

    aspx.cs

    public partial class clientscript : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) //this means it is first pageload 
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), " ", "aa()", true);
    
                }
               
            }
        }

    It shows as below:

    Best Regards

    Wei Zhang

    Does this line means a head that it's in the html ? <asp:Content ID="Content1" ContentPlaceHolderID="head1" Runat="Server">

    Wednesday, February 13, 2019 5:03 AM
  • User-1174608757 posted

    Hi zalnaser,

    Does this line means a head that it's in the html ? <asp:Content ID="Content1" ContentPlaceHolderID="head1" Runat="Server">

    In fact ,This Content control correspond to the <asp:ContentPlaceHolder ID="head1" runat="server"> control in  master page's head. So you could understand it as the head in the html.

    In master page , you could create a ContentPlaceHolder control which could correspond the Content control in subpage. When the subpage finally show the in the browser, the content of Content control will be added into the master page.

    I hope it could help you.

    Best Regards

    Wei Zhang

    Wednesday, February 13, 2019 6:29 AM
  • User858909527 posted

    Hi zalnaser,

    zalnaser

    Does this line means a head that it's in the html ? <asp:Content ID="Content1" ContentPlaceHolderID="head1" Runat="Server">

    In fact ,This Content control correspond to the <asp:ContentPlaceHolder ID="head1" runat="server"> control in  master page's head. So you could understand it as the head in the html.

    In master page , you could create a ContentPlaceHolder control which could correspond the Content control in subpage. When the subpage finally show the in the browser, the content of Content control will be added into the master page.

    I hope it could help you.

    Best Regards

    Wei Zhang

    I did what you said line by line and It works correctly as before. But the problem is that my script will show a window to fill, when the user fill it and close it ,he has 2 buttons in this page, when he click on one of them the form (script) will pop up again for him.

    Wednesday, February 20, 2019 5:11 AM
  • User-1174608757 posted

    Hi zalnaser,

    when he click on one of them the form (script) will pop up again for him.

    So , what is your problem?You don't want the pop up show again? Could you please post the code about your program? Have you put the Page.ClientScript.RegisterStartupScript()

    in the button click event in code behind?I hope you could share your code , and show me what problem you want to solve. I will try my best to solve your problem.

    Best Regards

    Wei Zhang

    Thursday, February 21, 2019 2:04 AM
  • User858909527 posted

    Hi zalnaser,

    zalnaser

    when he click on one of them the form (script) will pop up again for him.

    So , what is your problem?You don't want the pop up show again? Could you please post the code about your program? Have you put the Page.ClientScript.RegisterStartupScript()

    in the button click event in code behind?I hope you could share your code , and show me what problem you want to solve. I will try my best to solve your problem.

    Best Regards

    Wei Zhang

    This is my aspx page:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Master Pages/MasterPage.master" AutoEventWireup="true" Inherits="Patents.Public.Applicant.PaymentConfirmation" Codebehind="PaymentConfirmation.aspx.cs" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head1" Runat="Server">

    <script type="text/javascript" src="../../../js/jquery-3.3.1.js"></script>

    <script type="text/javascript" src="../../../js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="../../../js/tingle.min.js"></script>
    <link href="../css/survey.css" rel="stylesheet" type="text/css" />
    <link href="../css/tingle.min.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../../../js/bootstrap.js"></script>
    <script type="text/javascript" src="../../../js/bootstrap.min.js"></script>
    <script src="../js/Test.js"></script>--> My javascript is called here

    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <table>
    <tr>
    <td>
    <asp:Button ID="Email" runat="server" CssClass="btn btn-default btn-lg" Text="sendEmail"  OnClick="btnSendEmail_Click"/>
    </td>
    <td>
    <asp:Button ID="Print" runat="server" CssClass="btn btn-default btn-lg" Text="Print" OnClick="btnPrint_Click"/>
    </td>
    </tr>

    </table>

    </div>
    </asp:Content>

    This is my cs code:

    namespace Patents.Public.User
    {
    public partial class Test2 : Page
    {

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack) //this means it is first pageload
    {
    Page.ClientScript.RegisterStartupScript(this.GetType(), "fun", "fun()", true);

    }

    else

    if (IsPostBack) return;

    .

    .

    public void Email_Click(object sender, EventArgs e)
    {
    SendEmail();
    }

    public void Print_Click(object sender, EventArgs e)
    {
    Response.Redirect($"PaymentDetails.aspx?id={Request.Params["id"]}");
    }

    .

    }

    The script works for me, however as you see that I have 2 buttons:

    And those the summary of the problems that I have:

    1. if the user clicks on the email button that will send an email to the user, the script will work again and will pop up on the screen.
    2.  when the user clicks on the other button(print), it will redirect him to another page, but when the user roll back to the previous page the script will pop up again.
    3. when the user refresh the page the form script will pop up again.Regards,
    Thursday, February 21, 2019 6:54 AM
  • User-1174608757 posted

    Hi zalnaser,

    I am so sorry , that is my fault.

    if (!IsPostBack) is to judge whether it is the first post request. However if you just refresh the page, it is not a post request but a get request.

    So,I suggest you to use session to record the page state. For example ,when you first load the page , you could get session["id"] , since you haven't set the value of it , so it must be null. Then you could set the value of  it as 0 or other , then you could make a if else code when session["id"]=0 ,it is first page load, else when it is not null,it means you have loaded the page. Here is the code, I hope it could help you.

    js file:

    function aa() {
       
            alert("this function is in js file");
        
    }

    aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="client.aspx.cs" Inherits="WebApplication1._2._12.client" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <script src="aa.js"></script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    </asp:Content>
    

    aspx.cs:

     protected void Page_Load(object sender, EventArgs e)
            {
                
                if (Session["id"] == null)
                {
                    Session["id"] = "0";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "aa", "aa()", true);
    
                }
                else
                {
    
    
                }
                
            }

    Best Regards

    Wei Zhang

    Thursday, February 21, 2019 9:32 AM
  • User858909527 posted

    Hi zalnaser,

    I am so sorry , that is my fault.

    if (!IsPostBack) is to judge whether it is the first post request. However if you just refresh the page, it is not a post request but a get request.

    So,I suggest you to use session to record the page state. For example ,when you first load the page , you could get session["id"] , since you haven't set the value of it , so it must be null. Then you could set the value of  it as 0 or other , then you could make a if else code when session["id"]=0 ,it is first page load, else when it is not null,it means you have loaded the page. Here is the code, I hope it could help you.

    js file:

    function aa() {
       
            alert("this function is in js file");
        
    }

    aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="client.aspx.cs" Inherits="WebApplication1._2._12.client" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <script src="aa.js"></script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    </asp:Content>

    aspx.cs:

     protected void Page_Load(object sender, EventArgs e)
            {
                
                if (Session["id"] == null)
                {
                    Session["id"] = "0";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "aa", "aa()", true);
    
                }
                else
                {
    
    
                }
                
            }

    Best Regards

    Wei Zhang

    It works like before :( .

    Do you have any other idea?

    How Can I put the whole jquery code of 70 lines in place of "aa()" in the following line without declaring the script in the head?

    Page.ClientScript.RegisterStartupScript(this.GetType(), "aa", "aa()", true);

    Thursday, February 21, 2019 10:32 AM
  • User-1174608757 posted

    Hi zalnaser,

    I hope you could  post details about your code. How do you write the session  in your code behind? Below are all my steps.if you still has any problem, you could post all your code in your js file and aspx file

    First,I create a js file and set name  for example  'Test.js' and I put a function aa( ) which I want to call in subpage code behind  in it , such as below;

    Test.js

    function aa() {
       
            alert("this function is in js file");
        
    }

    2. I create a subpage  for  site.master. Below is site master and subpage:

    site.master

    <head runat="server">
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title><%: Page.Title %> - My ASP.NET Application</title>
    
        <asp:ContentPlaceHolder ID="header1" runat="server"></asp:ContentPlaceHolder>
        <asp:PlaceHolder runat="server">
            <%: Scripts.Render("~/bundles/modernizr") %>
        </asp:PlaceHolder>
    
        <webopt:bundlereference runat="server" path="~/Content/css" />
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    
    </head>
    <body>
        <form runat="server">
            <asp:ScriptManager runat="server">
                <Scripts>
                    <%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
                    <%--Framework Scripts--%>
                    <asp:ScriptReference Name="MsAjaxBundle" />
                    <asp:ScriptReference Name="jquery" />
                    <asp:ScriptReference Name="bootstrap" />
                    <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                    <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                    <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                    <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                    <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                    <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                    <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                    <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                    <asp:ScriptReference Name="WebFormsBundle" />
                    <%--Site Scripts--%>
                </Scripts>
            </asp:ScriptManager>
    
            <div class="navbar navbar-inverse navbar-fixed-top">
                <div class="container">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" runat="server" href="~/">Application name</a>
                    </div>
                    <div class="navbar-collapse collapse">
                        <ul class="nav navbar-nav">
                            <li><a runat="server" href="~/">Home</a></li>
                            <li><a runat="server" href="~/About">About</a></li>
                            <li><a runat="server" href="~/Contact">Contact</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="container body-content">
                <asp:ContentPlaceHolder ID="MainContent" runat="server">
                </asp:ContentPlaceHolder>
                <hr />
                <footer>
                    <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
                </footer>
            </div>
    
        </form>
    </body>
    </html>
    

    (subpage) WebForm4.aspx :

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication1.WebForm4" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="header1" runat="server"><%--this correspond to the head of sitemaster--%>
        <script src="2.12/aa.js"></script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    </asp:Content>
    

    then ,in WebForm4.aspx.cs:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["name"] == null)
                {
                    Session["name"] = "0";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "aa()", true);// aa() means the function name in text.js which you want to call in first page load.
    
                }
                else
                {
    
    
                }

    If you the aa()function has been called whenever you refresh the page, I hope you could post all you code. I will try my best to solve your problem as soon as possible.

    Best Regards

    Wei Zhang

    Friday, February 22, 2019 3:11 AM
  • User858909527 posted

    Hi zalnaser,

    I hope you could  post details about your code. How do you write the session  in your code behind? Below are all my steps.if you still has any problem, you could post all your code in your js file and aspx file

    First,I create a js file and set name  for example  'Test.js' and I put a function aa( ) which I want to call in subpage code behind  in it , such as below;

    Test.js

    function aa() {
       
            alert("this function is in js file");
        
    }

    2. I create a subpage  for  site.master. Below is site master and subpage:

    site.master

    <head runat="server">
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title><%: Page.Title %> - My ASP.NET Application</title>
    
        <asp:ContentPlaceHolder ID="header1" runat="server"></asp:ContentPlaceHolder>
        <asp:PlaceHolder runat="server">
            <%: Scripts.Render("~/bundles/modernizr") %>
        </asp:PlaceHolder>
    
        <webopt:bundlereference runat="server" path="~/Content/css" />
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    
    </head>
    <body>
        <form runat="server">
            <asp:ScriptManager runat="server">
                <Scripts>
                    <%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
                    <%--Framework Scripts--%>
                    <asp:ScriptReference Name="MsAjaxBundle" />
                    <asp:ScriptReference Name="jquery" />
                    <asp:ScriptReference Name="bootstrap" />
                    <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                    <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                    <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                    <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                    <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                    <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                    <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                    <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                    <asp:ScriptReference Name="WebFormsBundle" />
                    <%--Site Scripts--%>
                </Scripts>
            </asp:ScriptManager>
    
            <div class="navbar navbar-inverse navbar-fixed-top">
                <div class="container">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" runat="server" href="~/">Application name</a>
                    </div>
                    <div class="navbar-collapse collapse">
                        <ul class="nav navbar-nav">
                            <li><a runat="server" href="~/">Home</a></li>
                            <li><a runat="server" href="~/About">About</a></li>
                            <li><a runat="server" href="~/Contact">Contact</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="container body-content">
                <asp:ContentPlaceHolder ID="MainContent" runat="server">
                </asp:ContentPlaceHolder>
                <hr />
                <footer>
                    <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
                </footer>
            </div>
    
        </form>
    </body>
    </html>

    (subpage) WebForm4.aspx :

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication1.WebForm4" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="header1" runat="server"><%--this correspond to the head of sitemaster--%>
        <script src="2.12/aa.js"></script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    </asp:Content>

    then ,in WebForm4.aspx.cs:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["name"] == null)
                {
                    Session["name"] = "0";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "aa()", true);// aa() means the function name in text.js which you want to call in first page load.
    
                }
                else
                {
    
    
                }

    If you the aa()function has been called whenever you refresh the page, I hope you could post all you code. I will try my best to solve your problem as soon as possible.

    Best Regards

    Wei Zhang

    It doesn't work with me. 

    Monday, February 25, 2019 8:33 AM
  • User-1174608757 posted

    Hi zalnaser,

    All right,there also has other ways. You could use sessionStorage in jquery to get whether it is first page load. You could write as below,I hope it could help you.

     

    <head runat="server">
        <title></title>
        <script src="../2.12/aa.js"></script>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            $(function () {
                var a = sessionStorage.getItem('Data');
                if (a == null) {
                     sessionStorage.setItem("Data", "123");
                    aa();
                } else
                {
    
    
                }
    
    
    
            })
            
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
    
            </div>
        </form>
    </body>
    </html>
    

    Best Regards

    Wei Zhang

    Tuesday, February 26, 2019 2:32 AM
  • User858909527 posted

    Hi zalnaser,

    All right,there also has other ways. You could use sessionStorage in jquery to get whether it is first page load. You could write as below,I hope it could help you.

     

    <head runat="server">
        <title></title>
        <script src="../2.12/aa.js"></script>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            $(function () {
                var a = sessionStorage.getItem('Data');
                if (a == null) {
                     sessionStorage.setItem("Data", "123");
                    aa();
                } else
                {
    
    
                }
    
    
    
            })
            
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
    
            </div>
        </form>
    </body>
    </html>

    Best Regards

    Wei Zhang

    It works with me when I used  LiteralScripts.Text with else statement.

    Thanks a lot for all your tries with me. :D

    Tuesday, February 26, 2019 6:53 AM
  • User-1174608757 posted

    That is my pleasure. Enjoy your day :)

    Wei Zhang

    Tuesday, February 26, 2019 7:22 AM