Smart ListControl in ASP.NET 1.x/2.0
page 1 of 7
Published: 16 Nov 2006
Abstract
In this article Bilal Haidar shows you how to make any ListControl that ships with ASP.NET 1.x/2.0 a smart control by making those controls preserve the client-side changes on their collection of items.
by Bilal Haidar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 38103/ 52

Introduction

In ASP.NET when a post back to the server occurs and it happens that a control of type ListControl is present on the ASPX page, the ListControl functions as follows.

Items present in the ListControl will not be posted back to the server whether in the Forms collection or the QueryString collection depending on the Action method you are using in the Web Form.

Only the selected value is sent back to the server upon a post back.  This explains how a selected item can be detected on the server.

When the response is back to the client, the ListControl items are retrieved from the ViewState.

The above facts pop up a serious problem, especially when you are doing any changes on the client side.  Changes can be removing or adding new items to the ListControl.

Any changes on the client side will not be reflected, since the shown items on the client side of a ListControl are not posted back with the page post back.  Whatever changes you do, they will disappear once a response is sent back from the server to the client (browser), since the ListControl as mentioned above retrieves its values from the ViewState.

In some applications there is a need to add new items to a ListControl or even remove some items.  Imagine two ListControls placed side by side with some buttons in between to move items between those two ListControl.  For instance, you might have two ListBoxes to hold finished and unfinished tasks, when a task is finished you move it to the other ListBox and if a task has been moved, however, you discover later on that some fixes are needed, so you bring it back to the unfinished ListBox.

To have good performance for you web form, such movements are to be done on the client side using some JavaScript instead of posting to the server on every movement. However, as listed above, there is a problem that those changes will not be preserved by the ListBox, because of the default behavior of the ListBox or any other ListControl in ASP.NET.

This article is meant to solve this problem by making the ASP.NET ListBox in particular a smart ListControl, where it preserves its changes upon posting back to the server.  Changes can be done by either adding new items or removing items from its collection of items.

We have taken as an example the ListBox.  However, the same technique can be used to make any control in ASP.NET that implements ListControl a smart control.


View Entire Article

User Comments

Title: Re:   
Name: Bilal Hadiar [MVP]
Date: 2007-04-04 1:51:36 AM
Comment:
Hello,
I have updated the control to work on other browsers than IE!
Check my post here:
http://bhaidar.net/cs/archive/2007/04/03/xlistbox-and-mover-control.aspx
Title: Re:   
Name: Bilal Haidar [MVP]
Date: 2006-12-09 6:40:35 AM
Comment:
Hello:
If it is not working on those two browsers, means there is something in the JavaScript that is specific for the IE only!
Let me check what I can do and come back to you!

Regards
Title: Re:   
Name: Bilal Hadiar [MVP]
Date: 2006-12-06 1:54:05 AM
Comment:
Hello, what exactly is happening? I don't have Safari here, all my work in the company is IE that is why I didn't test it there!

Regards
Title: Safari / doesnt work   
Name: Sonu Kapoor
Date: 2006-12-05 3:19:28 PM
Comment:
It doesnt work properly on safari!






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 7:19:00 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search