Tuesday, 11 September 2012

Using jQuery with ASP.NET


Abstract: This article gives a brief introduction of jQuery and how it can be used in ASP.NET applications using Visual Studio 2008. In this guide we will see what is jQuery, how to get it, include it in our ASP.NET projects and use it.

Topics
  • What is jQuery?
  • How jQuery Works?
  • Get jQuery
  • Include jQuery in ASP.NET project
  • Include jQuery in a Web Page
  • Code Walkthrough
  • Example
  • Conclusion

What is jQuery?

With reference to jQuery website jQuery is
“write less, do more” fast and concise JavaScript library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.
The features of jQuery include:
  • Lightweight Footprint (About 19kb in size – Production version)
  • CSS3 Compliant (Supports CSS 1-3 Selectors)
  • Cross-browser (IE 6.0+, FF 2+, Safari 3.0+, Opera 9.0+, Chrome)

How jQuery Works?

The Problem: Sometimes we want some kind of client side logic to be executed on load of the page, so we try to make use of window.onload() event to kick start the client side script but unfortunately we have to wait till everything on the page gets loaded which includes larger image files also.

The Solution: jQuery has a little handy function $(document).ready(fn) which has a capability to launch your JavaScript logic as soon as the Document Object Model (DOM) is ready and DOM loading happens before the page has finished loading, which gives us flexibility to execute a client side logic even before a page is loaded.

Get jQuery

You can download jQuery from jQuery site from the home page. You can get 2 versions of jQuery: Production Version which is just 19kb in size, good in case if you want to just deploy it on production and Development Version which is useful in case if we plan to see the internals or plan to debug the library. The current version is1.3.2. To see latest version and more updates on jQuery you can visit jQuery site.

Step 1: Go to http://jquery.com

Step 2: On home page look for the below section:



Step 3: Download the version which is most suitable for your requirement. That’s all. Now let’s see how we can use it.

Include jQuery in ASP.NET project

Now since we have downloaded the jQuery library it’s time to add it to our ASP.NET project. So let’s start:

Step 1: Open Visual Studio 2005/2008 (I will demonstrate using VS 2008)

Step 2: Create a new Web Site.



Step 3: Give some name to the website.



Step 4: Below is the folder structure.



Step 5: To add the jQuery script file either you can directly add to the root or create a folder called as ‘Scripts’and save it to separate our script files. We will do using later approach. Right click the Scripts folder > Add Existing Item > Browse to the path where you downloaded the jQuery library (jquery-1.3.2.min.js) > Select the file and click Add. The structure will look similar to the following:



You can either change the name of the file to jquery.js or leave it the way it is.

That’s all. Now it's time to include jQuery in ASP.NET page.

Include jQuery in a web page

Step 1: Create a New Page, I created ‘Demo.aspx’.

Step 2: Now drag and drop the jquery-1.3.2.min.js file from the Solution Explorer on to your page to create a reference as shown below:



Alright cool! We are ready to write jQuery code now. So let’s write some simple script.

Code Walkthrough

We will create a small <div> control and <a> tag. On click of anchor tag we will call a .slideDown(s, c) function to demonstrate the slide down effect.

Step 1: Create <a> and <div>



