This is a migrated thread and some comments may be shown as answers.

[Solved] Self Referencing Grid - Add new to child disappears

10 Answers 220 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rhyss
Top achievements
Rank 1
Rhyss asked on 27 Oct 2009, 11:55 PM
I am having issues with getting the Add new record button to work in the self referencing grid.  When i click on it the whole child table i am trying to add to disappears.

i have tried to follow the advice from here but the issue remains.

My code behind is as follows:
protected void Page_Load(object sender, EventArgs e) 
        { 
            if (Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("3.5") != -1) 
            { 
                NavGrid.MasterTableView.FilterExpression = @"it[""ParentId""] = Convert.DBNull"; 
            } 
            else 
            { 
                NavGrid.MasterTableView.FilterExpression = "ParentId IS NULL"
            } 
            
        } 
 
        protected void NavGrid_ItemDeleted(object source, Telerik.Web.UI.GridDeletedEventArgs e) 
        { 
 
        } 
 
        protected void NavGrid_ItemInserted(object source, Telerik.Web.UI.GridInsertedEventArgs e) 
        { 
 
        } 
 
        protected void NavGrid_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e) 
        { 
 
        } 
 
        protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e) 
        { 
            
 
            if (e.Column.IsBoundToFieldName("Id")) 
            { 
                ((GridBoundColumn)e.Column).ReadOnly = true
            } 
        } 
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
        { 
           
        } 
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            
        } 
        public void CreateExpandCollapseButton(GridItem item, string columnUniqueName) 
        { 
          
        } 
        
 
        protected void NavGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            NavGrid.DataSource = NavDataSource
        } 
 
        protected void NavGrid_ItemCommand(object source, GridCommandEventArgs e) 
        { 
 
 
            if (e.CommandName == RadGrid.DeleteCommandName) 
            { 
                NavDataSource.Delete(); 
 
            } 
 
            if (e.CommandName == RadGrid.UpdateCommandName) 
            { 
 
                NavDataSource.Update(); 
            } 
 
            if (e.CommandName == RadGrid.PerformInsertCommandName) 
            { 
 
                NavDataSource.Insert(); 
            } 
 
          
        } 

My page is:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin_Navigations.aspx.cs" Inherits="FEP.Admin_Navigations" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"
    <title>Admin Navigations</title> 
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server"
    <script type="text/javascript"
        //This function is called from the Explorer.aspx page  
        function OnFileSelected(wnd, fileSelected) { 
            //NEW: Insert the returned value in the textbox with currentInputID  
            var textbox = $get(currentInputID); 
            textbox.value = fileSelected.substr(1); 
        } 
 
        var currentInputID = null
        function OpenFileExplorerDialog(inputID) { 
            var wnd = $find("<%= ExplorerWindow.ClientID %>"); 
            wnd.show(); 
 
            //NEW: Set the ID of the textbox in a global variable             
            currentInputID = inputID
        }  
    </script> 
</telerik:RadScriptBlock> 
</head> 
<body> 
    <form id="form1" runat="server"
