In this handler, we queried the database to get the details of the selected employee, then displayed the result in a DetailsView control. If we wanted to disallow the selection of a particular employee say, the employee with the ID 1 , we could do so by setting e. Cancel to True in the SelectIndexChanging event handler, as shown below: The built-in column types such as bound columns, check box columns, and so on have built-in editing templates, which you can customize by providing custom templates.
Either way, you should end up with the following code: Complete the generated code like this: The DetailsView control supports three display modes. You can change the current mode using its ChangeMode method, providing as parameter one of these values: ReadOnly This is the default mode, which is used to display data.
When you execute your project, and load the details of an employee, you see those details in ReadOnly mode. Edit This mode is used to edit an existing record. We saw this mode in action earlier, when we clicked the Edit button. Insert We use this mode to insert a new record. Its value will be DetailsViewMode. Edit when the ModeChanging event is fired as a result of the Edit button being clicked.
Select that option to open a dialog that provides us with a great deal of control over the options for each column or field. Click the smart tag of your DetailsView control, then click Edit Fields. After you convert the field to a template field, its definition will look like this: Visual Web Developer did a little bit of magic for us: The difference is that now you can easily refer to these inner controls from your code, you can easily change their appearance using custom HTML code, and, if you wish, you can replace them with totally different controls.
The power is in your hands. For example, you can widen the TextBox controls that are used to edit the fields, as well as performing other kinds of customizations. You can also give specific IDs to the inner template controls, rather than using their default generic names, so that you can find them easily when you need to.
Now, convert the other fields—except for CommandField—to template fields. The ItemUpdating event is triggered when the Update link is clicked—an action that will occur once the user enters new data into the text boxes and is ready to commit the updated data to the database.
If you decide to have a go and you should! To take care of the actual database update, though, we will be using a stored procedure called UpdateEmployeeDetails. Add " EmployeeID", Data. Add " NewAddress", Data. Add " NewCity", Data. Close End Try employeeDetails. NVarChar, 50 ; comm. It starts by reading the value of the DataKey of the DetailsView object. We read this information in the ItemUpdating event handler, like so: Value; The next step is to find the TextBox objects that contain the updated data.
Text; After we obtain the data we wish to insert into the database, we call the UpdateEmployeeDetails stored procedure we created earlier.