萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP.NET 程式設計

ASP.NET ViewState 與 DataTable 實作動態資料錄入:GridView 新增、刪除、編輯完整範例

架構設計

  • 資料來源:`DataTable` 存放於 `ViewState["dt"]`,跨 PostBack 保持狀態。
  • 呈現控制項:`GridView` 設定 `ShowFooter="true"`、`AutoGenerateColumns="false"`,使用 `TemplateField` 定義編輯介面。
  • 操作模式:Footer 行作為新增列;每列內建 DropDownList、TextBox 直接編輯;LinkButton 處理新增/刪除;控制項事件 (SelectedIndexChanged/TextChanged) 即時同步回 DataTable。

前端標記 .aspx

<%@ Page Language="C#" EnableViewState="true" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ViewState + DataTable 動態錄入示範</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView_list" runat="server" ShowFooter="true" AutoGenerateColumns="false" 
            OnRowDataBound="GridView_list_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="用途">
                    <ItemTemplate>
                        <asp:DropDownList ID="x1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="x1_SelectedIndexChanged">
                            <asp:ListItem Text="L0" Value="L0" />
                            <asp:ListItem Text="L1" Value="L1" />
                            <asp:ListItem Text="L2" Value="L2" />
                        </asp:DropDownList>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="x1" runat="server">
                            <asp:ListItem Text="L0" Value="L0" />
                            <asp:ListItem Text="L1" Value="L1" />
                            <asp:ListItem Text="L2" Value="L2" />
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="鋼種">
                    <ItemTemplate>
                        <asp:DropDownList ID="x2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="x2_SelectedIndexChanged">
                            <asp:ListItem Text="S1" Value="S1" />
                            <asp:ListItem Text="S2" Value="S2" />
                        </asp:DropDownList>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="x2" runat="server">
                            <asp:ListItem Text="S1" Value="S1" />
                            <asp:ListItem Text="S2" Value="S2" />
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="鑄造噸數">
                    <ItemTemplate>
                        <asp:TextBox ID="x3" runat="server" AutoPostBack="true" OnTextChanged="x3_TextChanged" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="x3" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="操作">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkDelete" runat="server" CommandName="DeleteRow" Text="刪除" OnClientClick="return confirm('確定刪除?');" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Text="新增" />
                        <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click" Text="刪除最後一列" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            
            <EmptyDataTemplate>
                <table style="border-collapse:collapse; width:100%;" border="1">
                    <tr><th>用途</th><th>鋼種</th><th>鑄造噸數</th></tr>
                    <tr>
                        <td><asp:DropDownList ID="x1" runat="server"><asp:ListItem>L0</asp:ListItem><asp:ListItem>L1</asp:ListItem></asp:DropDownList></td>
                        <td><asp:DropDownList ID="x2" runat="server"><asp:ListItem>S1</asp:ListItem><asp:ListItem>S2</asp:ListItem></asp:DropDownList></td>
                        <td><asp:TextBox ID="x3" runat="server" /></td>
                    </tr>
                </table>
            </EmptyDataTemplate>
        </asp:GridView>
    </form>
</body>
</html>

後端程式碼 .aspx.cs (或 `

copyright © 萬盛學電腦網 all rights reserved