<telerik:RadScriptManager ID="RadScriptManager1" runat="server"
    </telerik:RadScriptManager> 
    <div> 
     
        <telerik:RadGrid ID="NavGrid" runat="server" DataSourceID="NavDataSource"  
            ShowStatusBar="True" AllowAutomaticDeletes="True"  
            AllowAutomaticInserts="True" AllowAutomaticUpdates="True"  
            AutoGenerateColumns="False" onitemdeleted="NavGrid_ItemDeleted"  
            oniteminserted="NavGrid_ItemInserted"  
            onitemupdated="NavGrid_ItemUpdated"  
            oncolumncreated="RadGrid1_ColumnCreated" onitemcommand="NavGrid_ItemCommand"  
            onitemcreated="RadGrid1_ItemCreated" onitemdatabound="RadGrid1_ItemDataBound"  
            onneeddatasource="NavGrid_NeedDataSource"
            <MasterTableView DataSourceID="NavDataSource" TableLayout="Fixed"  
                style="width:auto;" DataKeyNames="Id, ParentId" commanditemdisplay="Top" HierarchyDefaultExpanded="true"
                <SelfHierarchySettings ParentKeyName="ParentId" KeyName="Id" /> 
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" HeaderText="Edit"
                        <HeaderStyle Width="35px" /> 
                    </telerik:GridEditCommandColumn> 
                    <telerik:GridBoundColumn DataField="Id" HeaderText="Id" UniqueName="Id" MaxLength="10" ReadOnly="True" ForceExtractValue="Always"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="ParentId" HeaderText="Parent Id" UniqueName="ParentId" MaxLength="10" ReadOnly="True" ForceExtractValue="Always"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name" MaxLength="50"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Target" HeaderText="Target" UniqueName="Target" MaxLength="50"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Control" HeaderText="Control" UniqueName="Control" MaxLength="50"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Content" HeaderText="Content" UniqueName="Content" MaxLength="50"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="ItemOrder" HeaderText="Item Order" UniqueName="ItemOrder" 
                        MaxLength="50" ConvertEmptyStringToNull="False"
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn HeaderText="Navigation Image" DataField="Image" DataType="System.String" 
                        UniqueName="NavImage"
                        <ItemTemplate> 
                            <asp:Image ID="SiteImage" Height="100px" Width="100px" AlternateText='<%#Bind("Name") %>' 
                                runat="server" ImageUrl='<%#Bind("Image") %>' /> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            <asp:TextBox runat="server" ID="fileName" Columns="50" Text='<%#Bind("Image") %>' /> 
                            <input type="button" onclick="OpenFileExplorerDialog('<%#Container.FindControl("fileName").ClientID %>'); return false;" 
                                value="Open ..." /> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridButtonColumn CommandName="Delete" ConfirmText="Are you sure you want to delete this record?" 
                        ConfirmTitle="Delete?" Text="Delete" UniqueName="DeleteColumn" ButtonType="ImageButton" 
                        HeaderText="Delete"
                        <HeaderStyle Width="50px" /> 
                    </telerik:GridButtonColumn> 
                    <telerik:GridButtonColumn CommandName="GoToNavigations" Text="Go to Navigations..."  
                        UniqueName="GoNav"
                    </telerik:GridButtonColumn> 
                    <telerik:GridButtonColumn CommandName="GoToQuestions" Text="Go to Quesitons..."  
                        UniqueName="GoQuest"
                    </telerik:GridButtonColumn> 
                    <telerik:GridButtonColumn CommandName="GoToIcons" Text="Go to Icons..."  
                        UniqueName="GoIcon"
                    </telerik:GridButtonColumn> 
                </Columns> 
                <EditFormSettings> 
                    <EditColumn UniqueName="EditCommandColumn"
                    </EditColumn> 
                </EditFormSettings> 
            </MasterTableView> 
            <ClientSettings EnablePostBackOnRowClick="False" EnableRowHoverStyle="True"  
                EnableAlternatingItems="False" AllowExpandCollapse="True"
                <Selecting AllowRowSelect="True" /> 
            </ClientSettings> 
        </telerik:RadGrid> 
     
    </div> 
    <asp:SqlDataSource runat="server" ConnectionString="<%$ ConnectionStrings:MySQL %>" 
    SelectCommand="Select Id, ParentId, Name, Target, Control, Content, Image, ItemOrder FROM Navigation" 
    ID="NavDataSource" ProviderName="<%$ ConnectionStrings:MySQL.ProviderName %>" 
    ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}" 
    InsertCommand="INSERT INTO Navigation(ParentId, Name, Target, Control, Content, Image, ItemOrder) VALUES (?ParentId, ?Name, ?Target, ?Control, ?Content, ?Image, ?ItemOrder)"  
    DeleteCommand="DELETE FROM Navigation WHERE Id = ?original_Id AND ((ParentId = ?original_ParentId) OR (ParentId is null AND ?ParentId is null)) AND Name = ?original_Name AND ((Target = ?original_Target) OR (Target is null AND ?original_Target is null)) AND Control = ?original_Control AND ((Content = ?original_Content) OR (Content is null AND ?original_Content is null)) AND ((Image = ?original_Image) OR (Image is null AND ?original_Image is null)) AND ItemOrder = ?original_ItemOrder"    
     
        UpdateCommand="UPDATE Navigation SET ParentId = ?ParentId, Name = ?Name, Target = ?Target, Control = ?control, Content = ?Content, Image = ?Image, ItemOrder = ?ItemOrder WHERE Id = ?original_Id AND ((ParentId = ?original_ParentId) OR (ParentId is null AND ?ParentId is null)) AND Name = ?original_Name AND ((Target = ?original_Target) OR (Target is null AND ?original_Target is null)) AND Control = ?original_Control AND ((Content = ?original_Content) OR (Content is null AND ?original_Content is null)) AND ((Image = ?original_Image) OR (Image is null AND ?original_Image is null)) AND ItemOrder = ?original_ItemOrder" > 
    <DeleteParameters> 
        <asp:Parameter Name="original_Id" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_ParentId" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Name" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Target" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Control" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Content" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Image" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_ItemOrder" Type="Int32" ConvertEmptyStringToNull="False" /> 
    </DeleteParameters> 
    <UpdateParameters> 
        <asp:Parameter Name="Id" Type="Int32" /> 
        <asp:Parameter Name="ParentId" Type="Int32" /> 
        <asp:Parameter Name="Name" Type="String" /> 
        <asp:Parameter Name="Target" Type="Int32" /> 
        <asp:Parameter Name="Control" Type="String" /> 
        <asp:Parameter Name="Content" Type="Int32" /> 
        <asp:Parameter Name="Image" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="ItemOrder" Type="Int32" /> 
         
        <asp:Parameter Name="original_Id" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_ParentId" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Name" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Target" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Control" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Content" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Image" Type="String" ConvertEmptyStringToNull="False" /> 
    </UpdateParameters> 
    <InsertParameters> 
        <asp:Parameter Name="ParentId" Type="Int32" /> 
        <asp:Parameter Name="Name" Type="String" /> 
        <asp:Parameter Name="Target" Type="Int32" /> 
        <asp:Parameter Name="Control" Type="String" /> 
        <asp:Parameter Name="Content" Type="Int32" /> 
        <asp:Parameter Name="Image" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="ItemOrder" Type="Int32" /> 
    </InsertParameters> 
    <SelectParameters> 
         
    </SelectParameters> 
