<%@ Page Language="C#" Debug="true" MaintainScrollPositionOnPostback="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        string produto = ((TextBox)e.Item.FindControl("ProductNameTextBox")).Text;
        bool chk = ((CheckBox)e.Item.FindControl("DiscontinuedCheckBox")).Checked;

        if (!string.IsNullOrEmpty(produto))
        {
            SqlDataSource1.InsertParameters["NomeProduto"].DefaultValue = produto;
            SqlDataSource1.InsertParameters["Esgotado"].DefaultValue = chk.ToString();
        }
        else
        {
            e.Cancel = true;
        }
    }

    protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.NewValues[0].ToString().Trim()))
        {
            SqlDataSource1.UpdateParameters["NomeProduto"].DefaultValue = e.NewValues[0].ToString();
            SqlDataSource1.UpdateParameters["Esgotado"].DefaultValue = e.NewValues[1].ToString();
        }
        else
        {
            e.Cancel = true;
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ProviderName="System.Data.SqlClient"
            DataSourceMode="DataSet" ConnectionString="<%$ ConnectionStrings:nwind%>" SelectCommand="SELECT ProductID, ProductName, UnitPrice, Discontinued FROM Products"
            InsertCommand="Insert into Products(ProductName, Discontinued)values(@NomeProduto, @Esgotado)"
            UpdateCommand="UPDATE Products
                    Set [ProductName] = @NomeProduto, [Discontinued] = @Esgotado 
                    WHERE [ProductID] = @ProductID"
            DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID">
            <InsertParameters>
                <asp:Parameter Name="NomeProduto" Direction="Input" Type="String" />
                <asp:Parameter Name="Esgotado" Type="Boolean" DefaultValue="false" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="NomeProduto" Direction="Input" Type="String" />
                <asp:Parameter Name="Esgotado" Type="Boolean" DefaultValue="false" />
            </UpdateParameters>
        </asp:SqlDataSource>

        <asp:ListView runat="server" ID="ListView1" DataSourceID="SqlDataSource1" GroupItemCount="3"
            GroupPlaceholderID="groupPlaceholder" ItemPlaceholderID="itemPlaceholder1" InsertItemPosition="LastItem"
            DataKeyNames="ProductID" OnItemInserting="ListView1_ItemInserting" OnItemUpdating="ListView1_ItemUpdating" ConvertEmptyStringToNull="true">
            <LayoutTemplate>
                <asp:LinkButton runat="server" ID="SortButton" Text="Ordenar (a-z; z-a)" CommandName="Sort"
                    CommandArgument="ProductName" />
                <table cellpadding="2" width="640px" runat="server" id="tblProducts">
                    <tr id="Tr2" runat="server" class="header">
                        <th id="Th1" runat="server" colspan="3">
                            LISTA DE PRODUTOS
                        </th>
                    </tr>
                    <tr runat="server" id="groupPlaceholder" />
                </table>
                <asp:DataPager runat="server" ID="DataPager" PageSize="5">
                    <Fields>
                        <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true"
                            FirstPageText="|<< " LastPageText=" >>|" NextPageText=" > " PreviousPageText=" < " />
                    </Fields>
                </asp:DataPager>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <tr id="Tr1" runat="server" style="background-color: #90EE90;">
                    <td>
                        <asp:Label runat="server" ForeColor="white" ID="NameLabel1" Text='<%#Eval("ProductName") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
            <GroupTemplate>
                <tr runat="server" id="ProductsRow">
                    <td runat="server" id="itemPlaceholder1" />
                </tr>
            </GroupTemplate>
            <GroupSeparatorTemplate>
                <tr id="Tr3" runat="server">
                    <td colspan="3">
                        <hr />
                    </td>
                </tr>
            </GroupSeparatorTemplate>
            <ItemTemplate>
                <tr runat="server">
                    <td id="Td1" runat="server">
                        <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("ProductName") %>' /><br />
                    </td>
                    <td id="Td3" runat="server">
                        <asp:CheckBox ID="DiscontinuedCheckBox" runat="server" Texto="Esgotado" Checked='<%#Eval("Discontinued") %>' />
                    </td>
                    <td style="width: 40px">
                        <asp:LinkButton runat="server" ID="SelectCategoryButton" Text="Selecione" CommandName="Select" />
                    </td>
                    <td>
                        <asp:LinkButton ID="EditButton" runat="Server" Text="Editar" CommandName="Edit" />
                    </td>
                    <td>
                        <asp:LinkButton ID="DeleteButton" runat="Server" Text="Excluir" CommandName="Delete" />
                    </td>
                </tr>
            </ItemTemplate>
            <InsertItemTemplate>
                <tr style="background-color: #D3D3D3">
                    <td valign="top">
                        <asp:Label runat="server" ID="ProductNameLabel" AssociatedControlID="ProductNameTextBox"
                            Text="Nome do produto" />
                        <asp:TextBox ID="ProductNameTextBox" runat="server" Text='<%#Bind("ProductName") %>' /><br />
                        <asp:Label runat="server" ID="DiscontinuedLabel" AssociatedControlID="DiscontinuedCheckBox"
                            Text="Esgotado" />
                        <asp:CheckBox ID="DiscontinuedCheckBox" runat="server" Texto="Esgotado" Checked='<%#Bind("Discontinued") %>' />
                    </td>
                    <td>
                        <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Inserir" />
                    </td>
                </tr>
            </InsertItemTemplate>
            <EditItemTemplate>
                <tr>
                    <td valign="top">
                        <asp:Label runat="server" ID="ProductNameLabel1" AssociatedControlID="ProductNameTextBox1"
                            Text="First Name" />
                        <asp:TextBox ID="ProductNameTextBox1" runat="server" Text='<%#Bind("ProductName") %>' /><br />
                        <asp:Label runat="server" ID="DiscontinuedLabel1" AssociatedControlID="DiscontinuedCheckBox1"
                            Text="Last Name" />
                        <asp:CheckBox ID="DiscontinuedCheckBox1" runat="server" Texto="Esgotado" Checked='<%#Bind("Discontinued") %>' />
                    </td>
                    <td>
                        <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Atualizar" />
                        <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancelar" />
                    </td>
                </tr>
            </EditItemTemplate>
            <EmptyItemTemplate>
                <td id="Td2" runat="server">
                    ***
                </td>
            </EmptyItemTemplate>
            <EmptyDataTemplate>
                <table class="emptyTable" cellpadding="5" cellspacing="5">
                    <tr>
                        <td>
                            Nenhum registro disponível.
                        </td>
                    </tr>
                </table>
            </EmptyDataTemplate>
        </asp:ListView>
    </div>
    </form>
</body>
</html>
