Friday, November 30, 2007

Silverlight 1.1 upgraded to Silverlight 2.0


Microsofts Silverlight is a cross platform and cross browser version of the .NET Framework. That's right folks it runs on Windows,Mac & Linux.

First quarter of 2008 they will be releasing Silverlight 2.0. This release has so many new features that they decided to up the number from 1.1 to 2.0.

Here is a break down of the new features...

WPF UI Framework: The current Silverlight Alpha release only includes basic controls support and a managed API for UI drawing. The next public Silverlight preview will add support for the higher level features of the WPF UI framework. These include: the extensible control framework model, layout manager support, two-way data-binding support, and control template and skinning support. The WPF UI Framework features in Silverlight will be a compatible subset of the WPF UI Framework features in last week's .NET Framework 3.5 release.

Rich Controls: Silverlight will deliver a rich set of controls that make building Rich Internet Applications much easier. The next Silverlight preview release will add support for core form controls (textbox, checkbox, radiobutton, etc), built-in layout management controls (StackPanel, Grid, etc), common functionality controls (TabControl, Slider, ScrollViewer, ProgressBar, etc) and data manipulation controls (DataGrid, etc).

Rich Networking Support: Silverlight will deliver rich networking support. The next Silverlight preview release will add support for REST, POX, RSS, and WS* communication. It will also add support for cross domain network access (so that Silverlight clients can access resources and data from any trusted source on the web).

Rich Base Class Library Support: Silverlight will include a rich .NET base class library of functionality (collections, IO, generics, threading, globalization, XML, local storage, etc). The next Silverlight preview release will also add built-in support for LINQ to XML and richer HTML DOM API integration.

This should spur a major migration from applications that would have been built on the desktop to be moved to the web. I'm really interested in what kind of performance this new framework will have and how far you can push it in terms of size. I wonder if there is a built in asynchronous framework to handle calls to different web services.

The blurry line between desktop application and web app has gotten a lot bigger.

Sunday, November 4, 2007

Single & Multiline TextBox with MaxLength Validation

After doing a bit of searching online I couldn't find an easy way to control the character length in a text box and a multi line text box. Plus the limit is defined by the schema of the table in a database. So I did what any developer would do with this problem. I used it as an excuse to write some code!

Here is one example of limiting characters in a text box or a multi line text box in a asp.net form. This example will also show how to add a dynamic warning message once the limit has been reached.

This example has 3 parts.

  • A Stored Procedure

  • Some C# code

  • A Javascript function



Lets get into the code!

Create the stored procedure

CREATE PROCEDURE [dbo].[GetFieldWidths]
(
@TableName nvarchar(40)
)
AS
BEGIN
SELECT COLUMN_NAME,
CHARACTER_MAXIMUM_LENGTH,
DATA_TYPE
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = @TableName
END
RETURN

Create a C# function to read all valid fields widths into a Generic Dictionary Collection.



// helper function
public static Dictionary FieldWidths
{
get
{
System.Web.HttpApplicationState ApplicationState = HttpContext.Current.Application;
if (ApplicationState["cDatabase.Tables.tblUsers.FieldWidths"] == null)
{
ApplicationState["cDatabase.Tables.tblUsers.FieldWidths"] = cDbaseFunc.FieldWidths(tblUsers.TableName);
}

return ApplicationState["cDatabase.Tables.tblUsers.FieldWidths"] as Dictionary;
}
}

// main function
public static Dictionary FieldWidths(string Table)
{
//Load field types
string[] aryStringTypes = new string[6] { "char", "nchar", "ntext", "nvarchar", "text", "varchar" };
List StringTypes = new List(aryStringTypes);

Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetStoredProcCommand(cDatabase.StoredProcedure.GetFieldWidths);
db.AddInParameter(dbCommand, "@TableName", DbType.String, Table);
IDataReader dr = db.ExecuteReader(dbCommand);

string FieldName = string.Empty;
int FieldLength;
string FieldType = string.Empty;

Dictionary FW = new Dictionary();
while (dr.Read())
{
FieldType = dr.GetString(2).ToLower();
if (StringTypes.Contains(FieldType))
{
FieldName = dr.GetString(0);
FieldLength = dr.GetInt32(1);
FW.Add(FieldName, FieldLength);
}
}
dr.Close();

return FW;
}

Create the Javascript function that limits the characters and creates the dynamic message.



function CheckCharMaxLenLimit(control,maxlength)
{
var ErrorMsgID = control.id + "_$MAXLENGTH_ERROR_MSG$";
var ErrorMsg = document.getElementById(ErrorMsgID);

var MaxLength;
if(maxlength != null)
{
MaxLength = maxlength;
}
else
MaxLength = control.maxLength - 1;

var TextLength = control.value.length;
if(TextLength > MaxLength)
{
control.value = control.value.substring(0,MaxLength);

if(ErrorMsg == null)
{
control.outerHTML = control.outerHTML + MaxLength + " character limit!";
}
}
else
{
if(ErrorMsg != null)
ErrorMsg.parentNode.removeChild(ErrorMsg);
}
}

The final step is too attach the javascript function to your text box control "onkeyup" event.
You can do this in the code behind like so....
this.txtUserName.Attributes["onkeyup"] = "CheckCharMaxLenLimit(this," + this.FieldWidths["Username"] + ");";

Summary


The nice thing about the C# function is that it caches the returning list of fields and widths into an Application State variable. If your tables schema changes allot you could always cache it in a session state variable. This reduces the hits to SQL server which improves performance.

The character limiting and checking is handled on the client side using the javascript function. This function also supports the "maxLength" attribute of the text box control. The dynamic message is also created on the client side by using the DOM to attach a child element to the text box. This creates a very quick UI response. Lastly the dynamic message can be formatted using standard CSS.

Like so...
.MAXLENGTH_ERROR_MSG
{
color:red;
}

This method uses SQL2005 but I'm sure it could easily be used with any database platform. I hope this helps someone out there with the same problem.