</asp:SqlDataSource> 
 
<telerik:RadWindow runat="server" Width="530px" Height="550px" VisibleStatusbar="false" 
    NavigateUrl="Admin_ImageExplorer.aspx?PathType=Navigation" ID="ExplorerWindow" Modal="true" 
    Behaviors="Close,Move"
</telerik:RadWindow> 
 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
        <AjaxSettings> 
            <telerik:AjaxSetting AjaxControlID="NavGrid"
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="NavGrid" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
        </AjaxSettings> 
    </telerik:RadAjaxManager> 
    </form> 
</body> 
</html> 
What am i missing here?

any help is greatly appreciated.
thanks
Jordan

10 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Oct 2009, 11:09 AM
Hi,

The code sample provided by Telerik  doesnot make use of Automatic Operations in the grid. From the code snippet that you have provided you have set all Automatic Operations to True which should be avoided .You need to manually pass the parameters for the CRUD operations and rebind the grid in the Item Command.

Thanks,
Princy
0
Rhyss
Top achievements
Rank 1
answered on 28 Oct 2009, 12:13 PM
Thanks for the feedback.

i think i am getting lost as i am using the DataSource and the example was not.  Can you possibly give some example as to how this would work with the datasource?
thanks
Jordan
0
Sebastian
Telerik team
answered on 28 Oct 2009, 12:23 PM
Hello Jordan,

I think that the sample code snippets from this help article can get you up and going:
http://www.telerik.com/help/aspnet-ajax/grdinsertupdatedeleteatdatabaselevel.html

Feel free to adopt them to work for self-referencing hierarchy grid structure.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Rhyss
Top achievements
Rank 1
answered on 28 Oct 2009, 12:40 PM
Thanks again for the feedback.

however, i am working in a framework that uses the datasources as set up in the code provided.  The same process has been set up for normal grids and works normally.  The heirarchy grid behaves differently and is thus causing the issues outlined.

The link i provided from a previous post seems to be the direct way to do it, however i must be missing something, perhaps a databind when i go to Add new record.  I had difficulty converting from the example due to the use of the SelfReferencing entity for the data.

What do i need to change in that example to go from the SelfReferencing entity to the Datasource?
0
Sebastian
Telerik team
answered on 28 Oct 2009, 01:23 PM
Hello Jordan,

The general idea is to construct appropriate update/insert/delete queries to refresh the data in the SqlDataSource you use for grid item generator. This can be done intercepting the UpdateCommand/InsertCommand/DeleteCommand server events of the grid where e.Item will reference the edited/inserted/deleted grid item. Then based on the primary key of that item you need to execute the Update/Insert/Delete query to refresh the underlying data.
 
Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Rhyss
Top achievements
Rank 1
answered on 28 Oct 2009, 01:45 PM
Thanks Sebastian.  Yes, i understand the premise.  I have used the Datasource and RadGrid a number of times successfully utilising the automatic updating/insert/delete. However, the issue is with the heirarchy grid it doesn't function as it has done many times before.

What i am trying to get to is if someone could, using a similar Datasource object as i have provided, get a self-referencing grid to work with non-auto generated columns.
0
Rhyss
Top achievements
Rank 1
answered on 29 Oct 2009, 09:18 AM
Come on.  there must be someone that has the code on how to do this with a Datasource.

I keep getting an error as the parameters are always null.

anyone?
0
Accepted
Princy
Top achievements
Rank 2
answered on 29 Oct 2009, 10:13 AM
Hi Jordan ,

Since  Automatic Operations doesnot work  with Self referencing hierarchy  you need to manually pass the parameters to the SqlDataSource . You can do this the following way

C#
 
protected void NavGrid_ItemCommand(object source, GridCommandEventArgs e)    
        {    
    
             if (e.CommandName == RadGrid.PerformInsertCommandName)    
            {    
              GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;  
 
              TextBox txtbx = (TextBox)item["Name"].Controls[0];  
 
              SqlDataSource1.InsertParameters["Name"].DefaultValue   
                                             = txtbx.Text;  
              NavDataSource.Insert();    
            }    
    
             
        }    
 

Hope this helps....Do let me know if you need further assistance.

Thanks,
Princy
0
Rhyss
Top achievements
Rank 1
answered on 29 Oct 2009, 12:32 PM
BINGO!!!!  You are the MAN Princy!

That is exactly what I needed.  I had all the parameters cast in the DataSource and thought that would propogate through.  Now I know I need to manually process all the parameters I should be right (previously I thought it only necessary to obtain the ParentId from this process and manually assign it).

