<%@ Page Language="C#" ValidateRequest="false"%>
<%@ Import Namespace="System.Data"%>
<script runat="server">
	protected void DataList1_EditCommand(object source,DataListCommandEventArgs e) {
		DataList1.EditItemIndex = e.Item.ItemIndex;
		DataList1.DataBind();
	}
	protected void DataList1_CancelCommand(object source,DataListCommandEventArgs e) {
		DataList1.EditItemIndex = -1;
		DataList1.DataBind();
	}
	protected void DataList1_UpdateCommand(object source,DataListCommandEventArgs e) {
		string IDcategoria = DataList1.DataKeys[e.Item.ItemIndex].ToString();
		string categoria = ((TextBox)e.Item.FindControl("txtCategoryName")).Text;
		string descricao = ((TextBox)e.Item.FindControl("txtDescription")).Text;
		
		if (!string.IsNullOrEmpty(categoria) && !string.IsNullOrEmpty(descricao)) {
			SqlDataSource1.UpdateParameters["IDcategoria"].DefaultValue = IDcategoria;
			SqlDataSource1.UpdateParameters["nomeCategoria"].DefaultValue = categoria;
			SqlDataSource1.UpdateParameters["descricao"].DefaultValue = descricao;
			int i=SqlDataSource1.Update();
		}
			DataList1.EditItemIndex = -1;
			DataList1.DataBind();
	}

</script>
<html>
	<head id="Head1" runat="server">
		<title>Exemplo com o controle DataList</title>
	</head>
	<body>
		<form id="form1" runat="server">
			<div>
				<asp:SqlDataSource
					id="SqlDataSource1"
					runat="server" ProviderName="System.Data.SqlClient"
					ConnectionString="<%$ ConnectionStrings:nwind%>"
					SelectCommand="SELECT CategoryID, CategoryName, Description FROM Categories"
					UpdateCommand="UPDATE [Categories] SET [CategoryName] = @nomeCategoria,
					[Description] = @descricao WHERE [CategoryID] = @IDcategoria">
					<UpdateParameters>
						<asp:Parameter Name="nomeCategoria" Type="String" />
					<asp:Parameter Name="descricao" Type="String" />
						<asp:Parameter Name="IDcategoria" Type="Int32" />
					</UpdateParameters>
				</asp:SqlDataSource>
				<asp:DataList ID="DataList1" runat="server"
					DataKeyField="CategoryID"
					DataSourceID="SqlDataSource1"
					OnEditCommand="DataList1_EditCommand"
					OnCancelCommand="DataList1_CancelCommand"
					OnUpdateCommand="DataList1_UpdateCommand">
					<ItemStyle Font-Names="Verdana" Font-Size="X-Small"/>
					<ItemTemplate>
						<%# ((DataRowView)Container.DataItem)["CategoryID"]%> - 
						<%# ((DataRowView)Container.DataItem)["CategoryName"]%>
						<br/><%# ((DataRowView)Container.DataItem)["Description"]%>
						<asp:Button id="btnSelect" Text="Editar" CommandName="Edit" runat="server"/>
					</ItemTemplate>
					<EditItemTemplate>
						<%# ((DataRowView)Container.DataItem)["CategoryID"]%> - 
						<asp:TextBox ID="txtCategoryName" runat="server" 
							Text='<%# ((DataRowView)Container.DataItem)["CategoryName"]%>' />
						<br/><asp:TextBox ID="txtDescription" runat="server" 
							Text='<%# ((DataRowView)Container.DataItem)["Description"]%>'/>
						<asp:Button id="btnUpdate" Text="Atualizar" CommandName="update"
						runat="server"/>
						<asp:Button id="btnCancel" Text="Cancelar" CommandName="cancel"
						runat="server"/>
					</EditItemTemplate>
					<SeparatorTemplate>
						<br/>
					</SeparatorTemplate>
				</asp:DataList>
			</div>
		</form>
	</body>
</html>

