.Net Hive

A blog about Microsoft .Net Platform & other industry happenings

Integrating jQuery in ASP.NET

Almost all of you have tried ASP.NET AJAX for Ajax related operations. But there are times when you need something more from ASP.NET AJAX, and unfortunately you cannot. These are the times you ask for alternatives.

The available alternatives for AJAX behavior in your application belong to open source community. Some of the open source frameworks for AJAX are

My personal favorite is jQuery. There are many reasons for that. One very important one is related to the UI flexibility provided by jQuery.

So, I have decided to run down a simple example for all of you where we will utilize jQuery in ASP.NET application to fetch the Server Side DateTime. Note that we will be developing this demo application in ASP.NET 1.1, to prove a point that how easy it is to integrate jQuery in already deployed application as compare to ASP.NET AJAX.

Some of the Pre-requisites, you should care about is the one and only jQuery library itself. You can get the library from here jQuery Download [http://jqueryui.com/download]. One great thing you will notice, when you will go to download section; it allows you to select/deselect different features of the framework along with theme selection as well. This feature helps the developer to avoid bulky packages and be functionality centric. You should also bookmark the website, as you will need it later on for different knowledge base articles regarding the framework. Also, the forums and discussion boards are very active and usually you get the response to your questions quite quickly.

Coming to the demo, first we have to include the jQuery library in the default.aspx

<script type=”text/javascript” src=”jquery-1.1.js”>

First we will define the action which will get triggered when user will click on “Get Server Time”

$(function() {
    $(“a#runSample1”).click(function() {
        $.get(“GetServerTime.aspx”, function(response) {
            alert(response);
         });
    });
});

For your reference, let’s take a look at when we just did.

This code will be executed on page onload event.

