Call Ajax Success Error and Failure function based on ASP.Net RadioButtonList selection

Corobori
 
on Jan 18, 2022 09:49 PM
1313 Views

I have got this jquery which is triggering a calculation.

I want to raise error in the jquery CallMyFunction.

<head runat="server">
      <script
              src="https://code.jquery.com/jquery-3.6.0.min.js"
              integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
              crossorigin="anonymous"></script>
      <script type = "text/javascript">
          var pageUrl = '<%=ResolveUrl("~/WebForm1.aspx")%>'
 
          function CallMyFunction()
          {
                action = $('#opt input:checked').val()               
                $.ajax({
                    type: "POST",
                    url: pageUrl + '/CallMyCode',
                    data: '{action: ' + action + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert("success");
                    },
                    error: function(response) {
                        alert("error");
                    },
                    failure: function (response) {
                        alert("failure");
                    }
                });
          }
      </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>             
            <asp:RadioButtonList ID="opt" runat="server"  >
                <asp:ListItem Selected="True" Value="0">OK</asp:ListItem>
                <asp:ListItem Value="1">Raise error</asp:ListItem>
                <asp:ListItem Value="2">Cause failure</asp:ListItem>
            </asp:RadioButtonList>
            <asp:Button ID="Button1" runat="server" OnClientClick="CallMyFunction();" Text="Button" />
        </div>
    </form>
</body>

 

<System.Web.Services.WebMethod()>
 Public Shared Function CallMyCode(ByVal action As Integer) As String
     If action = 0 Then
         Return "0" ' Should be a success
     Else
         If action = 1 Then
             Return "1" ' Should raise an error in the jquery CallMyFunction
         Else
             Return "2" ' Should cause a failure  in the jquery CallMyFunction
         End If
     End If
 End Function
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jan 20, 2022 05:36 AM

Hi Corobori,

Refer below example.

HTML

<asp:RadioButtonList ID="opt" runat="server">
    <asp:ListItem Selected="True" Value="0">OK</asp:ListItem>
    <asp:ListItem Value="1">Raise error</asp:ListItem>
    <asp:ListItem Value="2">Cause failure</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" OnClientClick="CallMyFunction();" Text="Button" />
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script type="text/javascript">
    var pageUrl = '<%=ResolveUrl("~/Default.aspx")%>'
    function CallMyFunction() {
        action = $('#opt input:checked').val();
        $.ajax({
            type: "POST",
            url: pageUrl + '/CallMyCode',
            data: '{action: ' + action + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response, xhr, status) {
                if (response.d == "0") {
                    alert("success");
                } else if (response.d == "1") {
                    handleError(xhr, status, '');
                } else if (response.d == "2") {
                    handlefailure(xhr, status, '');
                }
            },
            error: handleError,
            failure: handlefailure
        });
    }

    function handleError(xhr, status, error) {
        alert("error");
    }

    function handlefailure(xhr, status, error) {
        alert("failure");
    }
</script>

Code

C#

[System.Web.Services.WebMethod()]
public static string CallMyCode(int action)
{
    if (action == 0)
    {
        return "0"; 
    }
    else if (action == 1)
    {
        return "1";
    }
    else
    {
        return "2";
    }
}

VB.Net

<System.Web.Services.WebMethod()>
Public Shared Function CallMyCode(ByVal action As Integer) As String
    If action = 0 Then
        Return "0"
    ElseIf action = 1 Then
        Return "1"
    Else
        Return "2"
    End If
End Function

Screenshot