locked
reference page element from system.Web.Services.WebMethod RRS feed

  • Question

  • User-125499312 posted

    i have the following code:

    <script type="text/javascript">

    PageMethods.backtossjs();

     <System.Web.Services.WebMethod()> _
    Public Shared Function backtossjs()
    Dim pgeCCBS As New CreditCardBlueSnap
    pgeCCBS.divTest2.InnerHtml = Now.ToLongTimeString
    End Function

    when i run it i get his error:

    The server method 'backtossjs' failed with the following error: System.NullReferenceException-- Object reference not set to an instance of an object.

    thx for ur help

    Friday, May 11, 2018 3:10 PM

All replies

  • User283571144 posted

    Hi yzidell,

    According to the error message, it means you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longerinitialized).

    This means the reference is null, and you cannot access members (such as methods) through a null reference. 

    According to your codes, I couldn't directly find the reason why show null error.

    Could you please post the codes about which line show the error?

    Could you please post the details codes about CreditCardBlueSnap?

    Besides, we couldn't reference the page element in the web method.

    When we call the webmethod, we could only get the value from the webmethod, we couldn't modify the control or html element in the web method.

    Normally, it is used to do some data processing.

    We could use ajax at client-side to call the webmethod, the webmethod will send the result as json or other format to the client-side.

    Then we could use javascript or jquery to modify the page's element according to webmethod return data.

    More details about webmethod, you could refer to below article.

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/aspnet-ajax/understanding-asp-net-ajax-web-services 

    Best Regards,

    Brando

    Saturday, May 12, 2018 2:08 AM
  • User465171450 posted

    The Shared keyword means that it is not an instanced. That means it cannot access anything on the page since it lives outside the specific page. Shared, like the static keyword in C#, cannot access any instance variables on a class. 

    You would need to pass any information you want it to operate on, such as data from a textbox, directly to the webmethod.

    Saturday, May 12, 2018 2:29 AM