ASP.NET 从 Javascript 调用函数背后的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16183275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET Call Code Behind Function From Javascript
提问by Bob
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!
我创建了一个 webmethod,我试图从 javascript 调用它,但它似乎没有被触发。我从用户控件内的列表框中获取选定的索引值并将其传递给我的 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
回答by Vijay Singh Rana
I think you should use JSon
to call the web method, here are simple example
我认为你应该使用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");
}
});
And here is the link that may help you Link
这是可以帮助您的链接 Link