Archive for the ‘VB.NET’ Category
Want To Validate Username Availability Asynchronously?
While I was reading my feed aggregator at the weekend, I was paid attention to the recent post from Encosia because of its title. Validating a username asynchronously would be a great functionality in almost every types of online registration scenarios. Right after reading the post, I downloaded the binary file from CodePlex and created a very simple demo in about half an hour. That was really easy to implement.
Implementation
Before starting to code, be sure that the dll that is downloaded from CodePlex is in the Bin directory:

My sample (yes, very simple) registration form is:

As it can be seen, the trick is with the ServicePath and ServiceMethod attributes. ServicePath is the path to the webservice housing the ServiceMethod, and ServiceMethod is the method checking the availability of a username. My sample (yes, again very simple) webservice is:

Details of other attributes of the control can be read via its Discussions page at CodePlex.
Finally, here is how the form looks like after all these implemenations:

Download
You can download this sample registration form from here.
Issue Tracker
I would encourage to write your comments to here if you face any issues about the control.
Manual Sorting with ListView by Using Drag and Drop
Introduction
I have been following Matt’s jQuery related posts, and trying to get myself familiar with this new library. While searching for tutorials about it, I accidentally found this great blog. Accident followed by another accident, I figured out that one of their JavaScript posts can be applicable to the ListView control in order to create a “drag and drop” based manual sorting within the items of the ListView.
• I strongly suggest reading the original post first before passing to the rest of this post.
Implementing The Original Post…

As a data source, I just grabbed the XML file that I used before in here, and simplified it.

Here is the CSS implementation of the original post into my sample ASPX page:

And then, I created the DIVs programmatically by using a ListView as in the following:

If you look at the JavaScript.js file that I used in my sample code, you will see that the load function has been commented since I needed to create the “dragObject” objects dynamically in that function:

As a result of this, I organized the Page_Load event as:

Download
You can download the sample application via here.
Enhancing Paging in ListView by Using DataPager
DataPager is one of the new controls in ASP.NET 3.5, and it provides paging functionality for data-bound controls that implement the IPageableItemContainer interface, which is only the ListView control for the time being.
By default, DataPager has two commonly used paging styles:
• Numeric Pager via NumericPagerField
• Next and Previous Pager via NextPreviousPagerField
To get an idea about how to implement these basic common styles, you can read this post.
This post will explain how to enhance the paging experience in a ListView by using both a NextPreviousPagerField and a DropDownList which will enable users to select the page size of ListView. This page size is actually the PageSize property of the DataPager, and this property defines the number of records that are displayed for each page of ListView.
To avoid complexity, the following simple XML file (Contacts.xml) will be used as a data source:

And, the XMLDataSource that consumes this XML file is:

“xdsDemo” is the DataSourceID of the ListView “lvDemo”:

After getting this data binding done, LayoutTemplate of “lvDemo” should be similar to the following:

Even though it is not completely related with the topic and emphasis of this post, ItemTemplate and AltenatingItemTemplate templates of “lvDemo” are:

Finally, here is the code that sets DropDownList (ddlDemo) and DataPager (dpDemo):

As a conclusion of all these things, the result on the browser should be like:

Download
Sample code for this demo application can be downloaded via here.
Rotate Array by N Position – Via Programming Pearls
Lately, I have been trying to finish reading the Programming Pearls written by John Bentley. The chapters related with designing and developing algorithms are really entertaining. One of the questions that was discussed in the early chapters of the book was about an algorithm of rotating a one-dimensional vector, and I want to share my solution written in VB.NET and C# by utilizing a simple string array.
Question (with the terms of an array)
Rotate a one-dimensional array of K elements from left by N positions.
For instance, with K=7 and N=3, the array {a, b, c, d, e, f, g} is rotated to {d, e, f, g, a, b, c}.
Solution
As suggested in the book, I will be using the Array.Reverse method. I will also write the function that rotates an array from right as well in order to carry out the discussion further.
In VB.NET – Rotate By N From Left

In C# – Rotate By N From Left

In VB.NET – Rotate By N From Right

In C# – Rotate By N From Right

Page_Load of an ASP.NET environment would be a good place to test and visualize the results in this case.
In VB.NET

In C#

Finally, results on the browser should be like:

Disable UpdatePanel’ed ListView with CSS and JavaScript While Sorting
When a ListView, or GridView as well, allows sorting and is inside an UpdatePanel, it always bothers me that any of the sorting links in the header row are still clickable even though the initiated sorting progress is not being completed. Most of the time this situation may yield an error if the ListView is dealing with many records since everything happens inside an UpdatePanel asynchronously. As there are some other possible ways to beat this, here is my solution by utilizing a transparent image to cover up the ListView while it is being sorted.
• This link about CSS image transparency and opacity would be a good reading before jumping into the solution.
• I will be extending one of my former sample codes that I created here.
Solution
Instead of creating large volume of data for the sample code, I will just simulate 5 seconds sleeping inside the sorting event of ListView1:

Then, I will create the DIV which holds the image that will be made transparent via CSS at a later time and place ListView1 inside an UpdatePanel to have asynchronous postbacks:


Final nested code view with a global DIV named as “ListViewGlobalDIV” should be similar to the following:

I will make the “transparent.gif” transparent by using the following CSS code:
(“transparent.gif” is just a plain gray image having the approximate width and height of ListView1.)

This way of creating transparent images is not a CSS standard yet, but it works well in most of the modern browsers.
Notice the usage of absolute positioning in order to overlap TempTransparentDIV and UpdatePanel1.
Finally, time to write the JavaScript part:

If the JavaScript code refers to any objects from the ASP.NET AJAX Library, then it should be placed after the library has been loaded. Therefore, putting the above script to the end of the page would be a good practice since ScriptManager1 loads the library in this sample code.
As a result…
When you sort ListView1, it will be covered by “transparent.gif” with an 40% opacity and cursor will show the sandclock. All links would be unclickable till the sorting progresses are completed each time.

Download
Sample code can be downloaded via here.