DetailsView Control in ASP.NET - Part 10


Introduction

In Part 9 of this article series we have discussed how to Delete Data with DetailsView Control. Now in this article we will discuss how to work with DetailsView Control Events.


DetailsView Control Events

DetailsView control supports the following events:

·         DataBinding: Raised immediately before the DetailsView control is bound to its data source.
·         DataBound: Raised immediately after the DetailsView control is bound to its data source.
·         ItemCommand: Raised when any control contained in the DetailsView raises an event.
·         ItemCreated: Raised when a DetailsView renders a data item.
·         ItemDeleting: Raised immediately before a data item is deleted.
·         ItemDeleted: Raised immediately after a data item is deleted.
·         ItemInserting: Raised immediately before a data item is inserted.
·         ItemInserted: Raised immediately after a data item is inserted.
·         ItemUpdating: Raised immediately before a data item is updated.
·         ItemUpdated: Raised immediately after a data item is updated.
·         ModeChanging: Raised immediately before the DetailsView control's mode is changed.
·         ModeChanged: Raised immediately after the DetailsView control's mode is changed.
·         PageIndexChanging: Raised immediately before the current page is changed.
·         PageIndexChanged: Raised immediately after the current page is changed.

Several of these events reflect similar events exposed by the DataSource controls. For example, the SqlDataSource control includesInserting and Inserted events, which mirror the DetailsView control's ItemInserting and ItemInserted events. The page given below demonstrates how to use the ItemInserted event to handle any errors which might be raised when inserting a new record into a database table. To demonstrate that OnItemInserted event is working or not. By default I have used correct coding so it will not show you any error messages. If you want to test it out then change the following code

InsertCommand="INSERT INTO BOOK_LIST(ID, TITLE, AUTHOR, PRICE) VALUES (@ID, @TITLE, @AUTHOR, @PRICE)"

by

InsertCommand="INSERT INTO BOOK_LIST(IDS, TITLE, AUTHOR, PRICE) VALUES (@ID, @TITLE, @AUTHOR, @PRICE)"

In second line I have used IDS not ID and this will be the cause of error. Now if we click on New button and now click on insert after entering the data it will how you following error.



<%@ 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">
    Protected Sub dtlBooks_ItemInserted(ByVal sender As Object, ByVal e As DetailsViewInsertedEventArgs)
        If Not IsNothing(e.Exception) Then
            e.ExceptionHandled = True
            e.KeepInInsertMode = True
            lblError.Visible = True
        End If
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">

    <style type="text/css">
        .error
        {
            color:red;
            font:bold 14px Arial,Sans-Serif;
        }
    </style>


    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <asp:Label
        id="lblError"
        Text="Error occured while inserting!"
        Visible="false"
        EnableViewState="false"
        CssClass="error"
        Runat="server" />

    <asp:DetailsView
        id="dtlBooks"
        AllowPaging="true"
        DataSourceID="SqlDataSource1"
        AutoGenerateInsertButton="true"
        OnItemInserted="dtlBooks_ItemInserted"
        Runat="server" />

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            InsertCommand="INSERT INTO BOOK_LIST(ID, TITLE, AUTHOR, PRICE) VALUES (@ID, @TITLE, @AUTHOR, @PRICE)"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [TITLE], [AUTHOR], [PRICE] FROM [BOOK_LIST]">
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>


That’s very cool, isn’t it?

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