PageMethod는 쉽고 빠르게 Asp에 접근합니다.Net AJAX 우리는 AJAX의 힘으로 웹 애플리케이션의 사용자 경험과 성능을 쉽게 향상시킬 수 있습니다. AJAX에서 내가 좋아하는 가장 좋은 것들 중 하나는 PageMethod입니다.
PageMethod는 자바 스크립트에서 서버 측 페이지의 메소드를 노출 할 수있는 방법입니다. 이것은 느리고 성가신 포스트 백을 사용하지 않고도 많은 작업을 수행 할 수있는 많은 기회를 제공합니다.
이 게시물에서는 ScriptManager 및 PageMethod의 기본 사용을 보여줍니다. 이 예에서는 사용자 등록 양식을 작성하여 사용자가 그의 이메일 주소 및 비밀번호에 등록 할 수 있습니다.
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width: 200px;">
<asp:Label ID="lblEmailAddress" runat="server" Text="Email Address"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</fieldset>
<div>
</div>
<asp:Button ID="btnCreateAccount" runat="server" Text="Signup" />
</div>
</form>
</body>
</html>
설정 페이지 방법에 먼저 페이지에 스크립트 관리자를 드래그해야 : 여기에 내가 개발하기 위하여려고하고 페이지의 마크 업입니다.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
또한 EnablePageMethods="true"
을 변경했습니다.
그러면 ScriptManager에게 클라이언트 측에서 PageMethods를 호출 할 것임을 알립니다.
이제 다음 단계는 서버 측 기능을 만드는 것입니다. '공공 정적이 방법이어야한다 :
먼저 :
[WebMethod]
public static string RegisterUser(string email, string password)
{
string result = "Congratulations!!! your account has been created.";
if (email.Length == 0)//Zero length check
{
result = "Email Address cannot be blank";
}
else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
{
result = "Not a valid email address";
}
else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
{
result = "Not a valid email address";
}
else if (password.Length == 0)
{
result = "Password cannot be blank";
}
else if (password.Length < 5)
{
result = "Password cannot be less than 5 chars";
}
return result;
}
이 방법은 우리가 두 가지를 확인해야합니다 자바 스크립트를 통해 액세스 할 수 있는지 스크립트 관리자에게 신고하기 :
여기 내가 만든 기능이며,이 기능은 사용자의 입력을 확인합니다 '.
두 번째 : 위 코드에서 설명한대로 [WebMethod] 태그가 메소드 위에 있어야합니다.
이제 계정을 만드는 서버 측 기능을 만들었습니다. 이제 클라이언트 측에서 호출해야합니다. 여기에서 우리는 클라이언트 측에서 해당 함수를 호출 할 수있는 방법입니다 :
<script type="text/javascript">
function Signup() {
var email = document.getElementById('<%=txtEmail.ClientID %>').value;
var password = document.getElementById('<%=txtPassword.ClientID %>').value;
PageMethods.RegisterUser(email, password, onSucess, onError);
function onSucess(result) {
alert(result);
}
function onError(result) {
alert('Cannot process your request at the moment, please try later.');
}
}
</script>
내 서버 측 방법의 등록 사용자를 호출하려면, 스크립트 관리자는 PageMethods에서 사용할 수있는 프록시 기능을 생성합니다.
내 서버 측 함수에는 두 개의 매개 변수, 즉 전자 메일과 암호가 있습니다.이 매개 변수 다음에 메서드가 성공적으로 실행되면 (즉 첫 번째 매개 변수 즉 onSucess) 메서드가 실패하거나 두 번째 매개 변수 즉 결과 인 두 개의 함수 이름을 추가로 제공해야합니다.
이제 모든 것이 준비가 된 것 같습니다. 이제 OnClientClick="Signup();return false;"
을 가입 버튼에 추가했습니다. 내 영문 페이지 그래서 여기에 전체 코드 :
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<fieldset style="width: 200px;">
<asp:Label ID="lblEmailAddress" runat="server" Text="Email Address"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</fieldset>
<div>
</div>
<asp:Button ID="btnCreateAccount" runat="server" Text="Signup" OnClientClick="Signup();return false;" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
function Signup() {
var email = document.getElementById('<%=txtEmail.ClientID %>').value;
var password = document.getElementById('<%=txtPassword.ClientID %>').value;
PageMethods.RegisterUser(email, password, onSucess, onError);
function onSucess(result) {
alert(result);
}
function onError(result) {
alert('Cannot process your request at the moment, please try later.');
}
}
</script>
아마도 가장 쉬운 방법은 jQuery 또는 관련 프레임 워크를 사용하여 AJAX 요청을 서버로 다시 보내는 것입니다. http://api.jquery.com/jQuery.ajax/ 취소 블록에서 요청을 작성하여 섬기는 사람. 더 많은 정보가 필요하면 그냥 물어보십시오. – timothyclifford