2023年3月14日 星期二

[研究][ASP.NET]DetailsView事件執行順序

[研究][ASP.NET]DetailsView事件執行順序

2023-03-14

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#


********************************************************************************

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
            SelectCommand="SELECT [BookName], [SN] FROM [Table3]" 
            DeleteCommand="DELETE FROM [Table3] WHERE [SN] = @SN" 
            InsertCommand="INSERT INTO [Table3] ([BookName]) VALUES (@BookName)" 
            UpdateCommand="UPDATE [Table3] SET [BookName] = @BookName WHERE [SN] = @SN">
            <DeleteParameters>
                <asp:Parameter Name="SN" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="BookName" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="BookName" Type="String" />
                <asp:Parameter Name="SN" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="500px" AutoGenerateRows="False"
            DataSourceID="SqlDataSource1"
            OnItemCommand="DetailsView1_ItemCommand"
            OnItemDeleted="DetailsView1_ItemDeleted"
            OnItemDeleting="DetailsView1_ItemDeleting"
            OnItemInserted="DetailsView1_ItemInserted"
            OnItemInserting="DetailsView1_ItemInserting"
            OnItemUpdated="DetailsView1_ItemUpdated"
            OnItemUpdating="DetailsView1_ItemUpdating"
            OnModeChanged="DetailsView1_ModeChanged"
            OnModeChanging="DetailsView1_ModeChanging"
            OnPageIndexChanged="DetailsView1_PageIndexChanged"
            OnPageIndexChanging="DetailsView1_PageIndexChanging" DataKeyNames="SN" AllowPaging="True">
            <Fields>
                <asp:BoundField DataField="BookName" HeaderText="BookName" SortExpression="BookName" />
                <asp:BoundField DataField="SN" HeaderText="SN" SortExpression="SN" InsertVisible="False" ReadOnly="True" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Page_Load<br />";
        }

        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_ItemCommand<br />";
        }

        protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_ItemDeleted<br />";
        }

        protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_ItemDeleting<br />";
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_ItemInserted<br />";
        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_ItemInserting<br />";
        }

        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_ItemUpdated<br />";
        }

        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_ItemUpdating<br />";
        }

        protected void DetailsView1_ModeChanged(object sender, EventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_ModeChanged<br />";
        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_ModeChanging<br />";
        }

        protected void DetailsView1_PageIndexChanged(object sender, EventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_PageIndexChanged<br />";
        }

        protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
        {
            Label1.Text = Label1.Text + "DetailsView1_PageIndexChanging<br />";
        }
    }
}

xxxxx

********************************************************************************

執行後

Page_Load
********************************************************************************
按下「新增」後
Page_Load
DetailsView1_ItemCommand
DetailsView1_ModeChanging
DetailsView1_ModeChanged
********************************************************************************
按下「插入」後,自動回到顯示畫面
Page_Load
DetailsView1_ItemCommand
DetailsView1_ItemInserting
DetailsView1_ItemInserted
DetailsView1_ModeChanging
DetailsView1_ModeChanged
********************************************************************************
按下「新增」、「取消」後
Page_Load
DetailsView1_ItemCommand
DetailsView1_ModeChanging
DetailsView1_ModeChanged
********************************************************************************
按下「編輯」後
Page_Load
DetailsView1_ItemCommand
DetailsView1_ModeChanging
DetailsView1_ModeChanged
********************************************************************************
按下「更新」後

Page_Load
DetailsView1_ItemCommand
DetailsView1_ItemUpdating
DetailsView1_ItemUpdated
DetailsView1_ModeChanging
DetailsView1_ModeChanged
********************************************************************************
按下「編輯」、「取消」後

Page_Load
DetailsView1_ItemCommand
DetailsView1_ModeChanging
DetailsView1_ModeChanged
********************************************************************************
按下「2」後

Page_Load
DetailsView1_ItemCommand
DetailsView1_PageIndexChanging
DetailsView1_PageIndexChanged
********************************************************************************
按下「刪除」後
Page_Load
DetailsView1_ItemCommand
DetailsView1_ItemDeleting
DetailsView1_ItemDeleted
********************************************************************************

(完)

相關

[研究][C#][ASP.NET][WebForm] GridView 事件執行順序
http://shaurong.blogspot.com/2017/07/caspnetwebform-gridview.html

[研究][ASP.NET]DetailsView事件執行順序
http://shaurong.blogspot.com/2023/03/aspnetdetailsview.html


沒有留言:

張貼留言