.NET Framework: How to Use Simple Controls Shared By Web and Windows Forms

This sample application demonstrates how to databind and access selected information from single- and multi-select controls. The closest equivalent controls for both Windows Forms and Web Forms applications are used. The Windows Forms sample demonstrates databinding and selected/checked item information gathering for the ComboBox and the CheckedListBox. The Web Forms sample uses the DropDownList and CheckBoxList controls.

Featured Highlights

Requirements

This application requires the release version of Visual Studio .NET Professional (or greater) and access to the Northwind database residing in SQL Server or the Microsoft Data Engine (MSDE). To install MSDE, do the following:

1. Open the Start menu, click Programs, click Microsoft .NET Framework SDK, and then click Samples and Quickstart Tutorials.

2. Click "Step 1: Install the .NET Framework Samples Database".

3. Open a command window, and CD to [FRAMEWORKSDK]\Samples\Setup This is typically : C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\Setup

4. Type: osql -E -S (local)\NetSDK -i InstNwnd.sql.

Running the Sample

To install the Web Forms application:

1. Double-click SetupWebApp.vbs in the root folder. (Note: The Visual Studio .NET Solution file resides in the "How To" root folder, not at c:\inetpub\wwwroot\HowToUseSimpleWebControls.)

To run the Web Forms client:

1. In the Solution Explorer, right-click HowToUseSimpleWebControls and then click Set as StartUp Project.
2. Right-click Main.aspx and then click Set As Start Page.
3. Press F5 to build and run the sample.

If you get the following error the aspnet worker process is locking the Web-based DLLs. Take the corrective steps that follow.

Could not copy temporary files to the output directory.
The file 'bin\HowToUseSimpleWebControls.dll' cannot be copied to the run directory. The process cannot access the file because it is being used by another process.
The file 'bin\HowToUseSimpleWebControls.pdb' cannot be copied to the run directory. The process cannot access the file because it is being used by another process.

1. Open the IIS Management Console.
2. Drill down to the Default Web Site, then right-click and then click Properties.
3. Click the Home Directory tab.
4. Make sure there is no trailing "\" in the Local Path fiel (i.e., it should read "c:\inetpub\wwwroot" not "c:\inetpub\wwwroot\").
5. Click OK to exit the Properties dialog.
6. For more information on this issue, click here.

If the problem persists you can temporarily resolve it on each build with the following steps:

1. Press CTRL + ALT + Delete to open the Windows Task Manager.
2. Click the Processes tab.
3. Click the Image Name ListView header to sort aspnet_wp.exe to the top.
4. Click aspnet_wp.exe and then click End Process.
5. Close Windows Task Manager.

To run the Windows Forms client:

1. In the Solution Explorer, right-click VB.NET How-To Use Simple Windows Forms Controls and then click Set as StartUp Project.
2. Press F5 to build and run the sample.

Notes:

1 For the Helper.UI.AddOption method to work the DataSet must be pre-sorted. This is easy to do in the SQL statement. Without pre-sorting the DataSet you would be left with having to create a DataView and sorting on the DisplayMember (DataTextField) or ValueMember (DataValueField), which may not always achieve the results you want. For example, in this sample application the ComboBox is filled with data from the Products table. One of the products alphabetically precedes the phrase "All Products". Thus, if you sorted by the DisplayMember (DataTextField) "All Products" would be listed second. If you sorted by ValueMember (DataValueField), "All Products" would be listed first (because its value = 0) but the product names would not be in alphabetical order. Usually this is undesirable. If you can pre-sort, DataTable.Rows.InsertAt(0) works well.

2. Following are tables summarizing the usage differences between the single- and multi-select controls in this How-To.

ComboBox (in DropDownList mode, Windows Forms)

DropDownList (Web Forms)

DisplayMember and ValueMember are used to set the databindings. DataTextField and DataValueField are used to set the databindings.
There is no DataBind method. Databinding occurs automatically. However, when the datasource is a DataSet you must explicitly set the DataTable, e.g., clstProducts.DataSource = dsProducts.Tables(0). You must explicitly call the DataBind method. You can also bind directly to a DataSet unless it contains more than one DataTable, in which case you would need to explicitly set the DataTable.
Access information about the selected item using the SelectedIndex, SelectedValue, and Text properties. Access information about the selected item using the SelectedIndex, SelectedItem.Value, and SelectedItem.Text properties.

CheckedListBox (Windows Forms)

CheckBoxList (Web Forms)

The first two rows in the above table also apply to these controls.
"Selected" means "highlighted", which is different than "checked". "Selected" means "checked".
Exposes a SelectedItems and CheckedItems collection. The former is not relevant as it contains 0 or 1 items. Exposes only an Items collection. You must iterate through all items and check the value of the Selected property. (There is no Checked property.)
Objects in the CheckedItems collection are of type DataRowView. Objects in the Items collection are of type ListItem.