Detailsview Commandfield Update
In order to use the GridView and DetailsView controls effectively, we need to know how to handle their events. In this section, we’ll learn about the events raised by these controls. We’ll focus on the events that relate to editing and updating data, as our next goal will be to allow users to edit employee details in the DetailsView. Earlier, you learned how to respond to the user’s clicking of the Select button by handling the GridView’s SelectedIndexChanged event. Soon you’ll implement editing functionality, which you’ll achieve by adding an Edit button to the DetailsView control.
The editing features of the GridView and the DetailsView are very similar, so you can apply the same principles—and almost the same code—to both of them. Both the GridView and DetailsView controls support Edit command buttons, which will place Edit buttons in the control when the page loads. Once one of the Edit buttons has been clicked, the row (in case of GridView) or the entire form (in case of DetailsView) will become editable, and instead of an Edit button, users will see Update and Cancel buttons. These features are fairly amazing, because you can achieve this “edit mode” without writing any HTML at all: the columns know how to render their editable modes by themselves. If you don’t like their default edit mode appearances, you can customize them using templates. Before we write any code, let’s see what this edit mode looks like. Figure 11.12 shows a GridView control in which one row appears in edit mode.
How to access DetailsView CommandField in DataBound event of the DetailsView control. DetailsView Members. Occurs when an Update button within a DetailsView control is clicked. CommandField CheckBoxField GridView HyperLinkField. Nov 20, 2011 Hi, I try using DetailsView with SqlDataSource component. I already defined all select-command until delete command. The DetailsView. Aug 14, 2012 asp:DetailsView - Edit / Delete. Calling Stored Procedures to Update / Insert. Load Flow Software here. I bring a sample code for detailsView here, Notice to CommandField.
Action Events Triggered When Clicked Select SelectedIndexChanging, SelectIndexChanged Edit RowEditing Update RowUpdating, RowUpdated Cancel RowCancelingEdit Delete RowDeleting, RowDeleted (sorting buttons) RowSorting, RowSorted (custom action) RowCommand The DetailsView control, on the other hand, has buttons and events that refer to items, rather than rows, which makes sense when you realize that DetailsView is used to display the items in one record, while GridView displays a few items from many records. The DetailsView action types, and the events they generate, are listed in Table 11.2. <%#Eval('Name')%>The new item will appear in the designer as an Edit link immediately below the list of columns. If you execute the project and click that Edit link, an exception will be thrown, telling you that you didn’t handle the ModeChanging event. The DetailsView control doesn’t know how to switch itself to edit mode, but fortunately, it’s extremely easy to write the code yourself. To have Visual Web Developer generate the ModeChanging event signature for you, open AddressBook.aspx in Design view, select the DetailsView control, click the yellow button with the lightning symbol in the Properties window to display the list of the control’s events, and double-click on the ModeChanging entry.
This will generate an empty event handler for you (as well as the onmodechanging property on the DetailsView control if you’re using C#), and take you straight to the function in the code-behind file. Vista Msstyles Deviantart. Complete the generated code like this.
DetailsViewMode.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.
DetailsViewMode.Edit This mode is used to edit an existing record. We saw this mode in action earlier, when we clicked the Edit button. DetailsViewMode.Insert We use this mode to insert a new record. It’s similar to the edit mode, except all the controls are empty, so you can fill in data for a new item. If you look at the employeeDetails_ModeChanging, you’ll see it receives a parameter named e that is an object of class DetailsViewModeEventArgs. E’s NewMode property tells us which display mode was requested by the user. Its value will be DetailsViewMode.Edit when the ModeChanging event is fired as a result of the Edit button being clicked.
We pass this value to the DetailsView control’s ChangeMode method, which does exactly as its name suggests: it changes the mode of the DetailsView. With this code, you’ve implemented the functionality to make both the Edit and Cancel buttons work correctly, as we’ll see in an example shortly.