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