开发者

Page Methods throwing Undefined

开发者 https://www.devze.com 2023-01-28 13:34 出处:网络
I am trying to use page methods to call a function and I am getting a \"PageMethods is undefinded\" error. I have used them before whilst using C# but not VB so I\'m wondering if my syntax is wrong as

I am trying to use page methods to call a function and I am getting a "PageMethods is undefinded" error. I have used them before whilst using C# but not VB so I'm wondering if my syntax is wrong as all examples I can find are C#. I am also wondering if it's because my scriptmanager is on the master page?

I've read about this error everywhere but everything I have appears to be correct!

Can anyone point me at what I am doing wrong?

Thanks!

My code:

Part of Master Page

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"  EnablePageMethods="true" >
</asp:ScriptManager>
<div id="header">~~~~~

Content Page HTML

  <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
        function selectZones(commaSeparatedList){
            PageMethods.Zones(commaSeparatedList);
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="navigationPlaceHolder" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentBodyPlaceHolder" runat="server">

<a href='' onclick='selectZones("blah,blah,blah"); return false;'>click here</a>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="functionsMenuPlaceHolder" runat="server">
</asp:Content>

Content Page Code Behind

    Imports System.Web.Services

Public Class TestClass
    Inherits BaseClass

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    <WebMethod()>
    Protected Shared Sub Zones(ByVal zones As String)
        HttpContext.Current.Response.Writ开发者_JAVA百科e("test = " & zones)
    End Sub
End Class


I believe you have issue where the "selectZones" method is running before the scripts from script manager are completing to run. I suggest you try to put call into document ready or event and see if call works at this time. Scripts will run when they are seen by client browser, and if downloaded like script manager other scripts later in page will run first.


Finally worked it out. Nothing to do with the script manager or anything like that.

My method in the code behind needed to be public so now reads as follows:

 <WebMethod()> 
Public Shared Sub Zones(ByVal zones As String) 
    HttpContext.Current.Response.Write("test = " & zones) 
End Sub 

Did not realise this was needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消