Step 2: We will write jQuery code on click of <a>.



  • ‘$’ sign is a shorthand notation to call jQuery .ready function. Instead of $(document).ready(fn) we directly call $(fn).
  • When we click on <a> tag the click function gets called which in turn calls the slideDown function to slide down div of id showMe. ‘slow’ is the parameter passed to tell slide it down slowly. Initially the div control is hidden and on click of anchor tag we display the div.Abstract: This article gives a brief introduction of jQuery and how it can be used in ASP.NET applications using Visual Studio 2008. In this guide we will see what is jQuery, how to get it, include it in our ASP.NET projects and use it.

    Topics
    • What is jQuery?
    • How jQuery Works?
    • Get jQuery
    • Include jQuery in ASP.NET project
    • Include jQuery in a Web Page
    • Code Walkthrough
    • Example
    • Conclusion

    What is jQuery?

    With reference to jQuery website jQuery is
    “write less, do more” fast and concise JavaScript library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.
    The features of jQuery include:
    • Lightweight Footprint (About 19kb in size – Production version)
    • CSS3 Compliant (Supports CSS 1-3 Selectors)
    • Cross-browser (IE 6.0+, FF 2+, Safari 3.0+, Opera 9.0+, Chrome)

    How jQuery Works?

    The Problem: Sometimes we want some kind of client side logic to be executed on load of the page, so we try to make use of window.onload() event to kick start the client side script but unfortunately we have to wait till everything on the page gets loaded which includes larger image files also.

    The Solution: jQuery has a little handy function $(document).ready(fn) which has a capability to launch your JavaScript logic as soon as the Document Object Model (DOM) is ready and DOM loading happens before the page has finished loading, which gives us flexibility to execute a client side logic even before a page is loaded.

    Get jQuery

    You can download jQuery from jQuery site from the home page. You can get 2 versions of jQuery: Production Version which is just 19kb in size, good in case if you want to just deploy it on production and Development Version which is useful in case if we plan to see the internals or plan to debug the library. The current version is1.3.2. To see latest version and more updates on jQuery you can visit jQuery site.

    Step 1: Go to http://jquery.com

    Step 2: On home page look for the below section:



    Step 3: Download the version which is most suitable for your requirement. That’s all. Now let’s see how we can use it.

    Include jQuery in ASP.NET project

    Now since we have downloaded the jQuery library it’s time to add it to our ASP.NET project. So let’s start:

    Step 1: Open Visual Studio 2005/2008 (I will demonstrate using VS 2008)

    Step 2: Create a new Web Site.



    Step 3: Give some name to the website.



    Step 4: Below is the folder structure.



    Step 5: To add the jQuery script file either you can directly add to the root or create a folder called as ‘Scripts’and save it to separate our script files. We will do using later approach. Right click the Scripts folder > Add Existing Item > Browse to the path where you downloaded the jQuery library (jquery-1.3.2.min.js) > Select the file and click Add. The structure will look similar to the following:



    You can either change the name of the file to jquery.js or leave it the way it is.

    That’s all. Now it's time to include jQuery in ASP.NET page.

    Include jQuery in a web page

    Step 1: Create a New Page, I created ‘Demo.aspx’.

    Step 2: Now drag and drop the jquery-1.3.2.min.js file from the Solution Explorer on to your page to create a reference as shown below:



    Alright cool! We are ready to write jQuery code now. So let’s write some simple script.

    Code Walkthrough

    We will create a small <div> control and <a> tag. On click of anchor tag we will call a .slideDown(s, c) function to demonstrate the slide down effect.

    Step 1: Create <a> and <div>



    Step 2: We will write jQuery code on click of <a>.



    • ‘$’ sign is a shorthand notation to call jQuery .ready function. Instead of $(document).ready(fn) we directly call $(fn).
    • When we click on <a> tag the click function gets called which in turn calls the slideDown function to slide down div of id showMe. ‘slow’ is the parameter passed to tell slide it down slowly. Initially the div control is hidden and on click of anchor tag we display the div.

Monday, 10 September 2012

Simple Insert,Update,View and Delete with LINQ-To-SQL


Let's create a simple example which is very simple and also show basic step to use LINQ-To-SQL. First i have created a simple table called which have 4 fields like ProductId,Name,Description and Price. Like following.

Table-for-which-LINQ-To-SQL-Classes-Generated

Here is the description for each fields
  1. ProductId- A id which uniquely identify each product.
  2. Name- Name of Product
  3. Description- A short description about product
  4. Price- Price of Product.
Now, Lets create a Linq-To-SQL and rename it as MyDataContent.dbml as following. For that Go to your project ->Right Click->Add New Item->Go to Data Template-> LINQ to SQL Classes like following.

How-to-add-LINQ-To-SQL-Classes-to-your-application

Then open Database Explorer and drag and drop Product table on the newly generated LINQ-to-SQL Class like following.

How-Drag-and-Drop-Database-Tables-to-Linq-To-SQL-Classes

Now our LINQ-SQL-Class is Ready. Now we will insert some data to our table and then first we will write code to get all product information from the database. I have created a function called GetProduct which will print all the product information on page with the help of Response.Write . Following is code for that.
public void GetProducts()
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
var Products = from product in context.Products
             orderby product.Name
             select product;
foreach(Product p in Products)
{
Response.Write(string.Format("<B>Id</B>:{0}", p.ProductId.ToString()));
Response.Write(string.Format("<B>Name</B>:{0}", p.Name));
Response.Write(string.Format("<B>Description</B>:{0}", p.Description));
Response.Write(string.Format("<B>Price</B>:{0}", p.Price.ToString()));
Response.Write("===========================================");
}
}
}
Now Let's Create a function to Add Product. Following is code for function to add product.
public void AddProduct(Product product)
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
context.Products.InsertOnSubmit(product);
context.SubmitChanges();
}
}
In the above function i am passing a new object of product and passing that object to above function following is a code for that which create a new product via calling above function.
//Add New Product
Product product = new Product();
product.Name = "Product3";
product.Description="This is product 3 Description";
product.Price=10;
AddProduct(product);
Now we have added the code to add product now lets create a function to update the current product information. Following is a code for that.
public void UpdateProduct(int productId, string name, string description, double price)
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
 Product currentProduct = context.Products.FirstOrDefault(p => p.ProductId == productId);
 currentProduct.Name = name;
 currentProduct.Description = description;
 currentProduct.Price = price;
 context.SubmitChanges();
}
}
With the help of above function you can update current product like following.
////Update Product
UpdateProduct(2,"New Product2","New Description for product",20); 
Now we added the code for update product so now let's create a sample code to delete a specific product. Following is a code for that.
public void DeleteProduct(int productId)
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
Product currentProduct = context.Products.FirstOrDefault(p => p.ProductId == productId);
context.Products.DeleteOnSubmit(currentProduct);
context.SubmitChanges();
}
}
You can delete the product via passing product id as following.
//Delete product
DeleteProduct(3);
So that's it. you can create insert,update,delete operation with the help of LINQ-To-SQL within some minutes without writing so much code for .net and queries for databases.