Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="JQueryUserNameAvailability._Default" %>
<!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>UserName Availability</title>
<script language="javascript" type="text/javascript" src="jquery-1.2.6.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter UserName: <input type="text" id="txtUserName" />
Password: <input type="password" />
<br />
<br />
<div id="display" style="width:100px; font-family:Verdana; padding:5px 5px 5px 5px; font-weight:bold; font-size:14px"></div>
</div>
</form>
</body>
</html>
<script language="javascript" type="text/javascript">
var userName = '';
$(document).ready(function()
{
$("#txtUserName").blur(function()
{
userName = $(this).val();
if(userName.length <= 6)
{
$("#display").text("username must be atleast 7 characters");
$("#display").css("background-color","red");
}
else
{
$.ajax(
{
type:"POST",
url:"AjaxService.asmx/CheckUserNameAvailability",
data:"{\"userName\":\"" + userName + "\"}",
dataType:"json",
contentType:"application/json",
success: function(response)
{
if(response.d == true)
{
$("#display").text("username is available");
$("#display").css("background-color","lightgreen");
}
else
{
$("#display").text("username is already taken");
$("#display").css("background-color","red");
}
}
});
}
});
});
</script>
AjaxService.asmx
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
namespace JQueryUserNameAvailability
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AjaxService : System.Web.Services.WebService
{
[WebMethod]
public bool CheckUserNameAvailability(string userName)
{
List<String> userNames = new List<string>() { "azamsharp", "johndoe", "marykate", "alexlowe", "scottgu" };
var user = (from u in userNames
where u.ToLower().Equals(userName.ToLower())
select u).SingleOrDefault<String>();
return String.IsNullOrEmpty(user) ? true : false;
}
}
}
Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts
Jun 21, 2010
JQuery Example For validations
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>JQuery Example www.dotnetspark.com</title>
<style type="text/css">
label.error { color: red; vertical-align: top; }
.style1
{
font-size: xx-large;
font-weight: bold;
}
</style>
<script type="text/javascript" src="script/jquery-1.3.2.js"></script>
<script type="text/javascript" src="script/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#form1").validate({
rules: {
<%=txtUserName.UniqueID %>: {
minlength: 5,
required: true
},
<%=txtPassword.UniqueID %>: {
minlength: 5,
required: true
},
<%=txtEmail.UniqueID %>: {
required: true
},
<%=txtURL.UniqueID %>: {
required: true
}
}, messages: {
<%=txtUserName.UniqueID %>:{
required: "Plaese Enter your username",
minlength: "User name must be atleaet of 5 characters"
},
<%=txtPassword.UniqueID %>:{
required: "Plaese Enter your password",
minlength: "Password must be atleaet of 5 characters"
},
<%=txtEmail.UniqueID %>:{
required: "Plaese Enter your Email Id",
},
<%=txtURL.UniqueID %>:{
required: "Plaese Enter Website URL",
}
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table style="width:100%;">
<tr>
<td colspan="3" align="center" class="style1">
JQuery Validation Example</td>
</tr>
<tr>
<td colspan="2" align="center">
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblUserName" runat="server" Text="User Name :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblPassword" runat="server" Text="Password :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblEmail" runat="server" Text="Email Id :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmail" CssClass="email" runat="server"
Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblURL" runat="server" Text="URL :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtURL" CssClass="url" runat="server" Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
<!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>JQuery Example www.dotnetspark.com</title>
<style type="text/css">
label.error { color: red; vertical-align: top; }
.style1
{
font-size: xx-large;
font-weight: bold;
}
</style>
<script type="text/javascript" src="script/jquery-1.3.2.js"></script>
<script type="text/javascript" src="script/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#form1").validate({
rules: {
<%=txtUserName.UniqueID %>: {
minlength: 5,
required: true
},
<%=txtPassword.UniqueID %>: {
minlength: 5,
required: true
},
<%=txtEmail.UniqueID %>: {
required: true
},
<%=txtURL.UniqueID %>: {
required: true
}
}, messages: {
<%=txtUserName.UniqueID %>:{
required: "Plaese Enter your username",
minlength: "User name must be atleaet of 5 characters"
},
<%=txtPassword.UniqueID %>:{
required: "Plaese Enter your password",
minlength: "Password must be atleaet of 5 characters"
},
<%=txtEmail.UniqueID %>:{
required: "Plaese Enter your Email Id",
},
<%=txtURL.UniqueID %>:{
required: "Plaese Enter Website URL",
}
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table style="width:100%;">
<tr>
<td colspan="3" align="center" class="style1">
JQuery Validation Example</td>
</tr>
<tr>
<td colspan="2" align="center">
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblUserName" runat="server" Text="User Name :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblPassword" runat="server" Text="Password :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblEmail" runat="server" Text="Email Id :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmail" CssClass="email" runat="server"
Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblURL" runat="server" Text="URL :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtURL" CssClass="url" runat="server" Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
Labels:
Jquery,
validations
Registration form validation using jQuery
by Developer
1. March 2010 05:28
Mainly we will use javascript for validating the several fields of
our webpage. For example we have registration page with several fields
like username, mobile number, email, date of birth and gender. We have
to handle each filed validation in a different way. Here we are going to
learn how to validate simple textbox, radio button and checkbox.
jQuery provides us simple way to validate each filed. Here I am explaining validation procedure for each filed.
Below is the code to validate our simple registration form fields.
<html>
<head>
<title>validating the registration form fields using jQuery</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#btn_submit').click(function(event) {
//validating username
if ($('#user_name').val().length < 1) {
alert('Please enter user name');
$('#user_name').focus();
return false;
}
if (!is_username($('#user_name').val())) {
alert('Please enter valid user name');
$('#user_name').focus();
return false;
}
function is_username(uname) {
var pattern = new RegExp(/^[a-z0-9_]+$/);
return pattern.test(uname);
}
//validating mobile number
if ($('#mobile_number').val().length < 1) {
alert('Please enter your mobile number');
$('#mobile_number').focus();
return false;
}
if ((!is_mobile($('#mobile_number').val())) || ($('#mobile_number').val().length < 10)) {
alert('Please enter valid mobile number');
$('#mobile_number').focus();
return false;
}
function is_mobile(mobile) {
var pattern = new RegExp(/^[0-9]+$/);
return pattern.test(mobile);
}
//validating email address
if ($('#email_id').val().length < 1) {
alert('Please enter your Email Id');
$('#email_id').focus();
return false;
}
if (!is_email($('#email_id').val())) {
alert('Please enter valid Email Id');
$('#email_id').focus();
return false;
}
function is_email(emailid) {
var pattern = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
return pattern.test(emailid);
}
//validating date of birth
if ($('#dob').val().length < 1) {
alert('Please enter your Date of birth');
$('#dob').focus();
return false;
}
if (!is_dob($('#dob').val())) {
alert('Please enter valid date of birth');
$('#dob').focus();
return false;
}
function is_dob(dob) {
var pattern = new RegExp(/\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/);
return pattern.test(dob);
}
//validating gender
if ($("input[name='rdgender']:checked").val() != 'M' && $("input[name='rdgender']:checked").val() != 'F') {
alert('Please select your gender');
return false;
}
//validating checkbox
if ($('#chk_tc:checked').val() == null) {
alert('Please agree terms and conditions');
return false;
}
});
});
</script>
</head>
<body>
<div style="margin-left:300px;">
<label>User Name</label><input type="text" id="user_name"/><br/><br/>
<label>Mobile Number</label><input type="text" id="mobile_number"/><br/><br/>
<label>Email Id</label><input type="text" id="email_id"/><br/><br/>
<label>Date Of Birth</label><input type="text" id="dob"/><br/><br/>
<label>Gender</label>
<table id="rdgender" border="0">
<tbody>
<tr>
<td>
<input id="rdgender_0" name="rdgender" value="M" type="radio"/>
<label for="rdgender_0">Male</label>
</td>
<td>
<input id="rdgender_1" name="rdgender" value="F" type="radio"/>
<label for="rdgender_1">Female</label>
</td>
</tr>
</tbody>
</table>
<br/><br/>
<input type="checkbox" id="chk_tc"/><label>I agree the Terms & Condition</label><br/><br/>
<input type="submit" id="btn_submit" value="Submit"/>
</div>
</body>
</html>
In the above example we have simple registration form with several fields like username, mobile number, email, date of birth and gender. We will discuss validation procedure for each field separately.
In the first step we bind the our validation function for submit button through click event using $('#btn_submit').click(function(event) {. Inside this function we define the validation methods for each filed
Validating the user name field:
//validating username
if ($('#user_name').val().length < 1) {
alert('Please enter user name');
$('#user_name').focus();
return false;
}
if (!is_username($('#user_name').val())) {
alert('Please enter valid user name');
$('#user_name').focus();
return false;
}
function is_username(uname) {
var pattern = new RegExp(/^[a-z0-9_]+$/);
return pattern.test(uname);
}
By using above code we are validating the user name filed. It is simple textbox, first we are checking whether user entered anything in the user name field or not by checking its length. After that we are restricting the user to enter only alphabetic, underscore(_) and numbers within the user name filed by passing user name field value o the RegExp(/^[a-z0-9_]+$/);
Validating mobile number:
//validating mobile number
if ($('#mobile_number').val().length < 1) {
alert('Please enter your mobile number');
$('#mobile_number').focus();
return false;
}
if ((!is_mobile($('#mobile_number').val())) || ($('#mobile_number').val().length < 10)) {
alert('Please enter valid mobile number');
$('#mobile_number').focus();
return false;
}
function is_mobile(mobile) {
var pattern = new RegExp(/^[0-9]+$/);
return pattern.test(mobile);
}
For the mobile number field, we have to allow only numbers and length should be minimum of 10.
Validating Email Id:
//validating email address
if ($('#email_id').val().length < 1) {
alert('Please enter your Email Id');
$('#email_id').focus();
return false;
}
if (!is_email($('#email_id').val())) {
alert('Please enter valid Email Id');
$('#email_id').focus();
return false;
}
function is_email(emailid) {
var pattern = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
return pattern.test(emailid);
}
There are some specific conditions for email id like email id must have @ and .(dot). We are validating the Email Id by using RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
Validating the date of birth:
//validating date of birth
if ($('#dob').val().length < 1) {
alert('Please enter your Date of birth');
$('#dob').focus();
return false;
}
if (!is_dob($('#dob').val())) {
alert('Please enter valid date of birth');
$('#dob').focus();
return false;
}
function is_dob(dob) {
var pattern = new RegExp(/\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/);
return pattern.test(dob);
}
By using above code we are validating the date of birth. Date of birth must be like dd/mm/yy(day/month/year). We are validating the date of birth field by using RegExp(/\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/);
Validating the radio button (gender):
//validating gender
if ($("input[name='rdgender']:checked").val() != 'M' && $("input[name='rdgender']:checked").val() != 'F') {
alert('Please select your gender');
return false;
}
We have two radio buttons to ask user about his gender. We are validating radio buttons based on user input. We can validate radio button, that means gender field by comparing its value using $("input[name='rdgender']:checked").val(). It should be "M" or "F".
Validating checkbox:
//validating checkbox
if ($('#chk_tc:checked').val() == null) {
alert('Please agree terms and conditions');
return false;
}
At the end of your registration form, you need to have terms & conditions. User must accept your terms & conditions. Here you have checkbox, user must check the checkbox. You can validate checkbox by using its value $('#chk_tc:checked').val(). It should not be null.
In this way you can easily validate your registration form by using jQuery javascript functions.
jQuery provides us simple way to validate each filed. Here I am explaining validation procedure for each filed.
Below is the code to validate our simple registration form fields.
<html>
<head>
<title>validating the registration form fields using jQuery</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#btn_submit').click(function(event) {
//validating username
if ($('#user_name').val().length < 1) {
alert('Please enter user name');
$('#user_name').focus();
return false;
}
if (!is_username($('#user_name').val())) {
alert('Please enter valid user name');
$('#user_name').focus();
return false;
}
function is_username(uname) {
var pattern = new RegExp(/^[a-z0-9_]+$/);
return pattern.test(uname);
}
//validating mobile number
if ($('#mobile_number').val().length < 1) {
alert('Please enter your mobile number');
$('#mobile_number').focus();
return false;
}
if ((!is_mobile($('#mobile_number').val())) || ($('#mobile_number').val().length < 10)) {
alert('Please enter valid mobile number');
$('#mobile_number').focus();
return false;
}
function is_mobile(mobile) {
var pattern = new RegExp(/^[0-9]+$/);
return pattern.test(mobile);
}
//validating email address
if ($('#email_id').val().length < 1) {
alert('Please enter your Email Id');
$('#email_id').focus();
return false;
}
if (!is_email($('#email_id').val())) {
alert('Please enter valid Email Id');
$('#email_id').focus();
return false;
}
function is_email(emailid) {
var pattern = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
return pattern.test(emailid);
}
//validating date of birth
if ($('#dob').val().length < 1) {
alert('Please enter your Date of birth');
$('#dob').focus();
return false;
}
if (!is_dob($('#dob').val())) {
alert('Please enter valid date of birth');
$('#dob').focus();
return false;
}
function is_dob(dob) {
var pattern = new RegExp(/\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/);
return pattern.test(dob);
}
//validating gender
if ($("input[name='rdgender']:checked").val() != 'M' && $("input[name='rdgender']:checked").val() != 'F') {
alert('Please select your gender');
return false;
}
//validating checkbox
if ($('#chk_tc:checked').val() == null) {
alert('Please agree terms and conditions');
return false;
}
});
});
</script>
</head>
<body>
<div style="margin-left:300px;">
<label>User Name</label><input type="text" id="user_name"/><br/><br/>
<label>Mobile Number</label><input type="text" id="mobile_number"/><br/><br/>
<label>Email Id</label><input type="text" id="email_id"/><br/><br/>
<label>Date Of Birth</label><input type="text" id="dob"/><br/><br/>
<label>Gender</label>
<table id="rdgender" border="0">
<tbody>
<tr>
<td>
<input id="rdgender_0" name="rdgender" value="M" type="radio"/>
<label for="rdgender_0">Male</label>
</td>
<td>
<input id="rdgender_1" name="rdgender" value="F" type="radio"/>
<label for="rdgender_1">Female</label>
</td>
</tr>
</tbody>
</table>
<br/><br/>
<input type="checkbox" id="chk_tc"/><label>I agree the Terms & Condition</label><br/><br/>
<input type="submit" id="btn_submit" value="Submit"/>
</div>
</body>
</html>
In the above example we have simple registration form with several fields like username, mobile number, email, date of birth and gender. We will discuss validation procedure for each field separately.
In the first step we bind the our validation function for submit button through click event using $('#btn_submit').click(function(event) {. Inside this function we define the validation methods for each filed
Validating the user name field:
//validating username
if ($('#user_name').val().length < 1) {
alert('Please enter user name');
$('#user_name').focus();
return false;
}
if (!is_username($('#user_name').val())) {
alert('Please enter valid user name');
$('#user_name').focus();
return false;
}
function is_username(uname) {
var pattern = new RegExp(/^[a-z0-9_]+$/);
return pattern.test(uname);
}
By using above code we are validating the user name filed. It is simple textbox, first we are checking whether user entered anything in the user name field or not by checking its length. After that we are restricting the user to enter only alphabetic, underscore(_) and numbers within the user name filed by passing user name field value o the RegExp(/^[a-z0-9_]+$/);
Validating mobile number:
//validating mobile number
if ($('#mobile_number').val().length < 1) {
alert('Please enter your mobile number');
$('#mobile_number').focus();
return false;
}
if ((!is_mobile($('#mobile_number').val())) || ($('#mobile_number').val().length < 10)) {
alert('Please enter valid mobile number');
$('#mobile_number').focus();
return false;
}
function is_mobile(mobile) {
var pattern = new RegExp(/^[0-9]+$/);
return pattern.test(mobile);
}
For the mobile number field, we have to allow only numbers and length should be minimum of 10.
Validating Email Id:
//validating email address
if ($('#email_id').val().length < 1) {
alert('Please enter your Email Id');
$('#email_id').focus();
return false;
}
if (!is_email($('#email_id').val())) {
alert('Please enter valid Email Id');
$('#email_id').focus();
return false;
}
function is_email(emailid) {
var pattern = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
return pattern.test(emailid);
}
There are some specific conditions for email id like email id must have @ and .(dot). We are validating the Email Id by using RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
Validating the date of birth:
//validating date of birth
if ($('#dob').val().length < 1) {
alert('Please enter your Date of birth');
$('#dob').focus();
return false;
}
if (!is_dob($('#dob').val())) {
alert('Please enter valid date of birth');
$('#dob').focus();
return false;
}
function is_dob(dob) {
var pattern = new RegExp(/\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/);
return pattern.test(dob);
}
By using above code we are validating the date of birth. Date of birth must be like dd/mm/yy(day/month/year). We are validating the date of birth field by using RegExp(/\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/);
Validating the radio button (gender):
//validating gender
if ($("input[name='rdgender']:checked").val() != 'M' && $("input[name='rdgender']:checked").val() != 'F') {
alert('Please select your gender');
return false;
}
We have two radio buttons to ask user about his gender. We are validating radio buttons based on user input. We can validate radio button, that means gender field by comparing its value using $("input[name='rdgender']:checked").val(). It should be "M" or "F".
Validating checkbox:
//validating checkbox
if ($('#chk_tc:checked').val() == null) {
alert('Please agree terms and conditions');
return false;
}
At the end of your registration form, you need to have terms & conditions. User must accept your terms & conditions. Here you have checkbox, user must check the checkbox. You can validate checkbox by using its value $('#chk_tc:checked').val(). It should not be null.
In this way you can easily validate your registration form by using jQuery javascript functions.
Labels:
Jquery,
validations
Inserting data with out page refresh using jquery,json,asp.net
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="register.aspx.cs" Inherits="jquery.register" %>
<!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>test Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
<script src="jquery-1.2.6.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#btnsubmit").click(function(){
// alert($("#txtname").val());
// alert($("#txtemail").val());
// alert($("#txtmobile").val());
var name = $("#txtname").val();
var email = $("#txtemail").val();
var mobile = $("#txtmobile").val();
var options = {
type: "POST",
url: "register.aspx/login",
data: "{name:'" + name + "',email:'" + email + "',mobile:'" + mobile + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(response) {
alert(response.d);
},
error: function(request, status, errorThrown) {
alert(status);
}
};
$.ajax(options);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table class="style1">
<tr>
<td align="right">
Name:
</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
Email:
</td>
<td>
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
Mobile:
</td>
<td>
<asp:TextBox ID="txtmobile" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>
code:
using System;
using System.Collections;
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;
using System.Data.SqlClient;
namespace jquery
{
public partial class register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string login(string name,string email,string mobile)
{
string response = "";
string name1 = name;
string email1 = email;
string mobile1 = mobile;
SqlConnection con = new SqlConnection("user id=sa;password=sa;database=pavan;data source=localhost");
string s="insert into reg(name,username,hobies)values('"+name+"','"+email+"','"+mobile+"')";
SqlCommand cmd = new SqlCommand(s, con);
con.Open();
int s1= cmd.ExecuteNonQuery();
con.Close();
if (s1 == 1)
{
response = "Inserted";
}
return response;
}
}
}
<!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>test Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
<script src="jquery-1.2.6.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#btnsubmit").click(function(){
// alert($("#txtname").val());
// alert($("#txtemail").val());
// alert($("#txtmobile").val());
var name = $("#txtname").val();
var email = $("#txtemail").val();
var mobile = $("#txtmobile").val();
var options = {
type: "POST",
url: "register.aspx/login",
data: "{name:'" + name + "',email:'" + email + "',mobile:'" + mobile + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(response) {
alert(response.d);
},
error: function(request, status, errorThrown) {
alert(status);
}
};
$.ajax(options);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table class="style1">
<tr>
<td align="right">
Name:
</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
Email:
</td>
<td>
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
Mobile:
</td>
<td>
<asp:TextBox ID="txtmobile" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>
code:
using System;
using System.Collections;
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;
using System.Data.SqlClient;
namespace jquery
{
public partial class register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string login(string name,string email,string mobile)
{
string response = "";
string name1 = name;
string email1 = email;
string mobile1 = mobile;
SqlConnection con = new SqlConnection("user id=sa;password=sa;database=pavan;data source=localhost");
string s="insert into reg(name,username,hobies)values('"+name+"','"+email+"','"+mobile+"')";
SqlCommand cmd = new SqlCommand(s, con);
con.Open();
int s1= cmd.ExecuteNonQuery();
con.Close();
if (s1 == 1)
{
response = "Inserted";
}
return response;
}
}
}
Jun 17, 2010
Using jQuery in ASP.Net AJAX Applications with out page refresh
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script src="jquery-1.2.6.js" type="text/javascript"></script>
<script src="_scripts/jquery-1.2.6.js" type="text/javascript"></script>
<script language="javascript">
$(document).ready(function() {
$("#txtNoOfMales").change(function() {
var ticketRequired = this.value;
var options = {
type: "POST",
url: "CallServerWithParameters.aspx/GetAvailableTicketsForMales",
data: "{no:" + ticketRequired + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
if (response.d != "") {
alert(response.d);
$("#txtNoOfMales").focus();
}
}
};
//Call the PageMethods
$.ajax(options);
});
$("#txtNoOfFemales").change(function() {
var ticketRequired = this.value;
var options = {
type: "POST",
url: "CallServerWithParameters.aspx/GetAvailableTicketsForFemales",
data: "{no:" + ticketRequired + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
if (response.d != "") {
alert(response.d);
$("#txtNoOfFemales").focus();
}
}
};
//Call the PageMethods
$.ajax(options);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
No of Male Tickets:<asp:TextBox ID="txtNoOfMales" runat="server"></asp:TextBox>
No of Female Tickets:<asp:TextBox ID="txtNoOfFemales" runat="server" ></asp:TextBox>
</div>
</div>
</form>
</body>
</html>
code behind
.cs
using System;
using System.Collections;
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;
namespace jquery
{
public partial class CallServerWithParameters : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetAvailableTicketsForMales(int no)
{
string result = "";
int NoOfTicketsAvailable = 10;
if (no > NoOfTicketsAvailable)
{
result = "Only " + NoOfTicketsAvailable.ToString() + " Male ticket(s) avaialable. Please enter a lower number!";
}
return result;
}
[WebMethod]
public static string GetAvailableTicketsForFemales(int no)
{
string result = "";
int NoOfTicketsAvailable = 10;
if (no > NoOfTicketsAvailable)
{
result = "Only " + NoOfTicketsAvailable.ToString() + " Female ticket(s) avaialable. Please eneter a lower number!";
}
return result;
}
}
}
<head runat="server">
<title>Untitled Page</title>
<script src="jquery-1.2.6.js" type="text/javascript"></script>
<script src="_scripts/jquery-1.2.6.js" type="text/javascript"></script>
<script language="javascript">
$(document).ready(function() {
$("#txtNoOfMales").change(function() {
var ticketRequired = this.value;
var options = {
type: "POST",
url: "CallServerWithParameters.aspx/GetAvailableTicketsForMales",
data: "{no:" + ticketRequired + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
if (response.d != "") {
alert(response.d);
$("#txtNoOfMales").focus();
}
}
};
//Call the PageMethods
$.ajax(options);
});
$("#txtNoOfFemales").change(function() {
var ticketRequired = this.value;
var options = {
type: "POST",
url: "CallServerWithParameters.aspx/GetAvailableTicketsForFemales",
data: "{no:" + ticketRequired + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
if (response.d != "") {
alert(response.d);
$("#txtNoOfFemales").focus();
}
}
};
//Call the PageMethods
$.ajax(options);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
No of Male Tickets:<asp:TextBox ID="txtNoOfMales" runat="server"></asp:TextBox>
No of Female Tickets:<asp:TextBox ID="txtNoOfFemales" runat="server" ></asp:TextBox>
</div>
</div>
</form>
</body>
</html>
code behind
.cs
using System;
using System.Collections;
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;
namespace jquery
{
public partial class CallServerWithParameters : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetAvailableTicketsForMales(int no)
{
string result = "";
int NoOfTicketsAvailable = 10;
if (no > NoOfTicketsAvailable)
{
result = "Only " + NoOfTicketsAvailable.ToString() + " Male ticket(s) avaialable. Please enter a lower number!";
}
return result;
}
[WebMethod]
public static string GetAvailableTicketsForFemales(int no)
{
string result = "";
int NoOfTicketsAvailable = 10;
if (no > NoOfTicketsAvailable)
{
result = "Only " + NoOfTicketsAvailable.ToString() + " Female ticket(s) avaialable. Please eneter a lower number!";
}
return result;
}
}
}
Jan 21, 2010
How To change the color of gridview row on mouseover using JQUERY
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="_Default" %> <!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 id="Head1" runat="server"> <title></title> <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $("#<%=GridView1.ClientID%> tr").filter(function() { return $('td', this).length && !('table', this).length }).hover ( function() { $(this).css("background-color", "red") }, function() { $(this).css("background-color", "white") } ); }); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Create Object of person class Person personObject = new Person(); //Assign Person list to GridView GridView1.DataSource = personObject.GetPersonList(); //Call Bindmethod of GridView GridView1.DataBind(); } } public class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public List<Person> GetPersonList() { //Retrun List of Person List<Person> list = new List<Person>() { new Person{ID=1,Name="Person1",Age=32}, new Person{ID=2,Name="Person2",Age=45}, new Person{ID=3,Name="Person3",Age=43}, new Person{ID=4,Name="Person4",Age=21}, new Person{ID=5,Name="Person5",Age=76}, new Person{ID=6,Name="Person6",Age=54}, }; return list; } }
Labels:
Jquery
Subscribe to:
Comments (Atom)

