<%@ 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_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"
            UpdateCommand="UPDATE Products
                    Set [ProductName] = @NomeProduto, [Discontinued] = @Esgotado 
                    WHERE [ProductID] = @ProductID">
            <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" 
	DataKeyNames="ProductID" OnItemUpdating="ListView1_ItemUpdating" 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>                    
                    <td>
                        <asp:LinkButton ID="EditButton" runat="Server" Text="Editar" CommandName="Edit" />
                    </td>
                </tr>
            </ItemTemplate>           
            <EditItemTemplate>
                <tr>
                    <td valign="top">
                        <asp:Label runat="server" ID="ProductNameLabel1" AssociatedControlID="ProductNameTextBox1"
                            Text="Nome" />
                        <asp:TextBox ID="ProductNameTextBox1" runat="server" Text='<%#Bind("ProductName") %>' /><br />
                        <asp:Label runat="server" ID="DiscontinuedLabel1" AssociatedControlID="DiscontinuedCheckBox1"
                            Text="Esgotado" />
                        <asp:CheckBox ID="DiscontinuedCheckBox1" runat="server" 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>   
            <EmptyDataTemplate>
                <table class="emptyTable" cellpadding="5" cellspacing="5">
                    <tr>
                        <td>
                            Nenhum registro disponível.
                        </td>
                    </tr>
                </table>
            </EmptyDataTemplate>
        </asp:ListView>
    </div>
    </form>
</body>
</html>
