<%@ 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;
        }
    }
    
</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)">
            <InsertParameters>
                <asp:Parameter Name="NomeProduto" Direction="Input" Type="String" />
                <asp:Parameter Name="Esgotado" Type="Boolean" DefaultValue="false" />
            </InsertParameters>
        </asp:SqlDataSource>

        <asp:ListView runat="server" ID="ListView1" DataSourceID="SqlDataSource1" InsertItemPosition="LastItem"
         OnItemInserting="ListView1_ItemInserting" ConvertEmptyStringToNull="true">
            <LayoutTemplate>
                <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="itemPlaceholder" />
                </table>
                <asp:DataPager runat="server" ID="DataPager" PageSize="5">
                    <Fields>
                        <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true"
                            FirstPageText="|<< " LastPageText=" >>|" NextPageText=" > " PreviousPageText=" < " />
                    </Fields>
                </asp:DataPager>
            </LayoutTemplate>
            
            <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>                    
                </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" Checked='<%#Bind("Discontinued") %>' />
                    </td>
                    <td>
                        <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Inserir" />
                    </td>
                </tr>
            </InsertItemTemplate>            
            <EmptyDataTemplate>
                <table class="emptyTable" cellpadding="5" cellspacing="5">
                    <tr>
                        <td>
                            Nenhum registro disponível.
                        </td>
                    </tr>
                </table>
            </EmptyDataTemplate>
        </asp:ListView>
    </div>
    </form>
</body>
</html>
