Export (0) Print
Expand All

Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control 

The GridView control displays text boxes for editing by default. You can use the ASP.NET GridView control's built-in advanced functionality to add a drop-down list to the editing display. Tasks illustrated in this walkthrough include:

  • Configuring a GridView control to display SQL data.

  • Displaying data in the GridView control.

  • Displaying a drop-down list while editing in the GridView control.

In order to complete this walkthrough, you will need:

  • Microsoft Visual Web Developer (Visual Studio).

  • Microsoft Data Access Components (MDAC) version 2.7 or later.

    If you are using Microsoft Windows XP or Windows Server 2003, you already have MDAC 2.7. However, if you are using Microsoft Windows 2000, you might need to upgrade the MDAC already installed on your computer. For more information, see "Microsoft Data Access Components (MDAC) Installation" in the MSDN Library.

  • Access to the SQL Server Northwind database. For information about downloading and installing the SQL Server sample Northwind database, see Installing Sample Databases on the Microsoft SQL Server Web site.

    NoteNote

    If you need information about how to log on to the computer running SQL Server, contact the server administrator.

Create a new Web site and page by following these steps.

To create a file system Web site

  1. Open Visual Web Developer.

  2. On the File menu, click New, and then click Web Site. If you are using Visual Web Developer Express, on the File menu, click New Web Site.

    The New Web Site dialog box appears.

  3. Under Visual Studio installed templates, click ASP.NET Web Site.

  4. In the Location box, select File System, and enter the name of the folder where you want to keep the pages of your Web site.

    For example, type the folder name C:\WebSites\DropDownEdit.

  5. In the Language list, click the programming language you prefer to work in.

  6. Click OK.

    Visual Web Developer creates the folder and a new page named Default.aspx.

To start, you must create a connection to the computer running SQL Server where you have access to the Northwind database.

To create a connection to SQL Server

  1. In Server Explorer, right-click Data Connections, and then click Add Connection. If you are using Visual Web Developer Express, use Database Explorer.

    The Add Connection dialog box appears.

    • If the Data source list does not display Microsoft SQL Server (SqlClient), click Change, and in the Change Data Source dialog box, select Microsoft SQL Server.

    • If the Choose Data Source page appears, in the Data source list, select the type of data source you will use. For this walkthrough, the data source type is Microsoft SQL Server. In the Data provider list, click .NET Framework Data Provider for SQL Server, and then click Continue.

    NoteNote

    If the Server Explorer tab is not visible in Visual Web Developer, in the View menu, click Server Explorer. If the Database Explorer tab is not visible, in the View menu, click Database Explorer.

  2. In the Add Connection box, enter your server name in the Server Name box.

  3. For the Log on to the server section, select the option that is appropriate to access the running SQL Server database (integrated security or specific ID and password) and, if required, enter a user name and password.

  4. Select the Save my Password check box.

  5. Under Select or enter a database name, enter Northwind.

  6. Click Test Connection, and when you are sure that it works, click OK.

    Your new connection has been created under Data Connections in Server Explorer.

In this part of the walkthrough, you dynamically populate the grid with data.

To enable simple sorting

  1. Switch to or open the Default.aspx file

  2. Switch to Design view.

  3. In the Toolbox, from the Data group, drag a GridView control onto the page.

  4. On the GridView Tasks menu, in the Choose Data Source box, click <New data source>.

    The Data Source Configuration wizard appears.

  5. Click Database.

    This specifies that you want to get data from a database that supports SQL statements, which includes SQL Server and other OLE-DB–compatible databases.

  6. In the Specify an ID for the data source box, a default data source control name, SqlDataSource1, is displayed. You can leave this name.

  7. Click OK.

    The Configure Data Source wizard is displayed.

  8. In the Which data connection should your application use to connect to the database? box, enter the connection that you created in "To create a connection to SQL Server," and then click Next.

    The wizard displays a page in which you can choose to store the connection string in a configuration file. Storing the connection string in the configuration file has two advantages:

    • It is more secure than storing it in the page.

    • You can use the same connection string in multiple pages.

  9. Select the Yes, save this connection as check box, and then click Next.

    The wizard displays a page in which you can specify what data you want to retrieve from the database.

  10. Under Specify columns from a table or view, in the Name box, click Employees.

  11. Under Columns, select the EmployeeId, LastName, FirstName, HireDate, and City check boxes.

    The wizard displays the SQL statement that you are creating in a box at the bottom of the page.

    NoteNote

    The wizard allows you to specify selection criteria (WHERE clauses) and other SQL query options. For this walkthrough, you will create a simple statement with no selection or sort options.

  12. Click Advanced, select the Generate INSERT, UPDATE, and DELETE statements check box, and then click OK.

    This generates Insert, Update, and Delete statements for the SqlDataSource1 control based on the Select statement that you configured earlier.

    NoteNote

    Alternatively, you could manually create the statements by selecting Specify a custom SQL statement or stored procedure and entering SQL queries.

  13. Click Next.

  14. Click Test Query to be sure that you are retrieving the data you want.

  15. Click Finish.

  16. Right-click the GridView control and select Show Smart Tag. From the GridView Tasks menu, select the Enable Editing box.

