title: ASP.NET Call Code Behind Function From Javascript
body: <p>I made a webmethod that I'm trying to call from javascript, but it doesn't seem to be firing. I'm grabbing the selected index value from a listbox inside of a usercontrol and passing it to my webmethod to delete the selected user. I've looked at countless sites and haven't found a solution. I'm not getting any errors, everything else seems to be working. I've tried calling this function from a public sub in the code behind also with no luck. Any suggestions are greatly appreciated!</p>
  
  <pre><code>&lt;%@ Page Language="VB" AutoEventWireup="false" ClientIDMode="Static" CodeFile="Edit.aspx.vb" Inherits="_Default" %&gt;&lt;%@ Register src="AdminEdit.ascx" tagname="AdminEdit" tagprefix="uc1" %&gt;
  &lt;%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3
  .org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
          &lt;script type="text/javascript"&gt;
          function YesNo() {
              var result = confirm("Are you sure you want to delete?");
  
  
              if (result == true) {
                  //var strUser = e.options[e.selectedIndex].value;
                  var e = document.getElementById('&lt;%= newLb.clientID %&gt;');
                  //var e = document.getElementById("ListBox1");
                  var si = e.selectedIndex;
                  var sv = e.value;
                  document.write("TRUEEEEE");
                  PageMethods.DeleteUser(sv);
              }
              else {
                  document.write("FALSEEEEEE");
              }
          }
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;asp:ScriptManager ID="A1" runat="server"  EnablePageMethods="true"&gt;&lt;/asp:ScriptManager&gt;
  
      &lt;div&gt;
  
          &lt;asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Login.aspx"&gt;Login&lt;/asp:HyperLink&gt;
  
      &lt;/div&gt;
      &lt;asp:HyperLink ID="HyperLink2" runat="server" 
          NavigateUrl="~/ChangePassword.aspx"&gt;Change Password&lt;/asp:HyperLink&gt;
      &lt;br /&gt;
      &lt;asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/CreateUser.aspx"&gt;Create User&lt;/asp:HyperLink&gt;
      &lt;br /&gt;
      &lt;asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/AddRole.aspx"&gt;Add Roles&lt;/asp:HyperLink&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;uc1:AdminEdit ID="AdminEdit1" runat="server" /&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  Public newLb As New ListBox
  Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
      If Roles.IsUserInRole("admin") Then
      ElseIf Roles.IsUserInRole("editor") Then
          newLb = CType(AdminEdit1.FindControl("ListBox1"), ListBox)
      End If
  End Sub
          &lt;System.Web.Services.WebMethod()&gt;
      Public Shared Function DeleteUser(ByVal uName As String) As String
          Dim u As MembershipUser
          Dim newEdit As New _Default
          Dim _newLb = newEdit.newLb
          _newLb.Items.RemoveAt(0)
          u = Membership.GetUser(uName)
          Try
              Membership.DeleteUser(u.UserName)
          Catch ex As Exception
              Return "Error:" &amp; ex.Message
          End Try
          Return u.IsApproved.ToString
      End Function
  </code></pre>