Tuesday, 15 January 2013

asp.net - how to call C# function with javascript in .NET ? -



asp.net - how to call C# function with javascript in .NET ? -

basically im next tutorial from:

how phone call asp.net c# method using javascript

this code create userform , prompt user come in name , password.

i created webform called default.aspx

<!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 request @ moment, please seek later.'); } } </script>

and under default.aspx.cs have

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace email1 { public partial class _default : page { [webmethod] public static string registeruser(string email, string password) { string result = "congratulations!!! business relationship has been created."; if (email.length == 0)//zero length check { result = "email address cannot blank"; } else if (!email.contains(".") || !email.contains("@")) //some other basic checks { result = "not valid email address"; } else if (!email.contains(".") || !email.contains("@")) //some other basic checks { result = "not valid email address"; } else if (password.length == 0) { result = "password cannot blank"; } else if (password.length < 5) { result = "password canonot less 5 chars"; } homecoming result; } } } }

c# asp.net

No comments:

Post a Comment