The Details View automatically assigns the values assigned to its input controls to the Sql Data Source's corresponding Insert Parameters before starting the inserting workflow.The net result is that a new record is inserted into the database without writing a lick of code and without having to manually map the Sql Data Source's Insert Parameters to their sources (since the Details View does this for us automatically when Insert is clicked).
We will look at three examples: using a Sql Data Source to insert a record using values entered by the user through various Text Boxes, Drop Down Lists, and other Web controls on an ASP. This article focuses on the Sql Data Source control; for a detailed look at inserting with the Object Data Source, refer to the "Editing, Inserting, and Deleting" tutorials in my Working with Data in ASP. Let's explore the inserting "action" for the Access Data Source and Sql Data Source controls in more detail.We could set this control's s that point to the appropriate Web controls on that page. From the Designer, click on the Sql Data Source and go to the Properties window.There you will see an Insert Query option that, if clicked, displays the Command and Parameter Editor shown below., so no data will be displayed in the Details View.In fact, for this example we want the Details View to always be in an insertable mode (that is, we don't want to make the user have to click "New" to start adding a new record). You can accomplish these two tasks from the Fields dialog box or by manually entering the control's declarative markup.To use the Fields dialog box, click the Edit Fields link from the Details View's smart tag.
Add the two Bound Fields and Check Box Field and set their properties from the list on the right. When a user visits this page and enters the name, price, and discontinued status of a product and clicks Insert, a postback occurs.
The Details View and Form View controls have a lot in common - they both display one record at a time and can display interfaces for inserting and updating data.
The difference between the two is that the Details View control is composed of Data Fields (Bound Fields, Check Box Fields, Template Fields, and so on), just like the Grid View. The Form View, on the other hand, uses templates instead of Data Fields; consequently, it allows for a much more flexible layout of its display, inserting, and updating interfaces.
Likewise, if the user puts in an invalid unit price value (like "expensive!
"), an error will occur when attempting to insert the illegal value into the database.
The download at the end of this article includes another Details View example that illusrated adding validation controls and customizing the inserting interface to prompt the user for the new product's category via a drop-down list of available category choices.