How to use Visual Studio 2005 to create an ASP.NET 2.0 Web application that uses drop-down lists for users to edit data

Article translations Article translations
Article ID: 937094 - View products that this article applies to.
Expand all | Collapse all

INTRODUCTION

This article describes how to use Microsoft Visual Studio 2005 to create an ASP.NET 2.0 Web application that uses drop-down lists for users to edit data. By default, the data appears in a text box when a user edits the data. However, when the data appears in a text box, the user may type incorrect data.

Consider the following scenario:
  • The data source is a database table that contains the following fields:
    • ID
    • department
    • name
  • You bind the data source to a GridView control in an ASP.NET Web application.
  • You enable editing so that users can update data in the database table.
  • A user views the Web page and clicks Edit to update a record.
In this scenario, the user may type incorrect data in the field. For example, the user may spell the department data or the name data incorrectly.

To avoid this issue, you can use a drop-down list to display the valid data for a field.

MORE INFORMATION

You can create a Web application that uses drop-down lists for editing fields. To do this, follow these steps.

Note This example uses the PUBS database that is included with Microsoft SQL Server 2000 as the data source. However, you can use any data source.
  1. Create a Web application project. To do this, follow these steps:
    1. Start Visual Studio 2005.
    2. Click File, point to New, and then click Web site.
    3. In the Language list, select Visual Basic, and then click ASP.NET Web Site under Templates.
    4. Type a name for the Web site, and then click OK.
  2. From the Data node of the Toolbox, add a SQLDataSource object to the project, and then configure the object. To configure the SQLDataSource object, follow these steps:
    1. Right-click the SQLDataSource object, and then click Configure Data Source. The Configure Data Source wizard opens.
    2. In the Configure Data Source wizard, configure the SQLDataSource object to connect to the PUBS database, and then click Next two times.
    3. Click Specify columns from a table or view, and then click authors in the Name box.
    4. Click to select the * check box.
    5. Click Advanced, click to select the Generate INSERT, UPDATE, and DELETE statements check box, click OK, and then click Next.
    6. Verify that SELECT * FROM [authors] appears in the SELECT statement box, and then click Finish.
  3. Add a GridView control to the project, and then configure the control. To configure the GridView control, follow these steps:
    1. In the GridView Tasks flyout panel, configure the GridView control to use the SQLDataSource that you added in step 2.
    2. In the GridView Tasks flyout panel, click to select the Enable Editing check box.
  4. On the Debug menu, click Start Debugging.
  5. Click Edit to edit one record in the database table. Notice that a text box appears for each field.
  6. Add another SQLDataSource object to the project, and then configure the object. To configure the SQLDataSource object, follow these steps:
    1. Right-click the SQLDataSource object, and then click Configure Data Source. The Configure Data Source wizard opens.
    2. In the Configure Data Source wizard, configure the SQLDataSource object to use the same database connection that you used in step 2b, and then click Next.
    3. Click Specify columns from a table or view, and then click authors in the Name box.
    4. Click to select the state check box, click to select the Return only unique rows check box, and then click Next.
    5. Verify that SELECT DISTINCT [state] FROM [authors] appears in the SELECT statement box, and then click Finish.
  7. In the GridView Tasks flyout panel for the GridView control, click Edit Columns. The Fields dialog box appears.
    1. In the Fields dialog box, click state under Selected field.
    2. Click Convert this field into a TemplateField, and then click OK.
  8. In the GridView Tasks flyout panel, click Edit Templates.
  9. In the GridView Tasks flyout panel, click EditItemTemplate in the Display box.
  10. Delete the text box that appears under EditItemTemplate.
  11. Add a DropDownList control to the template, and then configure the control. To configure the data source for the DropDownList control, follow these steps:
    1. In the DropDownList Tasks flyout panel, click Choose Data Source. The Configure Data Source wizard opens.
    2. In the Configure Data Source wizard, click to select the SQLDataSource object that you added in step 6, and then click OK.

      Note State must appear in the Select a data field to display in the DropDownList box and in the Select a data field for the value of the DropDownList box.
  12. Configure the data bindings for the DropDownList control. To do this, follow these steps:
    1. In the DropDownList Tasks flyout panel, click Edit DataBindings. The DropDownList Databindings dialog box appears.
    2. In the DropDownList Databindings dialog box, click SelectedValue under Bindable properties.
    3. Click to select Field binding, click state in the Bound to box, and then click OK.
  13. On the Debug menu, click Start Debugging.
  14. Click Edit for one of the records. Notice that a drop-down list appears for the state field.

REFERENCES

For more information about how to create Web applications in Visual Studio 2005, visit the following Microsoft MSDN Web site:
http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx

Properties

Article ID: 937094 - Last Review: February 19, 2008 - Revision: 2.1
APPLIES TO
  • Microsoft .NET Framework 2.0
  • Microsoft Visual Studio 2005 Team Suite
  • Microsoft Visual Studio 2005 Team Edition for Software Architects
  • Microsoft Visual Studio 2005 Team Edition for Software Developers
  • Microsoft Visual Studio 2005 Team Edition for Software Testers
  • Microsoft Visual Studio 2005 Tools for the Microsoft Office System
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
Keywords: 
kbvs2005pro kbvs2005std kbhowto KB937094

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com