Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

AJAX postback? Javascript call to .Net code?

Back to Forum: Tech Off
  • User profile image
    qwert231

    I have an entry page that needs to prompt the user some times (with a javascript confirm) but not always. Right now, I have a javascript onclick event on the button that calls a PageMethod. (Let's say it's called 'CheckToPrompt'.)

    CheckToPrompt checks my database and returns a 'yes' or 'no' to my javascript 'CheckSuccess' function.

     

    I was able to get my code to work in the shared/static web method for checking if I need to prompt, but the javascript 'CheckSuccess' needs to raise a .Net event if the user confirms. Since an event raise can't be in a shared/static method, I have this function do a PostBack.

     

    I'd rather not have to do a whole PostBack, but raise the event the same way it would if I left the button alone, and let it do an Ajax postback. (This was the original design before the prompting requirement.)

     

    Here's my desired logic flow:

    User clicks 'Add'

    .Net checks for existing records

    If no existing records, Raise the 'Add' event

    If existing records, prompt with javascript.confirm

       If confirm == 'yes' raise the Add event

     

    Here's the current code, which works, but with a full postback at the end:

    ' .Net

    btnAdd.Attributes.Add("onclick", "javascript:DoAdd('" & ddAddCType.ClientID & "', '" & _
        txtAddCitationNumber.ClientID & "', '" & txtCitRange.ClientID & "', '" & txtCitAmount.ClientID & "')")

    // JScript File

    function DoAdd(typeId, addNumId, rangeId, amountId)
    {
        var citType = document.getElementById(typeId).value;
        var startNum = document.getElementById(addNumId).value;
        var citRange = document.getElementById(rangeId).value; //txtCitRange
        var citQty = document.getElementById(amountId).value; //txtCitAmount
        var endNum;
        if (citRange != '')
        { endNum = citRange; }
        else
        { endNum = startNum + citQty; }
        PageMethods.CheckIfCitationsExist(citType, startNum, endNum, CheckSuccess, CallFailed);
    }

     

    '.Net

            <System.Web.Services.WebMethod()> _
            Public Shared Function CheckIfCitationsExist(ByVal citType As String, ByVal startNum As String, ByVal endNum As String) As String
                Dim thisCitations As New IAppsData.Law.Citations
                If IsNumeric(startNum) AndAlso IsNumeric(endNum) Then
                    If thisCitations.CheckBatch(citType, CType(startNum, Integer), CType(endNum, Integer)) Then
                        Return "yes"
                    Else
                        Return "no"
                    End If
                Else
                    Return "no"
                End If

            End Function

     

    // JScript File

    function CheckSuccess(res)
            {  
                if (res == 'yes')
                {
                    if (confirm("Citations have already been assigned in this range. Are you sure?"))
                    {
                        <%= PostBackStr %>
                    }
                }
                else
                {
                    <%= PostBackStr %>
                }
            }

     

    '.Net

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                PostBackStr = Page.ClientScript.GetPostBackEventReference(Me, "AddArg")
                If Page.IsPostBack Then
                    Dim eventArg As String = Request("__EVENTARGUMENT")
                    If eventArg = "AddArg" Then
                        RaiseEvent Add(sender, e)
                    End If
                End If
            End Sub

     

     

     

    Can you see what I am doing, and what I want to do?

  • User profile image
    Charles

    I don't see why you can't do this entirely from JS, as long as the request goes to a server on the same domain that sends down the script... Use an async XmlHttpRequest (aka Ajax...) to run the server query that checks for records in some db. The URL you use for the Ajax request could be to a web service method call that returns "yes" or "no" or 1 or 0 (whatever...), then there is no postback at all.

    C

  • User profile image
    PerfectPhase

    Charles said:

    I don't see why you can't do this entirely from JS, as long as the request goes to a server on the same domain that sends down the script... Use an async XmlHttpRequest (aka Ajax...) to run the server query that checks for records in some db. The URL you use for the Ajax request could be to a web service method call that returns "yes" or "no" or 1 or 0 (whatever...), then there is no postback at all.

    C

    Agreed, do it all in javascript calling back to page methods or a wcf service.

     

    What does the server-side Add event do?

  • User profile image
    qwert231

    Charles said:

    I don't see why you can't do this entirely from JS, as long as the request goes to a server on the same domain that sends down the script... Use an async XmlHttpRequest (aka Ajax...) to run the server query that checks for records in some db. The URL you use for the Ajax request could be to a web service method call that returns "yes" or "no" or 1 or 0 (whatever...), then there is no postback at all.

    C

    Well, the check that returns yes or no, is ajax, but as a Page Method, not a Web Method. It's the Raise Event that isn't. The Add event triggers the page's controller to either Insert or Update based on page properties that the user has selected.

  • User profile image
    SlackmasterK

    Why not just put your add button inside an updatepanel?

  • User profile image
    qwert231

    SlackmasterK said:

    Why not just put your add button inside an updatepanel?

    I wouldn't mind... but I need to prompt the user in some instances. Originally the code only had the button in the UpdatePanel, and it would run the code without any checks. I can have the code do the checks, but then how do I prompt the user in the server side code?

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.