Many thanks.
0
Rhyss
Top achievements
Rank 1
answered on 05 Nov 2009, 01:35 AM
Sooooo close now.  The problem is that the Update now won't work.  The insert and delete works, but the update keeps on using the original values in the SqlDataSourceCommandEventArgs object in the NavDataSource_Updating function.  I use the DefaultValue to in the ItemCommand function and that is retaining the values at expected when referenced from the NavDataSource_Updating function, however I take it the Updating function uses the Values of the Parameters from the SqlDataSourceCommandEventArgs object.

What am i doing wrong here?  It is set up the same as the other Delete and Insert functions.  Here is my code:
ASPX:
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"
    <title>Admin Navigation</title> 
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server"
    <script type="text/javascript"
        //This function is called from the Explorer.aspx page  
        function OnFileSelected(wnd, fileSelected) { 
            //NEW: Insert the returned value in the textbox with currentInputID  
            var textbox = $get(currentInputID); 
            textbox.value = '~' + fileSelected; 
        } 
 
        var currentInputID = null
        function OpenFileExplorerDialog(inputID) { 
            var wnd = $find("<%= ExplorerWindow.ClientID %>"); 
            wnd.show(); 
 
            //NEW: Set the ID of the textbox in a global variable             
            currentInputID = inputID
        }  
       
    </script> 
