locked
想請問,在DetailView要如何"取消" RRS feed

  • 問題

  •  您好:

    請問 在以下範例中,我於EMPTYDATATEMPLATE中加入DETAILSVIEW,要來做ˋ新增的動作,但

    進入EMPTYDATATEMPLATE後,無法做取消的動作,請問該如何做取消讓他回到 上一層的GridView呢?

    另外,想請問,在.aspx中,要如何對<apsx:....> 做註解呢?

    <!--  -->過不了...

    謝謝!

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default_book_4_GridView_Add.aspx.vb" Inherits="Default_book_4_GridView_Add" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>自己寫的 GridView「資料新增」功能</title>
        <style type="text/css">
            .style1
            {
                color: #99CCFF;
                font-weight: bold;
                background-color: #000066;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <p class="style1">自己寫的 GridView「資料新增」功能
            </p>
        <p>
            <br />
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF"
                BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="id"
                DataSourceID="SqlDataSource1" Font-Size="Small" GridLines="Horizontal"
                PageSize="5">
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <Columns>
                    <asp:CommandField ButtonType="Button" ShowDeleteButton="True"
                        ShowEditButton="True" ShowSelectButton="True" />
                    <asp:ButtonField CommandName="Insert" Text="新增 Insert" ButtonType="Button"
                        HeaderText="自己寫的新增功能" />
                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
                        ReadOnly="True" SortExpression="id" />
                    <asp:BoundField DataField="test_time" DataFormatString="{0:d}"
                        HeaderText="test_time" SortExpression="test_time" />
                    <asp:BoundField DataField="class" HeaderText="class" SortExpression="class" />
                    <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
                    <asp:BoundField DataField="summary" HeaderText="summary"
                        SortExpression="summary" />
                    <asp:BoundField DataField="article" HeaderText="article"
                        SortExpression="article" />
                    <asp:BoundField DataField="author" HeaderText="author"
                        SortExpression="author" />
                </Columns>
                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Center"
                    Font-Bold="True" Font-Size="Medium" />
                <EmptyDataTemplate>
                    歡迎您進入「新增(Insert)」模式:<br />
                    <br />
                    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
                        CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource1"
                        DefaultMode="Insert" ForeColor="#333333" GridLines="None" Height="50px"
                        Width="125px">
                        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
                        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                        <FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
                        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                        <Fields>
                            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
                                ReadOnly="True" SortExpression="id" />
                            <asp:BoundField DataField="test_time" HeaderText="test_time"
                                SortExpression="test_time" />
                            <asp:BoundField DataField="class" HeaderText="class" SortExpression="class" />
                            <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
                            <asp:BoundField DataField="summary" HeaderText="summary"
                                SortExpression="summary" />
                            <asp:BoundField DataField="article" HeaderText="article"
                                SortExpression="article" />
                            <asp:BoundField DataField="author" HeaderText="author"
                                SortExpression="author" />
                            <asp:BoundField DataField="hit_no" HeaderText="hit_no"
                                SortExpression="hit_no" />
                            <asp:BoundField DataField="get_no" HeaderText="get_no"
                                SortExpression="get_no" />
                            <asp:BoundField DataField="email_no" HeaderText="email_no"
                                SortExpression="email_no" />
                            <asp:BoundField DataField="approved" HeaderText="approved"
                                SortExpression="approved" />
                            <asp:CommandField ShowInsertButton="True" />
                        </Fields>
                        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:DetailsView>
                </EmptyDataTemplate>
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                <AlternatingRowStyle BackColor="#F7F7F7" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                DeleteCommand="DELETE FROM [test] WHERE [id] = @id"
                InsertCommand="INSERT INTO [test] ([test_time], [class], [title], [summary], [article], [author], [hit_no], [get_no], [email_no], [approved]) VALUES (@test_time, @class, @title, @summary, @article, @author, @hit_no, @get_no, @email_no, @approved)"
                SelectCommand="SELECT * FROM [test]"
                UpdateCommand="UPDATE [test] SET [test_time] = @test_time, [class] = @class, [title] = @title, [summary] = @summary, [article] = @article, [author] = @author, [hit_no] = @hit_no, [get_no] = @get_no, [email_no] = @email_no, [approved] = @approved WHERE [id] = @id">
                <DeleteParameters>
                    <asp:Parameter Name="id" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="test_time" Type="DateTime" />
                    <asp:Parameter Name="class" Type="String" />
                    <asp:Parameter Name="title" Type="String" />
                    <asp:Parameter Name="summary" Type="String" />
                    <asp:Parameter Name="article" Type="String" />
                    <asp:Parameter Name="author" Type="String" />
                    <asp:Parameter Name="hit_no" Type="Int32" />
                    <asp:Parameter Name="get_no" Type="Int32" />
                    <asp:Parameter Name="email_no" Type="Int32" />
                    <asp:Parameter Name="approved" Type="String" />
                    <asp:Parameter Name="id" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="test_time" Type="DateTime" />
                    <asp:Parameter Name="class" Type="String" />
                    <asp:Parameter Name="title" Type="String" />
                    <asp:Parameter Name="summary" Type="String" />
                    <asp:Parameter Name="article" Type="String" />
                    <asp:Parameter Name="author" Type="String" />
                    <asp:Parameter Name="hit_no" Type="Int32" />
                    <asp:Parameter Name="get_no" Type="Int32" />
                    <asp:Parameter Name="email_no" Type="Int32" />
                    <asp:Parameter Name="approved" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
        </p>
        <div>
    
        </div>
        </form>
    </body>
    </html>
    

    Partial Class Default_book_4_GridView_Add
        Inherits System.Web.UI.Page
    
        Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
    
            If e.CommandName = "Insert" Then
                GridView1.DataSourceID = Nothing
                
            End If
        End Sub
    
    
        Protected Sub SqlDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Inserted
    
            GridView1.DataSourceID = "SqlDataSource1"
        End Sub
    End Class
    
    


    2013年3月17日 上午 11:28

所有回覆

  •     Protected Sub DetailsView1_ItemCommand(sender As Object, e As DetailsViewCommandEventArgs)
            If e.CommandName = "Cancel" Then
                'sender.ChangeMode(DetailsViewMode.ReadOnly)
                'sender.DefaultMode = DetailsViewMode.Edit        '其實好像此行也不需要 
                GridView1.DataSourceID = "SqlDataSource1"       '我以此行,就回到GriedView1 了
            End If
        End Sub

    只要將 GridView1 的資料重新連結回來即可

    • 已編輯 Nick_O3 2015年9月25日 上午 03:14
    2015年9月25日 上午 03:14