Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2012, 12:26 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
Default How about this neat little validation trick?

Here is a neat AJAX/Jquery Validation trick. Tell us what you think
With a default.aspx.cs web form with the code behind of:

Code:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;

public partial class Recipe3 : System.Web.UI.Page 
{
    public static string[] UserNameArray; 

    protected void Page_Load(object sender, EventArgs e)
    {
        UserNameArray = new string[7] { "Imar Spanjaars", "Lori Lopez", "Leah Venegas", "Lisa Bartell", "Ruth Armandariz", "Frances Callan", "Monica Gildone" }; 
    }


    [WebMethod()]
    public static bool CheckUserName(string sUserName)
    {
        foreach (string strUsername in UserNameArray){
            if (sUserName == strUsername)
                return true;
        }

        return false;
    }
}
Then on the front end in the markup section you have:
Code:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Recipe3.aspx.cs" Inherits="Recipe3" %>
<!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>Chapter 6 - Recipe 3: Consuming page methods with AJAX</title>
    <script src="js/jquery-1.4.1.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $("#btnSubmit").click(function(e) {
                e.preventDefault();
                if ($("#txtUserName").val() == '')
                    alert("Please enter the UserName");
                else    
                    sendData($("#txtUserName").val());
            });

            function sendData(sUserName) {
                var loc = window.location.href;
                loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "Recipe3.aspx" : loc;
                $.ajax({
                    type: "POST",
                    url: loc + "/CheckUserName",
                    data: '{"sUserName":"' + sUserName + '"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        if (msg.d)
                            alert("The User Name is valid");
                        else
                            alert("The User Name is invalid")
                    },
                    error: function() {
                        alert("An unexpected error has occurred during processing.");
                    }
                });
            }

        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" Text="Check User Name" />
</div>
    </form>
</body>
</html>
No mention of JSON in your book. Any consideration to using it later on maybe in newer texts for publishing maybe?
 
Old June 22nd, 2012, 05:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
No mention of JSON in your book. Any consideration to using it later on maybe in newer texts for publishing maybe?
JSON is mentioned very briefly on page 357 in Chapter 10 about AJAX.

I have no plans for a broader coverage of JSON in the upcoming edition of the book. I think the Pro edition covers it in more detail.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
A new Jquery trick and a treat for Imar and everyone else. vbboyd BOOK: Beginning ASP.NET 4 : in C# and VB 1 June 9th, 2012 06:32 PM
Properties In User Controls - A Tip And Trick Muhammad Zeeshan ASP.NET 2.0 Basics 0 November 23rd, 2007 03:51 AM
Properties In User Controls - A Tip And Trick Muhammad Zeeshan ASP.NET 2.0 Professional 0 September 20th, 2007 10:49 PM
java applet trick convet to c# chall3ng3r General .NET 0 February 8th, 2005 11:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.