DetailsView Control in ASP.NET - Part 6


Introduction

In Part 5 of this article series we have discussed how to customize paging interface in DetailsView Control but now in this article we will discuss how to update data with the DetailsView Control.


Updating Data with DetailsView Control

We use the DetailsView control to update existing database records. In order to update an existing record, assign the value True to the DetailsView control's AutoGenerateEditButton property as given below in example.



<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
   
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <asp:DetailsView
        id="dtlProducts"
        DataKeyNames="ID"
        AutoGenerateEditButton="true"
        AllowPaging="true"
        DataSourceID="SqlDataSource1"
        Runat="server" />
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [TITLE], [COMPANY], [PRICE] FROM [PRO_LIST]"
            UpdateCommand="UPDATE PRO_LIST SET ID =@ID, TITLE =@TITLE, COMPANY =@COMPANY, PRICE =@PRICE">
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>


If we open the page given above, record appears in Read Only mode. We can click the Edit button to switch the DetailsView into Edit mode and update the record. Notice that the DetailsView control includes a DataKeyNames property and an AutoGenerateEditButtonproperty. The DataKeyNames property contains the name of the primary key column. The AutoGenerateEditButton property automatically generates the user interface for editing the record. Also notice that the SqlDataSource control includes anUpdateCommand. The UpdateCommand updates the TITLE, COMPANY and PRICE database columns.

Note: Continue in Next Part.

Comments

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System