</telerik:RadScriptBlock> 
</head> 
<body> 
    <form id="form1" runat="server"
    <asp:ScriptManager ID="ScriptManager1" runat="server"
    </asp:ScriptManager> 
    <div> 
        <telerik:RadGrid ID="NavGrid" runat="server" GridLines="None" OnNeedDataSource="NavGrid_NeedDataSource" 
            OnColumnCreated="NavGrid_ColumnCreated" AllowAutomaticDeletes="True"  
            AllowAutomaticInserts="True" AllowAutomaticUpdates="True"  
            AutoGenerateColumns="False" onitemcommand="NavGrid_ItemCommand"  
            onitemdeleted="NavGrid_ItemDeleted" oniteminserted="NavGrid_ItemInserted"  
            onitemupdated="NavGrid_ItemUpdated"  
            onitemdatabound="NavGrid_ItemDataBound" AllowMultiRowSelection="True"  
            ShowStatusBar="True"
            <MasterTableView Width="100%" CommandItemDisplay="Top" 
                DataKeyNames="Id"
                <SelfHierarchySettings ParentKeyName="ParentId" KeyName="Id" /> 
            <Columns> 
                   <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn"
                            <HeaderTemplate> 
                             <asp:CheckBox id="headerChkbox" OnCheckedChanged="ToggleSelectedState" AutoPostBack="True" runat="server"></asp:CheckBox> 
                            </HeaderTemplate> 
                            <ItemTemplate> 
                                <asp:CheckBox id="CheckBox1" OnCheckedChanged="ToggleRowSelection" AutoPostBack="True" runat="server"></asp:CheckBox> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" HeaderText="Edit"
                        <HeaderStyle Width="35px" /> 
                    </telerik:GridEditCommandColumn> 
                    <telerik:GridBoundColumn DataField="Id" HeaderText="Id" UniqueName="Id" MaxLength="10" ReadOnly="True" ForceExtractValue="Always"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="ParentId" HeaderText="Parent Id" UniqueName="ParentId" MaxLength="10" ReadOnly="True" ForceExtractValue="Always"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name" MaxLength="50"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Target" HeaderText="Target" UniqueName="Target" MaxLength="50"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Control" HeaderText="Control" UniqueName="Control" MaxLength="50"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Content" HeaderText="Content" UniqueName="Content" MaxLength="50"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="ItemOrder" HeaderText="Item Order" UniqueName="ItemOrder" 
                        MaxLength="50" ConvertEmptyStringToNull="False"
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn HeaderText="Navigation Image" DataField="Image" DataType="System.String" 
                        UniqueName="NavImage"
                        <ItemTemplate> 
                            <asp:Image ID="SiteImage" Height="100px" Width="100px" AlternateText='<%#Bind("Name") %>'  
                                runat="server" ImageUrl='<%#Bind("Image") %>' /> 
                        </ItemTemplate> 
                        <EditItemTemplate> 
                            <asp:TextBox runat="server" ID="fileName" Columns="50" Text='<%#Bind("Image") %>' /> 
                            <input type="button" onclick="OpenFileExplorerDialog('<%#Container.FindControl("fileName").ClientID %>'); return false;" 
                                value="Open ..." /> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridButtonColumn CommandName="Delete" ConfirmText="Are you sure you want to delete this record?" 
                        ConfirmTitle="Delete?" Text="Delete" UniqueName="DeleteColumn" ButtonType="ImageButton" 
                        HeaderText="Delete"
                        <HeaderStyle Width="50px" /> 
                    </telerik:GridButtonColumn> 
                    <telerik:GridButtonColumn CommandName="GoToNavigations" Text="Go to Navigations..."  
                        UniqueName="GoNav"
                    </telerik:GridButtonColumn> 
                    <telerik:GridButtonColumn CommandName="GoToQuestions" Text="Go to Quesitons..."  
                        UniqueName="GoQuest"
                    </telerik:GridButtonColumn> 
                    <telerik:GridButtonColumn CommandName="GoToIcons" Text="Go to Icons..."  
                        UniqueName="GoIcon"
                    </telerik:GridButtonColumn> 
                </Columns> 
                <EditFormSettings> 
                    <EditColumn UniqueName="EditCommandColumn"
                    </EditColumn> 
                </EditFormSettings> 
            </MasterTableView> 
            <ClientSettings EnableAlternatingItems="False"
            </ClientSettings> 
        </telerik:RadGrid> 
        
        <%--<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AutoGenerateDeleteColumn="true" 
            AutoGenerateEditColumn="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand" 
            OnColumnCreated="RadGrid1_ColumnCreated"
            <MasterTableView Width="100%" CommandItemDisplay="Top" AutoGenerateColumns="true" 
                DataKeyNames="ID"
                <SelfHierarchySettings ParentKeyName="ParentID" KeyName="ID" /> 
            </MasterTableView> 
        </telerik:RadGrid>--%> 
     
    </div> 
    <asp:SqlDataSource runat="server" ConnectionString="<%$ ConnectionStrings:MySQL %>" 
    SelectCommand="Select Id, ParentId, Name, Target, Control, Content, Image, ItemOrder FROM Navigation" 
    ID="NavDataSource" ProviderName="<%$ ConnectionStrings:MySQL.ProviderName %>" OldValuesParameterFormatString="original_{0}" 
    InsertCommand="INSERT INTO Navigation(ParentId, Name, Target, Control, Content, Image, ItemOrder) VALUES (?ParentId, ?Name, ?Target, ?Control, ?Content, ?Image, ?ItemOrder)"  
    DeleteCommand="DELETE FROM Navigation WHERE Id = ?original_Id AND ((ParentId = ?original_ParentId) OR (ParentId is null AND ?original_ParentId is null)) AND Name = ?original_Name AND ((Target = ?original_Target) OR (Target is null AND ?original_Target is null)) AND Control = ?original_Control AND ((Content = ?original_Content) OR (Content is null AND ?original_Content is null)) AND ((Image = ?original_Image) OR (Image is null AND ?original_Image is null)) AND ItemOrder = ?original_ItemOrder"    
     
         
         
         
        UpdateCommand="UPDATE Navigation SET ParentId = ?ParentId, Name = ?Name, Target = ?Target, Control = ?control, Content = ?Content, Image = ?Image, ItemOrder = ?ItemOrder WHERE Id = ?original_Id AND ((ParentId = ?original_ParentId) OR (ParentId is null AND ?original_ParentId is null)) AND Name = ?original_Name AND ((Target = ?original_Target) OR (Target is null AND ?original_Target is null)) AND Control = ?original_Control AND ((Content = ?original_Content) OR (Content is null AND ?original_Content is null)) AND ((Image = ?original_Image) OR (Image is null AND ?original_Image is null))"  
        oninserting="NavDataSource_Inserting" onupdated="NavDataSource_Updated"  
        onupdating="NavDataSource_Updating" > 
    <DeleteParameters> 
        <asp:Parameter Name="original_Id" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_ParentId" Type="Int32" ConvertEmptyStringToNull="True" /> 
        <asp:Parameter Name="original_Name" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Target" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Control" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Content" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Image" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_ItemOrder" Type="Int32" ConvertEmptyStringToNull="False" /> 
    </DeleteParameters> 
    <UpdateParameters> 
         
        <asp:Parameter Name="ParentId" Type="Int32" /> 
        <asp:Parameter Name="Name" Type="String" /> 
        <asp:Parameter Name="Target" Type="Int32" /> 
        <asp:Parameter Name="Control" Type="String" /> 
        <asp:Parameter Name="Content" Type="Int32" /> 
        <asp:Parameter Name="Image" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="ItemOrder" Type="Int32" /> 
         
        <asp:Parameter Name="original_Id" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_ParentId" Type="Int32" ConvertEmptyStringToNull="True" /> 
        <asp:Parameter Name="original_Name" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Target" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Control" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Content" Type="Int32" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_Image" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="original_ItemOrder" Type="String" ConvertEmptyStringToNull="False" /> 
    </UpdateParameters> 
    <InsertParameters> 
        <asp:Parameter Name="ParentId" Type="Int32"  ConvertEmptyStringToNull="True"/> 
        <asp:Parameter Name="Name" Type="String" /> 
        <asp:Parameter Name="Target" Type="Int32" /> 
        <asp:Parameter Name="Control" Type="String" /> 
        <asp:Parameter Name="Content" Type="Int32" /> 
        <asp:Parameter Name="Image" Type="String" ConvertEmptyStringToNull="False" /> 
        <asp:Parameter Name="ItemOrder" Type="Int32" /> 
    </InsertParameters> 
    <SelectParameters> 
         
    </SelectParameters> 
