DetailsView Control in ASP.NET - Part 7


Introduction

In Part 6 of this article series we have discussed how to update data with DetailsView Control but now in this article we will discuss how add template fields in DetailsView for to increase the editing standards.


Template Fields in DetailsView Control

In DetailsView Control by default, we do not get any validation when editing records. In other words, there is nothing to prevent us from attempting to submit a null value to a database column that does not accept null values. It is very important to use client side validation in database programming. If we need to perform validation, then we need to use templates with the DetailsView control.The page given below uses TemplateFields for the TITLE, COMPANY and PRICE columns. All TemplateFields contain aRequiredFieldValidator. The PRICE column also includes a CompareValidator to check whether the value entered is a currency value.


<%@ 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"
        AutoGenerateRows="false"
        AutoGenerateEditButton="true"
        AllowPaging="true"
        DataSourceID="SqlDataSource1"
        DataKeyNames="ID"
        Runat="server">
        <Fields>
        <asp:TemplateField HeaderText="Product Name:">
        <EditItemTemplate>
        <asp:TextBox
            id="txtTitle"
            Text='<%# Bind("TITLE") %>'
            runat="server" />
        <asp:RequiredFieldValidator
            id="reqTitle"
            ControlToValidate="txtTitle"
            Text="Can not be Null!"
            Display="Dynamic"
            Runat="server" />
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Company Name:">
        <EditItemTemplate>
        <asp:TextBox
            id="txtCompany"
            Text='<%# Bind("COMPANY") %>'
            runat="server" />
        <asp:RequiredFieldValidator
            id="reqCompany"
            ControlToValidate="txtCompany"
            Text="Can not be Null!"
            Display="Dynamic"
            Runat="server" />
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Market Price:">
        <EditItemTemplate>
        <asp:TextBox
            id="txtPrice"
            Text='<%# Bind("PRICE", "{0:f}") %>'
            runat="server" />
        <asp:RequiredFieldValidator
            id="reqPrice"
            ControlToValidate="txtPrice"
            Text="Can not be Null!"
            Display="Dynamic"
            Runat="server" />
        <asp:CompareValidator
            id="cmpPrice"
            ControlToValidate="txtPrice"
            Text="Invalid Price!"
            Display="Dynamic"
            Operator="DataTypeCheck"
            Type="currency"
            Runat="server" />
        </EditItemTemplate>
        </asp:TemplateField>
        </Fields>
    </asp:DetailsView>

        <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 WHERE ID=@ID">
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>

Now if we attempt to edit a record and we do not provide a value for the Title, Company or Price columns, then a validation error is displayed. Also, if we enter anything other than a currency amount for the Price column, a validation error message is displayed. That’s very cool.

Note: Continue in Next Part.

Comments

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

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