Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call javascript function on page load in asp.net [duplicate]

Possible Duplicate:
Javascript that executes after page load

i have application in asp.net 4.0

i have javascript to display client zone offset into text box:- `

<script type="text/javascript">
    function GetTimeZoneOffset() {
        var d = new Date()
        var gmtOffSet = -d.getTimezoneOffset();
        var gmtHours = Math.floor(gmtOffSet / 60);
        var GMTMin = Math.abs(gmtOffSet % 60);
        var dot = ".";
        var retVal = "" + gmtHours + dot + GMTMin;
        document.getElementById('<%= offSet.ClientID%>').value = retVal;
    }

</script>

`

Html MarkUp

<asp:HiddenField ID="clientDateTime" runat="server" />
<asp:HiddenField ID="offSet" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></body>

How can I call this function on page load so that I can display offset into textbox?

like image 480
MonaliJ Avatar asked Dec 29 '12 05:12

MonaliJ


People also ask

How call javascript function on page load in MVC?

To include a JavaScript function, it is as simple as include a <script> tag and define the function inside the script block. Now, to call the function, add an onclick event on the employee name hyperlink and call the function.

How can we call a function after page load complete in asp net?

Hi, You can use ASP.Net Ajax ScriptManager to register startup script. In this startup script you can call a web service or page method that will fetch some data from server and then change your html controls on client side with the data from server.

How can we call Ajax function on page load in asp net?

Page_Load runs on the server, and jquery runs on the client. as suggested, you render javascript code included in the html page you are sending to the browser. once the browser loads the page, it will make the ajax call. this will all occur be after the server is done with the page (after Page_Unload).


1 Answers

Calling JavaScript function on code behind i.e. On Page_Load

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

If you have UpdatePanel there then try like this

ScriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);
like image 110
Satinder singh Avatar answered Oct 04 '22 15:10

Satinder singh