You can now test the page.

To test the page

  1. Press CTRL+F5 to run the page.

    The GridView control is displayed with EmployeeId, LastName, FirstName, HireDate, and City columns.

  2. Click the Edit link next to a row.

    The row selected for editing is displayed with LastName, FirstName, HireDate, and City columns as an editable text box. EmployeeId is not displayed in a text box, because it is a key field and not editable.

  3. Change a field such as LastName and click Update.

    The GridView control is displayed with EmployeeId, LastName, FirstName, HireDate, and City columns, with LastName updated with the new value.

  4. Close the browser.

In this part of the walkthrough, you can add a drop-down list to select from while editing the rows in the grid.

To display a drop-down list while editing

  1. From the Data node of the Toolbox, drag a SqlDataSource control onto the page.

    A new data source control named SqlDataSource2 is created.

  2. In the SqlDataSource Tasks menu, choose Configure Data Source.

  3. In the Which data connection should your application use to connect to the database? box, enter the connection that you created earlier.

  4. Click Next.

  5. On the Configure the Select Statement page, select Specify columns from a table or view, and then in the Name box, click Employees.

  6. Select only the City column, and then select the Return only Unique Rows check box. Click Next.

  7. Click Test Query to preview the data, and then click Finish.

  8. Right-click the GridView control and select Show Smart Tag. In the GridView Tasks menu, select Edit Columns.

  9. In the Fields dialog box, select City from the Selected fields list box.

  10. Click Convert this field into a TemplateField link.

  11. Click OK to close the Fields dialog box.

  12. Right-click the GridView control and select Show Smart Tag. In the GridView Tasks menu, select Edit Templates.

  13. Select EditItemTemplate in the Display drop-down list.

  14. Right-click the default TextBox control in the template and select Delete to remove it.

  15. From the Standard tab of the Toolbox, drag a DropDownList control onto the template.

  16. Right-click the DropDownList control and select Show Smart Tag. In the DropDownList Tasks menu, select Choose Data Source.

  17. Select SqlDataSource2.

  18. Click OK.

  19. In the DropDownList Tasks menu, choose Edit DataBindings. The SelectedValue property of the DropDownList control is selected in the DataBindings dialog box.

  20. Click the Field Binding radio button and select City for Bound To.

  21. Select the Two-way databinding check box.

  22. Click OK.

  23. Right-click the GridView control and select Show Smart Tag. In the GridView Tasks menu, click End Template Editing.

    Security noteSecurity Note

    User input in an ASP.NET Web page can include potentially malicious client script. By default, ASP.NET Web pages validate user input to make sure input does not include script or HTML elements. As long as this validation is enabled, you do not need to explicitly check for script or HTML elements in user input. For more information, see Script Exploits Overview.

You can now test the page.

To test the page

  1. Press CTRL+F5 to run the page.

    The GridView control is displayed with EmployeeId, LastName, FirstName, HireDate, and City columns.

  2. Click the Edit link next to a row.

    The current City value is preselected in the drop-down list.

    Select a different City value from the drop-down list and click Update.

    The City field is updated using the value selected in the drop down list.

Community Additions

ADD
Show:
© 2014 Microsoft