$(function() {

This  code, we are binding an event with the “onclick” event of the hyperlink. Which user will click?

$(“a#runSample1”).click(function() {

The above code allows us to define that, when user clicks the link “Get Server Time”, the action will get fired and will call a proxy page which we will create later on. That proxy page will return the server time into the response object which we just mentioned in the above code. And it will be displayed in message box.

$.get(“GetServerTime.aspx”, function(response) {
    alert(response);
});

Now proceeding to the proxy page “GetServerTime.aspx”. On this page, we don’t need the HTML. Therefore, we will only keep the top declaration and will remove rest of the HTML

<%@ Page Codebehind=”GetServerTime.aspx.cs” AutoEventWireup=”false” Inherits=”AJAXJQuerySample.GetServerTime” %>

The code behind of the proxy page will be

private void Page_Load(object sender, System.EventArgs e)
{
    Response.Expires = -1;
    //required to keep the page from being cached on the client’s browser
    Response.ContentType = “text/plain”;
    Response.Write(DateTime.Now.ToString());
    Response.End();
}

In the page_load method, Response.Expires = -1, allowes us to make sure that page wont get cached in the browser.

Response.ContentType = “text/plain”;
Response.Write(DateTime.Now.ToString());
Response.End();

The above code just set the content of the response to plain text and writes the current time to the response stream.

This is it. Now, we can run our sample application and click the “Get Server Time” link, this should give us the current time in an alert box:

 

Demo Screenshot

Demo Screenshot

Hope this will be hlpful to get you started. Let me know about it.

Waqas Ahmed

Advertisements

Filed under: ASP.NET, , , ,

Language Integrated Query – LINQ

In my previous post, I introduced some of the extensions provided in ADO.NET 3.5 and I told you that we will be talking about each of the extension individually later on.

To start, today I have picked LINQ. Language Integrated Query aka LINQ, is all about achieving query operations in a well comprehensive and open form. It allows you to query data regardless of any type, May it be relational databases, XML or Objects. Considering this phenomenon of querying across any kind of data source, this will be a great breakthrough and major shift in how our programmers deal with data related tasks. Having the possibility at your fingertip to query any kind of data will really change the way we program.

LINQ provides a set of patterns for querying any kind of data, whether it has a particular object model implemented or not. LINQ specifies how an object can be a “queryable,” with all the basic query operations needed, like: SELECT, WHERE, GROUP BY, ORDER BY, JOIN along with many other new operations like SelectMany, GroupJoin, TakeWhile, SkipWhile, ElementAt etc.

The syntax is the part where the real ease and flexibility shows up. You can have any language, that is supported on .Net Platform;  to be able to support LINQ.  C# and VB.NET has this built in features to interfacing LINQ. An example, where you have an array of Employees objects and you want a name of an employees having salary greater then 10000, you can write this query that looks like this:

var query_results =  from c in Employees
                                           let sal = 10000
                                           where c.Salary < sal
                                           select c.Name;

foreach (var result in query_results)
{
          Response.Write(result.ToString());
}

The above example mimics a scenario for a relational database. What happens if we have a dynamic data source like an RSS feed? Here is the simple example for that

XElement orders = …;
int order_amount = Convert.ToInt32(orders.Element(“amount”));

As you have seen that, LINQ is a way to go for data related operations due its flexibility and its data source independence. I have tried to give you an overview of LINQ but there are whole lot of information and resources available for you to get your hands dirty in LINQ :). I’m including some of the references below for your help:

Happy Reading

Waqas Ahmed

Filed under: .Net, , , ,

Data Access with ADO.NET 3.5 – Extensions

In our last post, we looked into objectives of the ADO.NET 3.5 which get shipped with VS 2008 Service Pack1. To achieve those objectives, we have some very good language extensions for ADO.NET 3.5. in C# and VB.

  • LINQ extension for VB 9.0 and C# 3.0 language provides a set of standard query operators to query many different kind of data sources using SQL like syntax. This extension for VB and C# implements LINQ pattern.
  • LINQ to Object is the default implementation for LINQ. It enables the developer to execute queries against a data persisted in memory that implements IEnumerable or IQueryable interface.
  • LINQ to SQL extension to LINQ to Objects provide an O/RM tool to generate and manage classes for business objects and to persist their instances in SQL Server tables.
  • LinqDataSource server control introduced in ASP.NET 3.5 simplifies the headache to bind data enabled web controls to business objects created with LINQ to SQL.

Later on we will discuss each of the above mentioned extensions individually, to look under the hood of ADO.NET 3.5 and it’s working.

Until than happy reading 🙂

Waqas Ahmed

Filed under: .Net, , , ,

Data Access with ADO.NET 3.5 – Primary Objectives

Release of Service Pack 1 for Visual Studio 2008 brought us some of the major lift updates .Net Framework. One of those update is ADO.NET 3.5 along with whole set of new languages and methodologies for data access as compare to .Net Framework 2.0. Some of the primary objectives of these updates are:

  • Increase developers productivity with minimal code required to achieve certain database/data related tasks.
  • Allow strong-type checking of database query syntax for relational data and their resultsets.
  • Enhancements to DataSet integration with multi Tier application architecture.
  • Enabled data intensive Winform projects with a local, synchronizable data cache derived from SQL Server Compact Edition v3.5
  • Enabled data intensive Winform projects with a local, synchronizable data cache derived from SQL Server Compact Edition v3.5.
  • Eliminate often known as the resistant mismatch between the code for data management, which usually involves SQL and OOPs programming.

Next time we will talk about the introduction of different extensions to LINQ related components /frameworks.

Waqas Ahmed

Filed under: .Net, , , ,

Microsoft Axum – Parallel Programming Language

Axum is an incubation project from Microsoft’s Parallel Computing Platform that aims to validate a safe and productive parallel programming model for the .NET framework.

It’s a language that builds upon the architecture of the web and the principles of isolation, actors, and message-passing to increase application safety, responsiveness, scalability and developer productivity. Other advanced concepts we are exploring are data flow networks, asynchronous methods, and type annotations for taming side-effects. 

You can get Microsoft Axum from http://www.microsoft.com/downloads/details.aspx?FamilyID=CFE70D5D-37AA-4C4C-8EEB-D4576C41BAA2&displaylang=en

Waqas Ahmed

Filed under: .Net, , , ,

Pages

Categories

Archives

.Net Hive @ Twitter

Blog Directory by Blog Flux
Computers/Tech blogs