Blog Content

    티스토리 뷰

    49.C# ASP.NET - FormView [DataControl 2]

    반응형
    FormView 컨트롤을 이용한 예제입니다.

    FormView
    사용자 정의 템플릿을 사용하여 데이터 소스의 단일 레코드 값을 표시한다.
    FormView 컨트롤을 사용하면 레코드를 편집, 삭제 및 삽입할 수 있다.

    데이터베이스가 연결된 프로젝트에서 새 웹폼을 추가합니다. [ 데이터베이스연결된 프로젝트 ]
    새 항목 추가 - WebForm 추가 ( FrmFormView.aspx )
    도구 - 데이터 - FormView 컨트롤 추가 및 직접 입력
    FrmFormView.aspx

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head id="Head1" runat="server">

        <title></title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

            폼뷰 컨트롤을 사용해서 데이터 입력<br /><br />

           

            <asp:FormView ID="FormView1" runat="server" DataKeyNames="Num"

                DataSourceID="sdsMemoWrite" DefaultMode="Insert">

                <EditItemTemplate>

                    Num:

                    <asp:Label ID="NumLabel1" runat="server" Text='<%# Eval("Num") %>' />

                    <br />

                    Name:

                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />

                    <br />

                    Email:

                    <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />

                    <br />

                    PostIP:

                    <asp:TextBox ID="PostIPTextBox" runat="server" Text='<%# Bind("PostIP")%>' />

                    <br />

                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"

                        CommandName="Update" Text="업데이트" />

                    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"

                        CausesValidation="False" CommandName="Cancel" Text="취소" />

                </EditItemTemplate>

                <InsertItemTemplate>

                    Name:

                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />

                    <br />

                    Email:

                    <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />

                    <br />

                    PostIP:

                    <asp:TextBox ID="PostIPTextBox" runat="server" Text='<%# Bind("PostIP")%>' />

                    <br />

                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"

                        CommandName="Insert" Text="삽입" />

                    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"

                        CausesValidation="False" CommandName="Cancel" Text="취소" />

                </InsertItemTemplate>

                <ItemTemplate>

                    Num:

                    <asp:Label ID="NumLabel" runat="server" Text='<%# Eval("Num") %>' />

                    <br />

                    Name:

                    <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>' />

                    <br />

                    Email:

                    <asp:Label ID="EmailLabel" runat="server" Text='<%# Bind("Email") %>' />

                    <br />

                    PostIP:

                    <asp:Label ID="PostIPLabel" runat="server" Text='<%# Bind("PostIP") %>' />

                    <br />

                    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"

                        CommandName="Edit" Text="편집" />

                    &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"

                        CommandName="Delete" Text="삭제" />

                    &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"

                        CommandName="New" Text="새로 만들기" />

                </ItemTemplate>

            </asp:FormView>

           

           

            <asp:SqlDataSource ID="sdsMemoWrite" runat="server"

                ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

               

                DeleteCommand="DELETE FROM [Memos] WHERE [Num] = @Num"

               

                InsertCommand="INSERT INTO [Memos] ([Name], [Email], [PostIP])

                                     VALUES (@Name, @Email, @PostIP)"

                                    

                SelectCommand="SELECT * FROM [Memos]"

               

                UpdateCommand="UPDATE [Memos] SET

                [Name] = @Name, [Email] = @Email, [PostIP] = @PostIP WHERE [Num] = @Num">

               

                <DeleteParameters>

                    <asp:Parameter Name="Num" Type="Int32" />

                </DeleteParameters>

               

                <UpdateParameters>

                    <asp:Parameter Name="Name" Type="String" />

                    <asp:Parameter Name="Email" Type="String" />

                    <asp:Parameter Name="PostIP" Type="String" />

                    <asp:Parameter Name="Num" Type="Int32" />

                </UpdateParameters>

               

                <InsertParameters>

                    <asp:Parameter Name="Name" Type="String" />

                    <asp:Parameter Name="Email" Type="String" />

                    <asp:Parameter Name="PostIP" Type="String" />

                </InsertParameters>

            </asp:SqlDataSource>       

        </div>

        </form>

    </body>

    </html>


    결과확인

    [그림49-1]

    아무리 많은 필드를 가진 테이블에 대해서도 입출력 기능을 1~2분내로 만들 수 있습니다.
    하지만, 현업에서는 거의(전부...) 사용하지 않습니다.
    테스트 용도로만 사용하세요.


    반응형

    Comments