在asp.net在JQuery的呼叫服务器侧方法
-
11-09-2019 - |
题
在我的应用程序具有用户其中第一输入他们的用户名的形式。现在我应该检查用户名是否是即时拍摄与否我已经wtitten的方法,“用户名”,这不这whuch返回true或false作为返回类型。 在这里我使用jQuery与Ajax来实现这一目标的概念做。 一旦用户输入这个名字,当他去了第二个文本框输入此代码应得到执行,并给他的结果作为弹出[MODA弹出。如果返回值是从用户名法“用户名”真已在使用中需要“使用alredy用户名”显示消息 如果返回值为false“没有必要显示”
现在我的代码看起来像这样
<head>
<title>Calling an ASP.NET Page Method with jQuery</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "Default.aspx/Username",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: OnSuccess,
error: OnFailure
});
});
function OnSuccess(result)
{
// so here i need to check whethere true or false
// based on that i need to show modal pop up
alert("Success!");
}
function OnFailure (result)
{
alert("The call to the page method failed.");
}
</script>
</head>
在此任何解决方案将是巨大的 谢谢
解决方案
<asp:TextBox id="txtUserName" runat="server"/>
<div id="divPrompt" style="display:none">User Name alredy in use</div>
<input id="otherText"...../>
<script type="text/javascript">
$(document).ready(function(){
$("#<%= txtUserName.ClientID%>").blur(function(){
$.ajax({
type: "POST",
url: "Default.aspx/Username",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: function (msg){
if(msg.hasOwnProperty("d")){
OnSuccess(msg.d);
} else{
OnSuccess(msg);
}
},
error: OnFailure
});
});
});
function OnSuccess(result)
{
if(result.UserNameInUser)
$("div#divPrompt").show();
else
$("div#divPrompt").hide();
}
function OnFailure (result)
{
alert("The call to the page method failed.");
}
</script>
不隶属于 StackOverflow