Archive

Archive for September, 2010

Calculate Age from Date using Javascript

September 24, 2010 Leave a comment

Hi all

try this example to Calculate Age from Date using Javascript

Add this Javascript code

    <script type="text/javascript">
      function checkAge()
      {
         var today = new Date();
         var d;
         var dateEntered = document.getElementById('<%=txtTextBox.ClientID %>').value;
         var objRegExp = /\d{1,2}\/\d{1,2}\/\d{4}$/
         if (!objRegExp.test(dateEntered))
         {
           alert('Date not in correct format');
           return false;
         }
          d = dateEntered.split('/');
          var yearEntered = parseInt(d[2]);
          var nowYear = parseInt(today.getFullYear()); 
          var age = nowYear - yearEntered;
          alert(age);
          return false;
       }
    </script>

and in your web page

   <div>
        <asp:TextBox ID="txtTextBox" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Click"  OnClientClick="return checkAge();" />
    </div>

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript

Select Max Value With LINQ To SQL

September 10, 2010 1 comment

Hi all

Try this Example to show how to Select Max Value With LINQ To SQL

In T-SQL such a query can be written like this

SELECT MAX(UnitPrice) FROM products

In LINQ To SQL we can write the following query

NorthwindDataContext dbml = new NorthwindDataContext();

var query = (from p in dbml.Products select (p.UnitPrice)).Max()  ;

Other than MAX we can also use other aggregates such as MIN, SUM etc..

Hope this helps

Good Luck

Categories: ASP.Net, Linq

JQuery Autocomplete With Linq

September 3, 2010 3 comments

Hi all

try this example to make Autocomplete with Linq using Jquery

in previous post i provide an example to show how to do this AutoComplete from Database using JQuery

Step1 :- First download the following files jquery.autocomplete.js and jquery.autocomplete.css

Step2 :- Open VS2008 and create a new website

Step3 :- add new Linq to SQL DataContext  and name it “Northwind.dbml” and add Products table in it

Step4 :- Add new Web Page and Drage TextBox control from Toolbox inside page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Autosugeestion.aspx.cs" Inherits="Autosugeestion" %>
<!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>
<script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="Scripts/jquery.autocomplete.js" type="text/javascript"></script>
<link href="CSS/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function() {
$("#<%=txtSearch.ClientID%>").autocomplete('Search.ashx');
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
Enter Product :- <asp:TextBox ID="txtSearch" runat="server" Width="190"></asp:TextBox>
</div>
</div>
</form>
</body>
</html>

Step5 :- Add new ASP.Net Generic Handlers file and name it “Search.ashx”


<%@ WebHandler Language="C#" %>

using System;
using System.Web;
using System.Linq;

public class Search : IHttpHandler {

public void ProcessRequest (HttpContext context)
{
string param = HttpContext.Current.Request.Params["q"];
NorthwindDataContext dbml = new NorthwindDataContext();
var query = from products in dbml.Products
where products.ProductName.StartsWith(param)
select products;
foreach (var item in query)
{
context.Response.Write(item.ProductName + Environment.NewLine);

}
}

public bool IsReusable {
get {
return false;
}
}
}

Hope this helps

Good Luck

Categories: ASP.Net, Jquery, Linq

Jquery to Change Image Opacity on MouseOver

September 1, 2010 Leave a comment

Hi all

try this example to use Jquery to Change Image Opacity on MouseOver


<%@ 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>Untitled Page</title>
    <style type="text/css">
    .imgOpa
    {
        height:250px;
        width:250px;
        opacity:1.0;
        /*filter:alpha(opacity=30);*/

    </style>
    <script type="text/javascript"
     src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
    </script>

    <script type="text/javascript">
        $(function() {
            $('.imgOpa').each(function() {
                $(this).hover(
                    function() {
                        $(this).stop().animate({ opacity: 0.3 }, 800);
                    },
                   function() {
                       $(this).stop().animate({ opacity: 1.0 }, 800);
                   })
                });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>Hover over an Image to change its Transparency level</h2>
        <br />
        <asp:Image ID="Image1" runat="server" ImageUrl="1.jpg" CssClass="imgOpa" />

    </div>
    </form>
</body>
</html>

Hope this helps

Good Luck

Categories: ASP.Net, Jquery