Wednesday, 16 April 2014

How to set focus on first or particular textbox during page load using JQuery in ASP.NET

In this article I will explain how to set focus on first or particular textbox during page load using JQuery in ASP.NET

In this scenario we need to write code for focus on first or particular textbox.

Example 1: Focus First Textbox

<script type="text/javascript">
$(document).ready(function () {
$('input:text:first').focus();
});
</script>

Example 2: Focus Particular Textbox

<script type="text/javascript">
$(document).ready(function () {
$(#txtFirstName').focus();
});
</script>

Below is the total code Textbox focus.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Set focus in particular textbox </title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>

// Focus first textbox

<script type="text/javascript">
$(document).ready(function () {
$('input:text:first').focus();
});
</script>

(Or)

// Focus Particular textbox

<script type="text/javascript">
$(document).ready(function () {
$(#txtFirstName').focus();
});
</script>

</head>
<body>
<form id="form1" runat="server">
<table align="Center">
<tr>
<td colspan="2">Set focus in particular textbox or First textbox
</td>
</tr>
<tr>
<td colspan="2" height="20px"></td>
</tr>
<tr>
<td>FirstName :</td>
<td>
<asp:TextBox runat="server" ID="txtFirstName" /></td>
</tr>
<tr>
<td>LastName :</td>
<td>
<asp:TextBox runat="server" ID="txtLastName" /></td>
</tr>
<tr>
<td>Location :</td>
<td>
<asp:TextBox runat="server" ID="txtLocation" /></td>
</tr>
<tr>
<td>City :</td>
<td>
<asp:TextBox runat="server" ID="txtCity" /></td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="btnSave" Text="Save" />
</td>
<td>
<asp:Button runat="server" ID="btnCancel" Text="Cancel" />
</td>
</tr>
</table>
</form>
</body>

</html>

Date picker (Or) Datepicker Examples

How to disable particular days in DatePicker on ASP.NET using JQuery


In this article I will explain how to disable particular days in Datapicker on ASP.NET using JQuery.


Sample code :

<script type="text/javascript">
var Alldays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var disableParticularDates = ["2014/04/16", "2014/05/20", "2014/06/24", "2014/04/18", "2014/05/21", "2014/06/28"]; // yyyy/MM/dd
var disableParticulaDays = ["Saturday", "Sunday"];
function ShowDisableDays(date) {
ymd = date.getFullYear() + "/" + ("0" + (date.getMonth() + 1)).slice(-2) + "/" + ("0" + date.getDate()).slice(-2);
day = new Date(ymd).getDay();
if ($.inArray(ymd, disableParticularDates) < 0 && $.inArray(Alldays[day], disableParticulaDays) < 0) {
return [true, "enabled", "Book Now"];
} else {
return [false, "disabled", "Sold Out"];
}
}
$(function () {
$("#datepicker").datepicker({ beforeShowDay: ShowDisableDays });
});
</script>

Output:


Different types of Date formats using JQuery in ASP.NET


Create asp Textbox.

<asp:TextBox ID="txtDate" runat="server"/>

Different types date formats in JQuery

Example 1: (dd/mm/yy)

$("#txtDate").datepicker({ dateFormat: 'dd/mm/yy' });

Output: format (ex: 04/05/2012)

Example 2: (mm/dd/yy)

$("#txtDate").datepicker({ dateFormat: 'mm/dd/yy' });

Output: format (ex: 05/04/2012)

Example 3: (yy-mm-dd)

$("#txtDate").datepicker({ dateFormat: 'yy-mm-dd' });

Output: format (ex: 2012-05-04)

Example 4: (DD,d MM, yy)

$("#txtDate").datepicker({ dateFormat: 'DD, d MM, yy' });

Output:  format (ex: Friday, 4 May, 2012)