</asp:SqlDataSource> 
<telerik:RadWindow runat="server" Width="530px" Height="550px" VisibleStatusbar="false" 
    NavigateUrl="Admin_ImageExplorer.aspx?PathType=Navigation" ID="ExplorerWindow" Modal="true" 
    Behaviors="Close,Move"
</telerik:RadWindow> 
    <telerik:RadAjaxManager runat="server" ID="RAM"
        <AjaxSettings> 
            <telerik:AjaxSetting AjaxControlID="NavGrid"
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="NavGrid" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
            <telerik:AjaxSetting AjaxControlID="NavDataSource"
                <updatedcontrols> 
                    <telerik:AjaxUpdatedControl ControlID="NavGrid" /> 
                </updatedcontrols> 
            </telerik:AjaxSetting> 
        </AjaxSettings> 
    </telerik:RadAjaxManager> 
    </form> 
</body> 
</html> 

C#:
public partial class Admin_Navigations2 : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                if (Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("3.5") != -1) 
                { 
                    NavGrid.MasterTableView.FilterExpression = @"it[""ParentId""] = Convert.DBNull"; 
                } 
                else 
                { 
                    NavGrid.MasterTableView.FilterExpression = "ParentId IS NULL"
                } 
 
            } 
        } 
        #region Grid Events 
 
        /// <summary> 
        /// Lets the grid rebind to the datasource when required. 
        /// </summary> 
        /// <param name="source">The object seeking a datasource</param> 
        /// <param name="e">The arguements used to initiate the call.</param> 
        protected void NavGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
        { 
            NavGrid.DataSource = NavDataSource
        } 
 
        /// <summary> 
        /// Fired when the columns in the grid get created. 
        /// </summary> 
        /// <param name="sender">The object initiating the request.</param> 
        /// <param name="e">The column item being created.</param> 
        protected void NavGrid_ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e) 
        { 
            if (e.Column.IsBoundToFieldName("Id")) 
            { 
                ((GridBoundColumn)e.Column).ReadOnly = true
            } 
        } 
 
        /// <summary> 
        /// Gets fired after every grid item event.  Due to the self-referencing nature of the grid, all CRUD actions 
        /// need to be handled manually. 
        /// </summary> 
        /// <param name="source">The grid object being manipulated.</param> 
        /// <param name="e">The event arguements used in intiating the call.</param> 
        protected void NavGrid_ItemCommand(object source, GridCommandEventArgs e) 
        { 
             
 
            if (e.CommandName == RadGrid.DeleteCommandName) 
            { 
                 
                string originalId = DefaultValues(((GridDataItem)e.Item)["Id"].Text); 
                string originalParentId = DefaultValues(((GridDataItem)e.Item)["ParentId"].Text); 
                string originalName = DefaultValues(((GridDataItem)e.Item)["Name"].Text); 
                string originalTarget = DefaultValues(((GridDataItem)e.Item)["Target"].Text); 
                string originalControl = DefaultValues(((GridDataItem)e.Item)["Control"].Text); 
                string originalContent = DefaultValues(((GridDataItem)e.Item)["Content"].Text); 
                string originalImage = DefaultValues(((Image)((GridDataItem)e.Item)["NavImage"].Controls[1]).ImageUrl); //((GridDataItem)e.Item)["NavImage"].Text); 
                string originalItemOrder = DefaultValues(((GridDataItem)e.Item)["ItemOrder"].Text); 
 
                NavDataSource.DeleteParameters["original_Id"].DefaultValue = originalId
                NavDataSource.DeleteParameters["original_ParentId"].DefaultValue = originalParentId
                NavDataSource.DeleteParameters["original_Name"].DefaultValue = originalName
                NavDataSource.DeleteParameters["original_Target"].DefaultValue = originalTarget
                NavDataSource.DeleteParameters["original_Control"].DefaultValue = originalControl
                NavDataSource.DeleteParameters["original_Content"].DefaultValue = originalContent
                NavDataSource.DeleteParameters["original_Image"].DefaultValue = originalImage
                NavDataSource.DeleteParameters["original_ItemOrder"].DefaultValue = originalItemOrder
 
             //   NavDataSource.Delete(); 
                NavGrid.Rebind(); 
 
            } 
 
            if (e.CommandName == RadGrid.UpdateCommandName) 
            { 
                string originalId = DefaultValues(((GridEditFormItem)e.Item).SavedOldValues["Id"]); 
                string originalParentId = DefaultValues(((GridEditFormItem)e.Item).SavedOldValues["ParentId"]); 
                string parentId = ((TextBox)((GridEditFormItem)e.Item)["ParentId"].Controls[0]).Text; 
                string originalName = DefaultValues(((GridEditFormItem)e.Item).SavedOldValues["Name"]); 
                string name = ((TextBox)((GridEditFormItem)e.Item)["Name"].Controls[0]).Text; 
                string originalTarget = DefaultValues(((GridEditFormItem)e.Item).SavedOldValues["Target"]); 
                string target = ((TextBox)((GridEditFormItem)e.Item)["Target"].Controls[0]).Text; 
                string originalControl = DefaultValues(((GridEditFormItem)e.Item).SavedOldValues["Control"]); 
                string control = ((TextBox)((GridEditFormItem)e.Item)["Control"].Controls[0]).Text; 
                string originalContent = DefaultValues(((GridEditFormItem)e.Item).SavedOldValues["Content"]); 
                string content = ((TextBox)((GridEditFormItem)e.Item)["Content"].Controls[0]).Text; 
                string originalImage = DefaultValues(((GridEditFormItem)e.Item).SavedOldValues["Image"]); 
                string image = ((TextBox)((GridEditFormItem)e.Item)["NavImage"].Controls[1]).Text; 
                string originalItemOrder = DefaultValues(((GridEditFormItem)e.Item).SavedOldValues["ItemOrder"]); 
                string itemOrder = ((TextBox)((GridEditFormItem)e.Item)["ItemOrder"].Controls[0]).Text; 
                //SelfReferencing entity = FindEntityByID(((GridEditFormItem)e.Item).ParentItem.GetDataKeyValue("Id")); 
                //((GridEditFormItem)e.Item).UpdateValues(entity); 
                //shouldSubmitChanges = true
                NavDataSource.UpdateParameters["original_Id"].DefaultValue = originalId
                NavDataSource.UpdateParameters["ParentId"].DefaultValue = parentId
                NavDataSource.UpdateParameters["original_ParentId"].DefaultValue = originalParentId
                NavDataSource.UpdateParameters["Name"].DefaultValue = name
                NavDataSource.UpdateParameters["original_Name"].DefaultValue = originalName
                NavDataSource.UpdateParameters["Target"].DefaultValue = target
                NavDataSource.UpdateParameters["original_Target"].DefaultValue = originalTarget
                NavDataSource.UpdateParameters["Control"].DefaultValue = control
                NavDataSource.UpdateParameters["original_Control"].DefaultValue = originalControl
                NavDataSource.UpdateParameters["Content"].DefaultValue = content
                NavDataSource.UpdateParameters["original_Content"].DefaultValue = originalContent
                NavDataSource.UpdateParameters["Image"].DefaultValue = image
                NavDataSource.UpdateParameters["original_Image"].DefaultValue = originalImage
                NavDataSource.UpdateParameters["ItemOrder"].DefaultValue = itemOrder
                NavDataSource.UpdateParameters["original_ItemOrder"].DefaultValue = originalItemOrder
                
              //  int valret = NavDataSource.Update(); 
             //   NavGrid.EditItems[0].Edit = false
 
                NavGrid.Rebind(); 
 
         //       e.Canceled = true
            } 
 
            if (e.CommandName == RadGrid.PerformInsertCommandName) 
            { 
                string parentId = string.Empty; 
                GridDataItem gdi = (GridDataItem)e.Item.OwnerTableView.ParentItem; 
                if (gdi == null) 
                { 
                    parentId = string.Empty; 
                } 
                else 
                { 
                    parentId = gdi.OwnerTableView.DataKeyValues[gdi.ItemIndex]["Id"].ToString(); 
                } 
 
               // string parentId = ((TextBox)((GridEditFormItem)e.Item)["ParentId"].Controls[0]).Text; 
                string name = ((TextBox)((GridEditFormItem)e.Item)["Name"].Controls[0]).Text; 
                string target = ((TextBox)((GridEditFormItem)e.Item)["Target"].Controls[0]).Text; 
                string control = ((TextBox)((GridEditFormItem)e.Item)["Control"].Controls[0]).Text; 
                string content = ((TextBox)((GridEditFormItem)e.Item)["Content"].Controls[0]).Text; 
                string image = ((TextBox)((GridEditFormItem)e.Item)["NavImage"].Controls[1]).Text; 
                string itemOrder = ((TextBox)((GridEditFormItem)e.Item)["ItemOrder"].Controls[0]).Text; 
 
                NavDataSource.InsertParameters["ParentId"].DefaultValue = parentId
                NavDataSource.InsertParameters["Name"].DefaultValue = name
                NavDataSource.InsertParameters["Target"].DefaultValue = target
                NavDataSource.InsertParameters["Control"].DefaultValue = control
                NavDataSource.InsertParameters["Content"].DefaultValue = content
                NavDataSource.InsertParameters["Image"].DefaultValue = image
                NavDataSource.InsertParameters["ItemOrder"].DefaultValue = itemOrder
                 
           //     NavDataSource.Insert(); 
 
                NavGrid.Rebind(); 
 
      //          e.Item.OwnerTableView.IsItemInserted = false
      //          e.Item.OwnerTableView.Rebind(); 
            } 
            
         
        } 
 
        /// <summary> 
        /// Takes in an object value to be used as a DefaultValue in a parameter 
        /// </summary> 
        /// <param name="oValue">The object to evaluate and return the string of.</param> 
        /// <returns>Returns the string equivalent of the object passed in.</returns> 
        private string DefaultValues(object oValue) 
        { 
            if (oValue == null) 
                return string.Empty; 
            else 
            { 
                string valString = oValue.ToString(); 
                if (valString == "&nbsp;" || valString == "~/media/images/web/NoImage.jpg") 
                    return string.Empty; 
                return valString; 
            } 
        } 
 
        /// <summary> 
        /// Once an item has been updated, show the user the feedback. 
        /// Will show an error if necessary. 
        /// </summary> 
        /// <param name="source">The Grid object being manipulated.</param> 
        /// <param name="e">the item information of the update event and item.</param> 
        protected void NavGrid_ItemUpdated(object source, GridUpdatedEventArgs e) 
        { 
            RadGrid rg = (RadGrid)source; 
            GridEditableItem gei = (GridEditableItem)e.Item; 
            string sId = gei.GetDataKeyValue("Id").ToString(); 
 
            if (e.Exception != null) 
            { 
                e.KeepInEditMode = true
                e.ExceptionHandled = true
                rg.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", e.Exception.Message))); 
            } 
            else 
            { 
            //    e.KeepInEditMode = false
                if (e.AffectedRows == 0) 
                { 
                    rg.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", sId + " : You have attempted to update a record that was simultaneously updated by another user. Please review the other user's changes and then redo your update."))); 
                } 
                else 
                { 
                    rg.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", sId + " : UPDATED!"))); 
                    NavGrid.Rebind(); 
 
                } 
            } 
        } 
 
        
 
        /// <summary> 
        /// Once an item has been added, show the user the feedback. 
        /// Will show an error if necessary. 
        /// </summary> 
        /// <param name="source">The Grid object being manipulated.</param> 
        /// <param name="e">the item information of the insert event and item.</param> 
        protected void NavGrid_ItemInserted(object source, GridInsertedEventArgs e) 
        { 
            RadGrid rg = (RadGrid)source; 
            if (e.Exception != null) 
            { 
                e.KeepInInsertMode = true
                e.ExceptionHandled = true
                rg.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", e.Exception.Message))); 
            } 
            else 
            { 
                rg.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", "New record : INSERTED!"))); 
            } 
        } 
 
        /// <summary> 
        /// Once an item is deleted, show the user the feedback. 
        /// Will show the error if necessary. 
        /// </summary> 
        /// <param name="source">The Grid object being manipulated.</param> 
        /// <param name="e">the item information of the delete event and item.</param> 
        protected void NavGrid_ItemDeleted(object source, GridDeletedEventArgs e) 
        { 
            RadGrid rg = (RadGrid)source; 
            GridEditableItem gei = (GridEditableItem)e.Item; 
            string sId = gei.GetDataKeyValue("Id").ToString(); 
 
            if (e.Exception != null) 
            { 
                e.ExceptionHandled = true
                rg.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", e.Exception.Message))); 
            } 
            else 
            { 
                if (e.AffectedRows == 0) 
                { 
                    rg.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", sId + " : You have attempted to delete a record that was simultaneously updated by another user. Please review the other user's changes and then redo your delete."))); 
                } 
                else 
                { 
                    rg.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", sId + " : DELETED!"))); 
                } 
            } 
        } 
        #endregion 
 
        protected void NavGrid_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            GridItem gi = e.Item; 
            if (gi.ItemType == GridItemType.Item || gi.ItemType == GridItemType.AlternatingItem) 
            { 
                Image img = (Image)gi.FindControl("SiteImage"); 
                if (img != null) 
                { 
                    if (img.ImageUrl == null || img.ImageUrl == string.Empty) 
                    { 
                        img.ImageUrl = "~/media/images/web/NoImage.jpg"
                    } 
                } 
            } 
        } 
 
 
        protected void ToggleRowSelection(object sender, EventArgs e) 
        { 
            GridItem ogi = (GridItem)(((CheckBox)sender).Parent.Parent); 
             
            ogi.Selected = (sender as CheckBox).Checked; 
 
            if (ogi.OwnerTableView.ParentItem != null) 
            { 
                ogi.OwnerTableView.ParentItem.Selected = true
                (ogi.OwnerTableView.ParentItem.FindControl("CheckBox1") as CheckBox).Checked = true
            } 
 
        } 
 
        protected void ToggleSelectedState(object sender, EventArgs e) 
        { 
            if ((sender as CheckBox).Checked) 
            { 
                foreach (GridDataItem dataItem in NavGrid.Items) 
                { 
                    (dataItem.FindControl("CheckBox1") as CheckBox).Checked = true
                    dataItem.Selected = true
                } 
            } 
            else 
            { 
                foreach (GridDataItem dataItem in NavGrid.Items) 
                { 
                    (dataItem.FindControl("CheckBox1") as CheckBox).Checked = false
                    dataItem.Selected = false
                } 
            } 
        } 
 
        protected void NavDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e) 
        { 
 
        } 
 
        protected void NavDataSource_Updated(object sender, SqlDataSourceStatusEventArgs e) 
        { 
 
        } 
 
        protected void NavDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e) 
        { 
 
        } 
    } 

Tags
Grid
Asked by
Rhyss
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Rhyss
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or