This sample shows (using the Pubs Database), using a Master/Detail view, how to use the new ASP.Net 2.0 DetailsView control to edit a record. One advantage of editing this way, is that the DetailsView control uses up less real estate on the screen.
There are very few items that actually change from our other Master/Detail sample. In order to set up the DetailsView control for editing, all that is needed is the added CommandField section, in the Fields Section, and an Update SQL Statement. Here, we could just as easily have assigned the UpdateCommand and SelectCommand in the SQL DataSource, but, for this sample, it uses less width to do it this way, and also it shows how to assign these statements programmatically.
Also notice, that, in order to update the GridView, after updating with the DetailsView control, there is one extra subroutine, (called ‘DVAfterUpdate’ in this sample). Its only purpose is to update the GridView. Of course we need to reference the sub, using the OnItemUpdated property of the DetailsView, but as you can see, with the new controls of ASP.Net 2.0, updating your database is much easier now!
<script language="VB" Runat="server">
Dim strUpdate as String
Dim strSelect as String
Sub Page_Load(Source as Object, E as EventArgs)
strSelect="SELECT [au_id], [au_lname], [au_fname], [phone], " & _
"[address], [city], [state], [zip], [contract] FROM [authors] " & _
"WHERE ([au_id] = @au_id)"
strUpdate="UPDATE [authors] SET [au_lname]=@au_lname, " & _
"[au_fname]=@au_fname, [phone]=@phone, [address]=@address, " & _
"[city]=@city, [state]=@state, [zip]=@zip, [contract]=@contract " & _
"WHERE [au_id]=@au_id"
SQLDS3.SelectCommand=strSelect
SQLDS3.UpdateCommand=strUpdate
End Sub
Sub DVAfterUpdate(sender As Object, e As DetailsViewUpdatedEventArgs)
gvAuthors.DataBind()
End Sub
</script>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 4.5">
<title>
Master/Detail Editing with DetailsView
</title>
</head>
<body>
<form id="form1" Runat="server">
<table>
<tr>
<td align="Center" valign="Top">
<asp:GridView Runat="server"
Id="gvAuthors"
GridLines="Both"
cellpadding="0"
cellspacing="0"
Headerstyle-BackColor="#BDCFE7"
Headerstyle-Font-Name="Arial"
Headerstyle-Font-Size="12"
BackColor="#E7EFFF"
Font-Name="Arial"
Font-Size="10"
BorderColor="Black"
DataSourceID="SQLDS2"
DataKeyNames="au_id"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="au_id" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="au_lname" HeaderText="LName" />
<asp:BoundField DataField="au_fname" HeaderText="Fname" />
<asp:BoundField DataField="state" HeaderText="State" />
</Columns>
</asp:GridView>
</td>
<td align="Left" valign="Top">
<asp:DetailsView
DataKeyNames="au_id" DataSourceID="SQLDS3"
GridLines="Both"
cellpadding="0"
cellspacing="0"
Headerstyle-BackColor="#BDCFE7"
Headerstyle-Font-Name="Arial"
Headerstyle-Font-Size="12"
BackColor="#E7EFFF"
Font-Name="Arial"
Font-Size="10"
BorderColor="Black"
HeaderText="Author Details" AutoGenerateRows="False"
HeaderStyle-Font-Bold="True"
OnItemUpdated="DVAfterUpdate"
ID="dvPubs" runat="server">
<Fields>
<asp:BoundField DataField="au_id" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="au_lname" HeaderText="Lname" />
<asp:BoundField DataField="au_fname" HeaderText="Fname" />
<asp:BoundField DataField="phone" HeaderText="Phone" />
<asp:BoundField DataField="address" HeaderText="Address" />
<asp:BoundField DataField="city" HeaderText="City" />
<asp:BoundField DataField="state" HeaderText="St" />
<asp:BoundField DataField="zip" HeaderText="Zip" />
<asp:CheckBoxField DataField="contract" HeaderText="Contract" />
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
</td>
</tr>
</table>
<asp:SqlDataSource ID="SQLDS2" runat="server"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]"
ConnectionString="<%$ ConnectionStrings:YourConnStringGoesHere %>">
</asp:SqlDataSource>
<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:YourConnStringGoesHere %>"
ID="SQLDS3" runat="server">
<SelectParameters>
<asp:ControlParameter ControlID="gvAuthors" Name="au_id"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>