domenica 15 febbraio 2009

ASP.NET, Web Services and Json: a different approach with jQuery

In a previous article I explained how it is possible to implement in ASP.NET a client/server application based on Json web services.

The client side was implemented using all the amazing possibilities offered by WCF and the Ajax support of DotNet, but this isn't the only possibility, we can consume a web service through jQuery library.

Implementing the application with jQuery requires a few changes to the previous code:
- The ScriptManager control is no more required
- It's necessary to include the jQuery Javascript library (i.e. from AJAX Libraries API site:
- Some minor changes to the Javascript and HTML code of the Default.aspx page.

Thanks to Alberto for some good pieces of code and useful explanations.

Download sample

lunedì 10 novembre 2008

How to derive a web control from another one

I'm developing an project containing a repeater; it is filled with a datatable and every item of the repeater contains some web controls, including a button. In response to the click on one of the buttons I have to delete a record in the table referenced by an unique ID. Many possibilities exist, usually a common solution is to use the property "CommandArgument", but because I like elegance (and using a string property to store an integer isn't elegant!), to explore new solutions and, finally, because I want to lose my VB roots and to enter the wonderful world of the C# OO programming I tried the way of creating a new web control, similar to a button, but with a new property, RecordId, useful to store an integer: the natural way is to derive the new control from a button.

The steps to accomplish this easy job are:

- Start Visual C# Express 2008 (or equivalent)

- Create a new Class Library project

- Add System.Web to references

- Rename the class with a significant name (for example ButtonExt)

- Define the new control in the namespace System.Web.UI.WebControls

- Derive the class from System.Web.UI.WebControls.Button

- Add the RecordId property

- Override LoadViewState and SaveViewState for viewstate managing

- Compile and obtain the DLL of the new control

And if you want to use it:

- Create a new project

- Add the DLL of the control to references

- Drag the DLL of the control to the General Section of the Toolbox

- Drag the control to the form and use it!

My two cents

Download source

Download example

domenica 13 luglio 2008

ASP.NET, Web Services and Json

Web services offer a standardized mechanism to interface remote applications written in different languages.

They are based on two different protocols: HTTP for the tunnelling of the information and (usually, but we will show that this isn't the only possibility) XML for the coding of the information.

The protocol also requires the server exposes some information about the format of every possible functions so the client autonomously can compose and send data requests.

The best scenario where you can implement web services is client-server applications over Internet.

The server side usually needs a web server and the service is written as a web dynamic page in PHP, ASP.NET, JSP or similar languages/environments.

The client side could be a stand alone application, or, better, a web application running in a browser, in this case the compatibility with the widest range of browsers/operating systems restricts the choice to 3 solutions: a Flash plug-in, a Java applet or a Javascript application (and in the future Silverlight?).

I don't like Java applets (I'm looking to definitely remove JVM from my notebook) and I don't know Actionscript (the language for Flash), so the only choice appears to be Javascript.

In my opinion Javascript offers few advantages: it is implemented in every modern browser without installing anything else, it is quite standardized and, luckily, it offers the HttpXmlResponse class to retrieve remote data through a HTTP request. But it is interpreted and it doesn't offer native functions managing XML files, so the result is that Javascript applications exchanging XML-formatted data could be very slow.

Is XML the only data coding system on web services? No, it isn't: Json is another [great] possibility!

What is Json? Json - similarly to XML - is a method to code structured data, but it reproduce a subset of the Javascript syntax of declaration and initialization of variables, arrays and objects, so it's possible to decode Json files simply with the eval() native Javascript function: easy and fast.

But this is not all: Visual Studio NET 2008 offers the possibility of implement Json web services and, client-side, encapsulates the remote functions of the web services in pure-Javascript local function entry points. Great!

Implementing Json web services in ASP.NET is very easy:

- open Visual Web Developer 2008 Express Edition
- create a new ASP.NET project
- create a "Service" directory
- inside "Service" create an "AJAX-enabled WCF Service" file named "Json.svc"
- open "Json.cs" and modify the line "[ServiceContract(Namespace = "")]" to "[ServiceContract(Namespace = "Test")]"
- in "Json.cs" add "using System.Collections;" the public method "GetArrayList()" (who makes and return a populated ArrayList)
- to "Default.aspx" a ScriptManager:

<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:ServiceReference Path="~/Services/Json.svc" />

- to "Default.aspx" add a label (lblTest):
<asp:Label ID="lblTest" runat="server" Text=""></asp:Label>

- to "Default.aspx" add the Javascript code that connects to the remote web service:

<script language="javascript" type="text/javascript">
var x = 0;
function getal()

function onMethodCompleted(results)
$get("lblTest").innerText = "Element " + x + ":" + results[x++];

- to "Default.aspx" add a button (with onclick event):

<asp:Label ID="lblTest" runat="server" Text="PUSH"></asp:Label><br />

Run and try!

Download sample

Some screenshots of the output (for Chandra):

domenica 4 maggio 2008

How to embed a resource in a DotNet DLL (and how to use it)

In my previous post I proposed a web custom control useful to embed videos in your web pages.
The control is made of some server-side code written in C# and some javascript functions and one flash plug-in, both client-side.
An interesting point is that all the necessary code is embedded in a single DLL, making the exploiting process in a ASP.NET panorama easier.

Step 1
In the Solution Explorer, over the file that you want to embed, right-click and choice Properties then set “”Embedded Resource” on “Build Action” :

Step 2
If the file is a web resource that you want to reference with an URL (for example a javascript file, a flash plug-in or a css file) you must open the Properties/AssemblyInfo.cs file:

Then add the following line (Filename.extension is the file to embed, Namespace is the namespace of the project or of the control and MIME_Type is the MIME type of the file):

[assembly: WebResource(“Namespace.Filename.extension”, “MIME_Type“)]

For example in my previous project:

Step 3
Using the embed file is easy, you can obtain the referencing URL with the GetWebResourceUrl method:

Page.ClientScript. GetWebResourceUrl (this.GetType(), Namespace.Filename.extension)

That’s all!

domenica 9 marzo 2008

EmbedVideo: a web control to embed video (FLV, WFV, ...) and Flash animations

CalSoft.EmbedVideo is a web custom control for ASP.NET. It is useful to put videos or Flash animations in your web pages.
It supports AVI, WMV and any other kind of formats supported by Windows Multimedia Player and FLV files on every Flash-enabled browser thanks to OS FLV, a free Flash 9 player and, finally, SWF files. The objects are embedded with a free javascript library, SWFobject that manages also browser differences and Flash versions.
Some interesting points are:
- thanks to the SWFobject it resolve EOLAS problems (the gray frame around the control and the need for an extra click to activate it) in IE.
- all the extra files (the two JavaScript libraries and the swf player) are embedded inside a single DLL.

- download and unzip (framework 2.0) or (framework 3.0) or (framework 3.5)
- create your web project in Visual Web Developer 2008 (or other Visual Studio products)
- right click on the toolbox window
- click on "Choose Items ..."
- click "Browse..." and select the EmbedVideo.dll just unzipped.

Now you are ready to use the control easily in your project: drag it in the page and set the properties (the meaning of each property is banal).

The icon of the control:

Note that the current version isn't compatible with and was developed with Visual #C Express 2008.

The control is released under GPL license; please pay attention to the licenses of applications and libraries embedded consulting the respective sites.

Download the source
Download EmbedVideo DLL for framework 2.0
Download EmbedVideo DLL for framework 3.0
Download EmbedVideo DLL for framework 3.5
Sample here

Some useful links:
Pazera Free Video to Flash Converter