ASP.NET调用code函数从JavaScript的背后 [英] ASP.NET Call Code Behind Function From Javascript
问题描述
我做了,我试图从JavaScript调用一个WebMethod,但它似乎并没有被解雇。我抓住从列表框中选择的索引值的用户控件内并将它传递给我的webmethod删除所选的用户。我已经看了无数的网站,并没有找到一个解决办法。我没有得到任何错误,一切似乎是工作。我已经打过电话从公共子这个功能背后还有code,没有运气。任何建议都大大AP preciated!
<%@页面语言=VBAutoEventWireup =假的ClientIDMode =静态codeFILE =Edit.aspx.vb继承=_默认% ><%@注册SRC =AdminEdit.ascx变量名=AdminEdit标签preFIX =UC1%GT;
<%@注册标签preFIX =ASP命名空间=AjaxControlToolkit大会=AjaxControlToolkit%GT;
!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENHTTP://www.w3
.ORG / TR / XHTML1 / DTD / XHTML1-transitional.dtd>
< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
<脚本类型=文/ JavaScript的>
功能YESNO(){
VAR的结果=确认(你确定要删除吗?);
如果(结果==真){
// VAR strUser的= e.options [e.selectedIndex] .value的;
变种E =的document.getElementById('<%= newLb.clientID%GT;');
//变种E =的document.getElementById(ListBox1中);
变种SI = e.selectedIndex;
VAR SV = e.value;
文件撰写(TRUEEEEE);
PageMethods.DeleteUser(SV);
}
其他{
文件撰写(FALSEEEEEE);
}
}
< / SCRIPT>< /头>
<身体GT;
<表ID =form1的=服务器>
< ASP:的ScriptManager ID =A1=服务器的EnablePageMethods =真>< / ASP:ScriptManager的> < DIV> < ASP:超链接ID =HyperLink1=服务器NavigateUrl =〜/的Login.aspx>登录和LT; / ASP:超链接> < / DIV>
< ASP:超链接ID =HyperLink2=服务器
NavigateUrl =〜/为ChangePassword.aspx>修改密码< / ASP:超链接>
< BR />
< ASP:超链接ID =HyperLink3=服务器NavigateUrl =〜/ CreateUser.aspx>创建用户和LT; / ASP:超链接>
< BR />
< ASP:超链接ID =HyperLink4=服务器NavigateUrl =〜/ AddRole.aspx>添加角色和LT; / ASP:超链接>
< BR />
< BR />
< UC1:AdminEdit ID =AdminEdit1=服务器/>
< /表及GT;
< /身体GT;
< / HTML>
公共newLb作为新的ListBox
保护小组的Page_Load(发送者为对象,E作为EventArgs的)把手Me.Load
如果Roles.IsUserInRole(管理员)然后
elseif的Roles.IsUserInRole(编辑),然后
newLb = CTYPE(AdminEdit1.FindControl(ListBox1中),列表框)
万一
结束小组
&所述; System.Web.Services.WebMethod()>
公共共享功能DeleteUser(UNAME BYVAL作为字符串)作为字符串
昏暗u,如的MembershipUser
昏暗newEdit作为新_Default
昏暗_newLb = newEdit.newLb
_newLb.Items.RemoveAt(0)
U = Membership.GetUser(UNAME)
尝试
Membership.DeleteUser(u.UserName)
抓住EX为例外
返回错误:&放大器; ex.Message
结束Try
返回u.IsApproved.ToString
结束功能
我认为你应该使用 JSON
来调用Web方法,这里有简单的例子
$阿贾克斯({
键入:POST,
的contentType:应用/ JSON的;字符集= UTF-8,
网址:yourpage.aspx / yourmethod
数据:{},
数据类型:JSON
成功:功能(数据){
//写功能来显示数据
},
错误:功能(结果){
警报(错误);
}
});
在这里是可以帮助你的链接
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!
<%@ 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
I think you should use JSon
to call the web method, here are simple example
$.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
这篇关于ASP.NET调用code函数从JavaScript的背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!