Personal Blog of Mustafa Basgun

Archive for the ‘VB.NET’ Category

Want To Validate Username Availability Asynchronously?

leave a comment »

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:

mustafa-basgun-blog-aspnet-validate-username-1

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

mustafa-basgun-blog-aspnet-validate-username-2

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:

mustafa-basgun-blog-aspnet-validate-username-3

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:

mustafa-basgun-blog-aspnet-validate-username-4

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.

Written by Mustafa Basgun

October 6, 2008 at 9:51 PM

Manual Sorting with ListView by Using Drag and Drop

leave a comment »

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.

Written by Mustafa Basgun

June 30, 2008 at 6:00 PM

Enhancing Paging in ListView by Using DataPager

with 3 comments

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.

Written by Mustafa Basgun

May 7, 2008 at 10:30 AM

Rotate Array by N Position – Via Programming Pearls

with 2 comments

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:

Written by Mustafa Basgun

April 11, 2008 at 6:00 AM

Posted in C#, VB.NET

Disable UpdatePanel’ed ListView with CSS and JavaScript While Sorting

with one comment

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.

Written by Mustafa Basgun

March 14, 2008 at 9:00 AM

Create A Simple Dynamic Clock by Using UpdatePanel of ASP.NET AJAX

with 23 comments

If you are developing web applications for different customer scenarios in which timestamping matters, there is a chance that you will be asked to put a real-time dynamic clock on the page depending on your real estate limitations on the UI. Any search result would give you several remedies to this purpose, but I wanted to have something short, neat and cross-browser compatible.

Here is the solution that I came up with by using an UpdatePanel and a few lines of JavaScript code.

First, create the UpdatePanel as in the following sample form:

OnLoad event of upDynamicClock will be handled in the server side:

Create a listener function (DisplayDynamicClock) by using the native setInterval function of JavaScript in order to update the content of lblDynamicClock and tell the page to run this function via window.onload:

Notice the usage of “__doPostBack” function as a bridge between JavaScript and ASP.NET for triggering a postback at upDynamicClock.

Conclusion

I have tested the outcome with the latest versions of IE and Firefox, and it worked fine with both of them:

Download

Sample code for this post can be found here.

Written by Mustafa Basgun

February 27, 2008 at 4:00 PM

Extract Zip File By Using SharpZipLib Library in ASP.NET

with 5 comments

If you are developing an IO style application, it is a high possibility that one of the requirements would ask you a utility to extract an uploaded zip file. Well, here is a one way to accomplish this with VB.NET by using the SharpLibZip library.

After downloading the appropriate binary file and placing it to the Bin directory of your project, let’s have the webform similar to the following:

HTML presentation of this webform should look like:

Click event of Button1 is the one using the ExtractZip class:

Now, time to delve into the ExtractZip class! Code speaks pretty well for itself, therefore I will not be putting detailed description. Parameters are:

Before jumping into the extraction, it would be a good practice to perform some simple checks:

And, the Try Catch statement doing the main job is:

Delete the original zip file if the optional DeleteZip parameter was left as True:

Download

Working sample of this ExtractZip class can be downloaded via here.

Written by Mustafa Basgun

February 8, 2008 at 5:00 PM