Search code examples
javascriptasp.netpagemethods

Load a javascript function while page loads


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??


Solution

  • This one should work

    <script language="javascript">
    var m_Names = new Array();
    
    window.onload = function () 
        {
            LoadArray();
    
        }
     .....your functions    
    </script>