从Javascript函数背后的ASP.NET调用代码

时间:2013-04-24 04:16:24

标签: javascript asp.net webmethod

我做了一个网络方法,我试图通过javascript调用,但它似乎没有被解雇。我从usercontrol中的列表框中抓取所选索引值,并将其传递给我的webmethod以删除所选用户。我看过无数的网站,但没有找到解决方案。我没有收到任何错误,其他一切似乎都有效。我已经尝试从后面的代码中的公共子调用此函数也没有运气。非常感谢任何建议!

<%@ Page Language="VB" AutoEventWireup="false" ClientIDMode="Static" CodeFile="Edit.aspx.vb" Inherits="_Default" %><%@ Register src="AdminEdit.ascx" tagname="AdminEdit" tagprefix="uc1" %>
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3
.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
        <script type="text/javascript">
        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('<%= newLb.clientID %>');
                //var e = document.getElementById("ListBox1");
                var si = e.selectedIndex;
                var sv = e.value;
                document.write("TRUEEEEE");
                PageMethods.DeleteUser(sv);
            }
            else {
                document.write("FALSEEEEEE");
            }
        }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="A1" runat="server"  EnablePageMethods="true"></asp:ScriptManager>

    <div>

        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Login.aspx">Login</asp:HyperLink>

    </div>
    <asp:HyperLink ID="HyperLink2" runat="server" 
        NavigateUrl="~/ChangePassword.aspx">Change Password</asp:HyperLink>
    <br />
    <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/CreateUser.aspx">Create User</asp:HyperLink>
    <br />
    <asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/AddRole.aspx">Add Roles</asp:HyperLink>
        <br />
        <br />
        <uc1:AdminEdit ID="AdminEdit1" runat="server" />
    </form>
</body>
</html>
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
        <System.Web.Services.WebMethod()>
    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:" & ex.Message
        End Try
        Return u.IsApproved.ToString
    End Function

1 个答案:

答案 0 :(得分:3)

我认为您应该使用JSon来调用Web方法,这里是简单的示例

 $.ajax({
 type: "POST",
 contentType: "application/json; charset=utf-8",
 url: "yourpage.aspx/yourmethod",
 data: "{}",
 dataType: "json",
 success: function(data) {
 //Write functionality to display data
 },
 error: function(result) {
 alert("Error");
 }
 });

以下链接可以帮助您Link