<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <telerik:RadCodeBlock ID="CodeBlock" runat="server">

        <script type="text/javascript">
        
            var hasChanges, inputs, dropdowns, editedRow, textareas;
    		
    	    function RowClick(sender, eventArgs)
		    {
		   
			    if(editedRow && hasChanges)
			    {
					hasChanges = false;
					if(confirm("Update changes?"))
				    {                       
					    $find("<%= RadGrid1.MasterTableView.ClientID %>").updateItem(editedRow);
				    }
			    }
		    }

		    function RowDblClick(sender, eventArgs)
		    {   
		       
			    editedRow = eventArgs.get_itemIndexHierarchical();
			    $find("<%= RadGrid1.MasterTableView.ClientID %>").editItem(editedRow);
		    }
		    
            function GridCommand(sender, args)
		    {
		       
		        if (args.get_commandName() != "Edit")
		        {
		            editedRow = null;
		        }
		    }

		    function GridCreated(sender, eventArgs)
		    {	
		          
		        var gridElement = sender.get_element();
		        var elementsToUse = [];
			    inputs = gridElement.getElementsByTagName("input");
			    for (var i = 0; i < inputs.length;i++)
			    {
			        var lowerType = inputs[i].type.toLowerCase();
			        if(lowerType == "hidden" || lowerType == "button")
			        {
                        continue;
			        }
    			    
			        Array.add(elementsToUse, inputs[i]);
				    inputs[i].onchange = TrackChanges;
			    }
    			
			    dropdowns = gridElement.getElementsByTagName("select");
			    for (var i = 0; i < dropdowns.length;i++)
			    {
				    dropdowns[i].onchange = TrackChanges;
			    }
			    
			    textareas = gridElement.getElementsByTagName("textarea");
			    for (var i = 0; i < textareas.length;i++)
			    {
				    textareas[i].onchange = TrackChanges;
			    }			    

			    //setTimeout(function(){if(elementsToUse[0])elementsToUse[0].focus();},100);
		    }

		    function TrackChanges(e)
		    {
		       
			    hasChanges = true;
		    }        
            
        
        
        </script>

    </telerik:RadCodeBlock>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                SelectCommand="SELECT [ProductID], [SupplierID], [ProductName], [UnitsInStock], [UnitPrice], [Discontinued] FROM [Products]"
                DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @original_ProductID"
                InsertCommand="INSERT INTO [Products] ([SupplierID], [ProductName], [UnitsInStock], [UnitPrice], [Discontinued]) VALUES (@SupplierID, @ProductName, @UnitsInStock, @UnitPrice, @Discontinued)"
                OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Products] SET [SupplierID] = @SupplierID, [ProductName] = @ProductName, [UnitsInStock] = @UnitsInStock, [UnitPrice] = @UnitPrice, [Discontinued] = @Discontinued WHERE [ProductID] = @original_ProductID">
                <DeleteParameters>
                    <asp:Parameter Name="original_ProductID" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="SupplierID" Type="Int32" />
                    <asp:Parameter Name="ProductName" Type="String" />
                    <asp:Parameter Name="UnitsInStock" Type="Int16" />
                    <asp:Parameter Name="UnitPrice" Type="Decimal" />
                    <asp:Parameter Name="Discontinued" Type="Boolean" />
                    <asp:Parameter Name="original_ProductID" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="SupplierID" Type="Int32" />
                    <asp:Parameter Name="ProductName" Type="String" />
                    <asp:Parameter Name="UnitsInStock" Type="Int16" />
                    <asp:Parameter Name="UnitPrice" Type="Decimal" />
                    <asp:Parameter Name="Discontinued" Type="Boolean" />
                </InsertParameters>
            </asp:SqlDataSource>
        </div>
        <telerik:RadAjaxPanel ID="panel" runat="server">
            <telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticUpdates="true" DataSourceID="SqlDataSource1"
                GridLines="None">
                <MasterTableView AutoGenerateColumns="False" EditMode="inplace" AllowPaging="true"
                    DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
                    <Columns>
                        <telerik:GridEditCommandColumn>
                        </telerik:GridEditCommandColumn>
                        <telerik:GridBoundColumn DataField="ProductID" DataType="System.Int32" HeaderText="ProductID"
                            ReadOnly="True" SortExpression="ProductID" UniqueName="ProductID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="SupplierID" DataType="System.Int32" HeaderText="SupplierID"
                            SortExpression="SupplierID" UniqueName="SupplierID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName"
                            UniqueName="ProductName">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="UnitsInStock" DataType="System.Int16" HeaderText="UnitsInStock"
                            SortExpression="UnitsInStock" UniqueName="UnitsInStock">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn HeaderText="UnitPrice">
                            <ItemTemplate>
                                <asp:Label ID="unitLabel" runat="server" Text='<%#Eval("UnitPrice") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <telerik:RadTextBox ID="textBox" runat="server" TextMode="MultiLine"  Text='<%#Bind("UnitPrice") %>'>
                                </telerik:RadTextBox>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridCheckBoxColumn DataField="Discontinued" DataType="System.Boolean" HeaderText="Discontinued"
                            SortExpression="Discontinued" UniqueName="Discontinued">
                        </telerik:GridCheckBoxColumn>
                    </Columns>
                </MasterTableView>
                <ClientSettings>
                    <Selecting AllowRowSelect="true" />
                    <ClientEvents OnRowClick="RowClick" OnRowDblClick="RowDblClick" OnGridCreated="GridCreated"
                        OnCommand="GridCommand" />
                </ClientSettings>
            </telerik:RadGrid>
        </telerik:RadAjaxPanel>
    </form>
</body>
</html>
