1

Hi I want to run a javascript function when the page loads. But as my page derives from the master page there is no form . The is my aspx file

<%@ Page Title="" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="test3.aspx.vb" Inherits="test3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<script language="javascript">
    var m_Names = new Array();

    function LoadArray() {
        PageMethods.Load_Array(onSucceeded, onFailed);
    }
    function onSucceeded(result, userContext, methodName) {
        m_Names = result;
    }

    function onFailed(error, userContext, methodName) {
        alert("An error occurred")
    }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager> 
<asp:TextBox ID="txt_model_code" runat="server"></asp:TextBox><br />
<br />
<input type="button" value="db Function" /><br />
</asp:Content>

I want to run the LoadArray() function initialy when the page loads. This function is calling a pagemethod given in aspx.vb code file..

Partial Class test3
Inherits System.Web.UI.Page
<System.Web.Services.WebMethod()>
Public Shared Function Load_Array() As String()
    Dim Model_Name_old As String()()
    Dim mod_code As String()
    Dim mod_name As String()
    Dim cod_upper As Integer

    //calling webservice that retunrs a jagged array
    Dim ins As New localhost_insert_model.dbModel

    Model_Name_old = ins.get_Model_Name("A")

    mod_code = Model_Name_old(0)
    mod_name = Model_Name_old(1)

    Return mod_name
  End Function
End Class

So how can i load the javascrip LoadArray() function onPageLoad in this scenario??

4 Answers 4

2

This one should work

<script language="javascript">
var m_Names = new Array();

window.onload = function () 
    {
        LoadArray();

    }
 .....your functions    
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can use $document.ready() from jQuery.

2 Comments

Try this,<body onload="myFunction()"> or window.onload = function() { myFunction() }
You can also check, webdeveloper.com/forum/showthread.php?t=173832. You may find your answer.
0

If you don't need the whole page to have loaded completely you could just call it after you created the functions?

var m_Names = new Array();

function loadArray() {
    PageMethods.Load_Array(onSucceeded, onFailed);
}
function onSucceeded(result, userContext, methodName) {
    m_Names = result;
}

function onFailed(error, userContext, methodName) {
    alert("An error occurred")
}

loadArray();

Comments

0

You can use this code:

Sys.Application.add_load(function(e) { LoadArray(); });

Is its name suggest, this is a page_load handler, which pretty much duplicates in functionality is server-side counter part.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.