JQuery validation Example
Hi all,
I have recently worked on some client-side validation for dynamic generated UI by ASP.Net. As usual, I started to spend some time to write a small htm page for prototype and testing.
<!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><title>
</title><link href="./main.css" type="text/css" rel="stylesheet" /><link href="./globalClasses.css" type="text/css" rel="stylesheet" />
<script src="./jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="./jquery.validate.min.js" type="text/javascript"></script>
</head>
<body>
<form name="ctl01" method="post" id="ctl01">
<div id="Ctl0_QuestionList">
<input name="Ctl0_QuestionLlistError" type="hidden" />
<div id="textboxContainerPanel">
<div id="textBoxPanel">
<input name="Ctl0_TextBox1" type="text" id="Ctl0_TextBox1"/>
</div>
</div>
<br />
<select name="Ctl0_DropDownList1" id="Ctl0_DropDownList1">
<option value=""></option>
<option value="BMW">BMW</option>
<option value="Honda">Honda</option>
<option value="Toyota">Toyota</option>
</select>
<br />
<div id="radioContainerPanel">
<div id="radiobuttonpanel">
<input id="Ctl0_RadioButtonList1_0" type="radio" name="Ctl0_RadioButtonList1" value="Milk" /><label for="Ctl0_RadioButtonList1_0">Milk</label>
<br />
<input id="Ctl0_RadioButtonList1_1" type="radio" name="Ctl0_RadioButtonList1" value="Apple" /><label for="Ctl0_RadioButtonList1_1">Apple</label>
<br />
<input id="Ctl0_RadioButtonList1_2" type="radio" name="Ctl0_RadioButtonList1" value="Cheese" /><label for="Ctl0_RadioButtonList1_2">Cheese</label>
</div>
</div>
</div>
<br />
<input type="submit" name="Ctl0_Button1" value="Button" id="Ctl0_Button1" type="submit" class="submit" onclick="printValue();"/>
<script type="text/javascript">
function printValue()
{
var answeredCount = 0;
var tag = 'test';
$("[id$='QuestionList'] input[type='text']").each ( function (index) {
if ($(this).val() != "") answeredCount += 1;
});
$("[id$='QuestionList'] select").each( function (index) {
if ($(this).val() != "") answeredCount += 1;
});
answeredCount += $("[id$='QuestionList'] input[type='radio']:checked").size();
alert(tag + ":" + answeredCount);
}
//<![CDATA[
$(document).ready(function() {
$('#ctl01').validate(
{
errorPlacement: function (error, element)
{
if (element.attr('type') == 'radio')
{
error.insertAfter(element.parent());
}
else
{
error.insertAfter(element);
}
}
}
);
$("[name$='TextBox1']").rules('add',
{
required: true,
messages:
{
required: 'please enter TextBox1.'
}
});
$("[name$='DropDownList1']").rules('add',
{
required: true,
messages:
{
required: 'please enter DropDownList1.'
}
});
$("[name$='RadioButtonList1']").rules('add',
{
required: true,
messages:
{
required: 'please enter RadioButtonList1.'
}
});
$("[name$='QuestionLlistError']").rules('add',
{
required: function(element) {
var requiredCount = 2;
var answeredCount = 0;
$("[id$='QuestionList'] input[type='text']").each ( function (index) {
if ($(this).val() != "") answeredCount += 1;
});
$("[id$='QuestionList'] select").each( function (index) {
if ($(this).val() != "") answeredCount += 1;
});
answeredCount += $("[id$='QuestionList'] input[type='radio']:checked").size();
return (requiredCount > answeredCount);
},
messages:
{
required: 'please enter QuestionList1.'
}
});
});//]]>
</script>
</form>
</body>
</html>
Above is the small htm page of my work. Please spend some time play with it and you will find it is very useful (I believe) for:
1. Change the error message for radio button.
2. Dynamic validation for requried question count/response count.
Enjoy!!
Asp.Net Mvc Theme and Globalization Support
I am very interested to use ASP.Net Mvc framework and read many of related articles. However I haven’t found a flexible theme and globalization supporting solution with sample codes. Therefore, I would like to build this to provide everyone as a starting point to customize their own solution.
You can download the sample solution from here.
This solution, I have implemented a new ResourceProviderFactory to customize the corresponding theme and define the resource file mapping; and also a new view engine in order to support different implementations for different themes. In the solution, I have used FluentHtml from MvcContrib and MVC T4 templates. To remind, this is just a starting point, there are lots of enhancement you can make such as helpers to access resource stings. You are welcome to share your feedback when using this and/or any enhancemets you have made on it.
Linq to XSD: cannot reference the namespace
I have recently install LinqtoXSD coz I would like to write some text on IQueryable objects which require some test data. After I have installed LinqtoXSD, then I follow the sample found in http://www.codeproject.com/KB/linq/LINQ_to_XSD.aspx. I have rebuild the whole solution after adding the XSD schema file. However Resharper complain that the namespace I have defined in XSD doesn’t exist. I have opened the object browser and check that the namespace do exist.
I don’t know what I did wrongly, then I have downloaded the demo source from above link and do a diff on the related project and solution files, but there is no findings.
Finally I close and re-open the solution, then the namespace is displayed by intellisence and resharper no longer to complain it.
P.S.> I am using VS 2008 SP1.
Install loopback network adapter in Vista host to connect virtual machine
Hello,
I am very busy to setup my new house in last month. After that, I also upgrade my XP to vista and plan to run a Windows 2008 virtual machine. Therefore I try to setup a loopback network adapter to connect between vista host and virtual machine running on Virtual PC 2007. Initially I think this is a 5-minutes task, and you can easily find information / steps from the internet teaching you how to do this. However after reading thr’ the internet, I cannot find the correct steps to setup loopback adapter with virtual PC.
I find a web site that just have a little problem with the setup procedures:
http://weblogs.asp.net/bsimser/archive/2009/04/20/using-the-loopback-adapter-and-sharepoint.aspx
Here are the high-level steps:
- install loopback network adapter in host machine (vista)
- configure static ip (say 192.168.0.1 with subnet 255.255.255.0) to loopback network adapter
- assign the loopback network adapter ONLY to virtual machine (win2008) networking setting.
- start virtual machine
- go to network connection in virtual machine, you can find there is a network adapter here.
- then configure this network adapter with static ip (say 192.168.0.100 with subnet 255.255.255.0)
- then try to ping 192.168.0.1 from virtual machine.
- If you want to use the computer name to ping each other, you can open windows\system32\drivers\etc\host file and add the corresponding entry; do this for both host and virtual machine.
Now my vista (host) can talk to win 2008 (virtual machine) each other. Wish this can help you setup your network in 5 minutes…(i used 2 hours to make this work).
SharpZipLib Example
Hello everyone, i recently need to unzip the downloaded data files. As a result, I find that there is a useful library provided by ISharpCode with sample code in Peter Bromberg’s blog.
Compute MD5 checksum in hex string
Recently I need to validate the checksum of the downloaded data file. After looking around, I find this reference to teach you how to compute the MD5 hash, http://www.nikhedonia.com/notebook/entry/verifying-md5-checksum/. However i found that the MD5 hash in the checksum file stores as 32-characters, which is different from 16-bytes MD5 hash. Finally I knew that this 32-characters string is the hex string of the 16 bytes MD5 values. Therefore i have made some modifications and below is my code for your reference:
public string ComputeMd5HashFromFile(string filePath)
{
StreamReader fileReader = null;
UTF8Encoding utf8Encoding = new System.Text.UTF8Encoding();
StringBuilder result = new StringBuilder(32);
byte[] hash = null;
try
{
fileReader = File.OpenText(filePath);
MD5 md5 = new MD5CryptoServiceProvider();
hash = md5.ComputeHash(utf8Encoding.GetBytes(fileReader.ReadToEnd()));
foreach (byte b in hash)
{
result.Append(b.ToString("x2").ToUpper()); // used to convert each byte to a hex string
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (fileReader != null) fileReader.Close();
}
return result.ToString().ToUpper();
}
Create Custom Windows Shortcut
For my job, I often need to use the Windows Calculator to calculate the program results. Therefore I would like to find an easy way to open the Calculator – create a custom Windows shortcut. First create a shortcut of the program, then open its property window. There is a shortcut-key field in the dialog, input any single character to activate keyboard shortcut. It’s awesome for me!!

Javascript: onchange problem
I have wrote some javascript to try synchronize the value from textBox1 to textBox2, and then added to the textBox1 in cs file like this:
String SyncValueScript = "document.getElementById('" + textBox2Control.ClientID +
"').value = this.value;";
textBox1Control.Attributes.Add("onchange", SyncValueScript);
However I found that there is a situation that onchange will not be fired, even the value of textBox1 is changed. Below is the step to reproduce this situation:
- Open two IE windows.
- Copy some values in the clipboard and paste on textBox1 in IE window 1.
- Then click on IE window 2.
- Go back to the IE window 1 by click on controls other than textBox 1.
- you will find that the value of textBox1 is not synced to textBox2.
Solution: there is a very easy fix for this problem, just use “onblur” event instead of “onchange”.
SSIS: Custom File Logging
I have tried to use the out-of-the-box logging feature provided by the SSIS package under SSIS > Logging. However this do not allow me to log down some application-related information, such as file path, result count, etc. As a result, I want to implement a custom file-based logging.
After research, I find that there are two ways I can implement the custom loggin – use script task to write log to text file or implement your own Integration Services log provider. For the sake of easy deployment, i choose to use script task to implement my custom file-based logging. The followings are the steps I am going to do:
1. Create a log folder and assign the log file path
2. Standard log script task to write log
3. Uses event handler to log additional events such as OnError and OnWarning
First, create a log folder using File System Task together with File Connection. Then concatenate the log file name to the variable in a script task:

Dts.Variables("SWIFTLogFilePath").Value = Dts.Variables("SWIFTLogFolder").Value.ToString() & _
"\\swift" & Year(Now) & _
IIf(Month(Now) < 10, "0", "").ToString() & Month(Now) & _
IIf(Day(Now) < 10, "0", "").ToString() & Day(Now) & _
".log"
To remind, please ensure your variables are defined in ReadOnlyVariables / ReadWriteVariables. Then you can create another script task to write log to a file. This is the sample code for writing a log file:

Public Sub WriteLog(ByVal logContent As String)
Dim logWriter As StreamWriter
Try
logWriter = New StreamWriter(Dts.Variables("SWIFTLogFilePath").Value.ToString(), True)
logWriter.WriteLine(logContent)
logWriter.Close()
Catch ex As Exception
'Unexpected Exception
End Try
End Sub
Besides logging, i find that there is a trick/bug found in For Each Container. You can see that I have duplicated to create log script task block in four different branches. If I merge these four branches by pointing to the same log script task, the script task will not be executed. Therefore it doesn’t support branch merging and runs tasks in sequential order only.

The last thing I want to show you is the Event Handler, you can implement the event handler of OnError and OnWarning to capture these information into your log file; just go to “Event Handlers” tab and select Executable and Event you want to handle. Then just put a script task as previous one, but additional information like System::ErrorCode, and System::ErrorDescription can be logged down as well.
