Question

I need to send a script to the page from WebMethod used by Ajax that fires when click HTML link. I couldn't use the script manager with "Me" or "Page" control and can't reference any controls.

I just need to return that session is nothing , Any Ideas?

The button clicked to send Ajax is HTML link and all I need to check if session expired (which I can check it on load) so if it's expired want to alert user since I already don't complete the process after checking it in code behind

<WebMethod()> _
Public Shared Function Send(ByVal data As String) As String

    If Not System.Web.HttpContext.Current.Session("MemberID") Is Nothing Then

            Try

               ''''' my code
               ''''''''''''''''''''''

            If Not System.Web.HttpContext.Current.Session("MemberID") Is Nothing Then
                Return "Success"
            Else
                Return "noSession"

            End If

        Catch ex As Exception

            Return "failure"

        End Try
    Else
        ScriptManager.RegisterStartupScript(Me, GetType(String), "Checkeng", [String].Format("LevelsMsg();"), True)
    End If

End Function

JQuery Ajax

It's more complecated but I thinkk this is the main part:

$(document).on("click", "#Add", function() {
        var _fulldata = $('#basket').html();
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: 'Order.aspx/SendOrder',
            data: "{'fulldata':'" + _fulldata + "'}",
            async: false,
            success: function(response) {

            },
            error: function() {
                alert("There is a problem in saving data");
            }
        });

});
Was it helpful?

Solution

Your WebMethodis a Shared function which is equivalent to a Static function in C#. This means you will not be able to access any variables other than those declared inside of this Shared function. However, the nature of WebMethods allow a return to "its" caller via "Success" or "error" which can be "intercepted". Thus, no need to use ScriptManager.RegisterStartupScript since your POST will return to AJAX realm anyway, which means you can call any JavaScript function there.

You could Change your code this way:

VB.NET Code-Behind:

<WebMethod()> _
Public Shared Function Send(ByVal data As String) As String
    If Not System.Web.HttpContext.Current.Session("MemberID") Is Nothing Then
        Try
            ' YOUR CODE        
            Return "Success"
        Catch ex As Exception
            Return "Failure"
        End Try
    Else
        Return "NoSession";
    End If
End Function

JavaScript:

$(document).on("click", "#Add", function() {
    var _fulldata = $('#basket').html();
    $.ajax({
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        url: 'Order.aspx/SendOrder',
        data: "{'fulldata':'" + _fulldata + "'}",
        async: false,
        success: function(response) {
            /* since we put "NoSession", then we should check for it here */
            if(response.d == "NoSession") {
                /* This is where you "replace" the use of RegisterStartupScript 
                   by safely calling any JS function here */
                LevelsMsg();
            }
        },
        error: function() {
            alert("There is a problem in saving data");
        }
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top