Edit always ends up on last page

2 posts, 1 answers
  1. eboen
    eboen avatar
    9 posts
    Member since:
    Oct 2007

    Posted 28 Dec 2008 Link to this post

    I have a RadGrid issue. My grid binds data via ObjectDatasource and my inserts, updates and deletes are happening in my codebehind.  Editing and inserts are contained the the Grids FormTemplate.  I use ItemDataBound event to preload two dropdownlists in the formtemplate.  When a user clicks "Add a record" the formtempate displays on the first page first row, this is fine.  When the user clicks on a record to edit, the formtemplate displays on the last page of the grid at the top.  Example:  pagesize is set to 25 and we have 70 records and you click on edit on the 4 record on the 1st page, the formtemplate will display on the 3rd page at the top.  The grid works as far as insert, editing and deleting just can't figure out why the formtemplate doesn't display where the row was originally. Any suggestions would be great. I'm including code, I stripped out most everything that isn't "relevant", to keep it brief.

    .aspx page

    <telerik:RadGrid ID="RadGrid1" runat="server" Width="900px" 
                DataSourceID="ObjectDataSource1" GridLines="Vertical" AllowPaging="True" AllowSorting="True" 
                AutoGenerateColumns="False" PageSize="25"   
                OnItemDataBound="RadGrid1_ItemDataBound"   
                onitemcommand="RadGrid1_ItemCommand" AllowFilteringByColumn="True" OnInit="RadGrid1_Init">  
                <MasterTableView DataKeyNames="PublicVideoID" DataSourceID="ObjectDataSource1" CommandItemDisplay="Top" > 
                    <CommandItemTemplate> 
                    <div> 
                            <asp:LinkButton ID="btnInsert" runat="server" CommandName="InitInsert" Visible='<%# !RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="../images/icons/newRecord.gif" /> Add New Video</asp:LinkButton>&nbsp;  
                            <asp:LinkButton ID="btnCancel" runat="server" CommandName="CancelAll" CausesValidation="false" Visible='<%# RadGrid1.EditIndexes.Count > 0 || RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="../images/icons/cancel.gif" /> Cancel Editing</asp:LinkButton>&nbsp;  
                            <asp:LinkButton ID="btnRebind" runat="server" CommandName="RebindGrid" Visible='<%# RadGrid1.EditIndexes.Count < 0 || !RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="../images/icons/refresh.gif" /> Refresh Video List</asp:LinkButton> 
                        </div> 
                    </CommandItemTemplate> 
                    <RowIndicatorColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                    </RowIndicatorColumn> 
                    <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                    </ExpandCollapseColumn> 
                    <Columns> 
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" 
                            EditImageUrl="../images/icons/editrecord.gif" > 
                            <HeaderStyle Width="30px" /> 
                        </telerik:GridEditCommandColumn> 
                        <telerik:GridBoundColumn DataField="PublicVideoID" DataType="System.Int32" HeaderText="ID" 
                            ReadOnly="True" SortExpression="PublicVideoID" UniqueName="PublicVideoID"   
                            Display="true" AllowFiltering="False">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="VideoID" DataType="System.Int32" HeaderText="VideoID" 
                            SortExpression="VideoID" UniqueName="VideoID" Display="false" > 
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" 
                            UniqueName="FirstName" AllowFiltering="False">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name" SortExpression="LastName" 
                            UniqueName="LastName" > 
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="BusinessName" HeaderText="Organization" SortExpression="BusinessName" 
                            UniqueName="BusinessName">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Name" HeaderText="Public Story Title" SortExpression="Name" 
                            UniqueName="Name" AllowFiltering="False">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Description"   
                            HeaderText="Public Director’s Notes" SortExpression="Description" 
                            UniqueName="Description" Display="false" AllowFiltering="False">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="FileName" HeaderText="FileName" SortExpression="FileName" 
                            UniqueName="FileName" Display="false" > 
                        </telerik:GridBoundColumn> 
                        <telerik:GridCheckBoxColumn DataField="RemoteVideo" DataType="System.Boolean" HeaderText="RemoteVideo" 
                            SortExpression="RemoteVideo" UniqueName="RemoteVideo" Display="false">  
                        </telerik:GridCheckBoxColumn> 
                        <telerik:GridBoundColumn DataField="FileSize" DataType="System.Int32" HeaderText="FileSize" 
                            SortExpression="FileSize" UniqueName="FileSize" Display="false">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Duration" HeaderText="Duration" SortExpression="Duration" 
                            UniqueName="Duration" Display="false" Groupable="False">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="ThumbnailName" HeaderText="ThumbnailName" Display="false" 
                            SortExpression="ThumbnailName" UniqueName="ThumbnailName" > 
                        </telerik:GridBoundColumn> 
                        <telerik:GridCheckBoxColumn DataField="Featured" DataType="System.Boolean" HeaderText="Featured" 
                            SortExpression="Featured" UniqueName="Featured">  
                        </telerik:GridCheckBoxColumn> 
                        <telerik:GridBoundColumn DataField="ReleaseDate" HeaderText="ReleaseDate" SortExpression="ReleaseDate" 
                            UniqueName="ReleaseDate" Display="false">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridCheckBoxColumn DataField="Approved" DataType="System.Boolean" HeaderText="Approved" 
                            SortExpression="Approved" UniqueName="Approved">  
                        </telerik:GridCheckBoxColumn> 
                        <telerik:GridBoundColumn DataField="Created" DataType="System.DateTime" HeaderText="Created" 
                            SortExpression="Created" UniqueName="Created" Display="false" > 
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Category" HeaderText="Category" SortExpression="Category" 
                            UniqueName="Category">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="Keywords" HeaderText="Keywords" SortExpression="Keywords" 
                            UniqueName="Keywords" Display="false">  
                        </telerik:GridBoundColumn> 
                        <telerik:GridCheckBoxColumn DataField="Favorite" DataType="System.Boolean" HeaderText="Favorite" 
                            SortExpression="Favorite" UniqueName="Favorite">  
                        </telerik:GridCheckBoxColumn> 
                        <telerik:GridButtonColumn CommandArgument="DigitalAssetID" CommandName="Delete" ButtonType="ImageButton" 
                                ImageUrl="../images/icons/deleteRecord.gif" Text="Delete File" UniqueName="column">  
                                <HeaderStyle Width="30px" /> 
                            </telerik:GridButtonColumn> 
                    </Columns> 
                    <EditFormSettings EditFormType="Template">  
                            <FormTemplate> 
                                    <table style="width: 100%;">  
                                        <tr><td>table full of text boxes</td></tr>  
                                    </table>         
                            </FormTemplate> 
                            <EditColumn UniqueName="EditCommandColumn1">  
                            </EditColumn> 
                        </EditFormSettings> 
                </MasterTableView> 
                <GroupingSettings CaseSensitive="False" /> 
                <ClientSettings AllowDragToGroup="True">  
                </ClientSettings> 
                <FilterMenu EnableTheming="True">  
                    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                </FilterMenu> 
                <PagerStyle AlwaysVisible="true" Mode="NextPrevAndNumeric" /> 
            </telerik:RadGrid> 

    code behind
        //Just need a subset of filters  
        protected void RadGrid1_Init(object sender, System.EventArgs e)  
        {  
            GridFilterMenu menu = RadGrid1.FilterMenu;  
            int i = 0;  
            while (i < menu.Items.Count)  
            {  
                if (  
                   menu.Items[i].Text == "NoFilter" ||  
                   menu.Items[i].Text == "Contains" ||  
                   menu.Items[i].Text == "DoesNotContain" ||  
                   menu.Items[i].Text == "StartsWith" ||  
                   menu.Items[i].Text == "IsEmpty" ||  
                   menu.Items[i].Text == "IsNotEmpty" ||  
                   menu.Items[i].Text == "EqualTo" ||  
                   menu.Items[i].Text == "NotEqualTo")  
                {  
                    i++;  
                }  
                else  
                {  
                    menu.Items.RemoveAt(i);  
                }  
            }  
        }   
          
        //ItemCommand  
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
        {          
            if (e.CommandName == "Delete")  
            {  
               //I have to delete my records manually   
            }  
     
            if (e.CommandName == RadGrid.RebindGridCommandName)  
            {  
                //reset filters  
                RadGrid1.Rebind();  
                RadGrid1.CurrentPageIndex = 0;  
            }  
     
            if (e.CommandName == RadGrid.InitInsertCommandName)  
            {  
                // cancel the default operation  
                e.Canceled = true;  
                //Prepare an IDictionary with the predefined values  
                System.Collections.Specialized.ListDictionary newnewValues = new System.Collections.Specialized.ListDictionary();  
                
                //set default checked state for the checkbox inside the EditItemTemplate  
                  
                //Insert the item and rebind  
                e.Item.OwnerTableView.InsertItem(newValues);  
                e.Item.OwnerTableView.Rebind();  
            }  
     
     
            if (e.CommandName == RadGrid.UpdateCommandName)  
            {  
                //update my records manually  
                e.Item.OwnerTableView.CurrentPageIndex = 0;  
            }  
     
            if (e.CommandName == RadGrid.PerformInsertCommandName)  
            {  
                //insert my records manually  
                e.Item.OwnerTableView.CurrentPageIndex = 0;  
            }  
     
            if(e.CommandName == RadGrid.EditCommandName)  
            {  
                //prepopulate all my textboxes manually  
                  
                // cancel the default operation  
                e.Canceled = true;  
                    
                //Insert the item and rebind  
                e.Item.OwnerTableView.InsertItem(newValues);  
            }  
        }  
     
        //Need to bind some listboxes in the FormTemplate  
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
        {  
             
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
            {  
                GridEditableItem edititem = (GridEditableItem)e.Item;  
                //looking for Session variable PubVidID so we can set the dropdownlist correctly  
                if (Session["PubVidID"] !=null)  
                {                int pubVidID = -1;  
                    if (int.TryParse(Session["PubVidID"].ToString(), out pubVidID))  
                    {  
                        Session["PubVidID"] = null;  
                        if (pubVidID > 0)  
                        {  
                            PublicVideos vds = new PublicVideos();  
                            PublicVideoCategoryTableAdapter pcsta = new PublicVideoCategoryTableAdapter();  
     
                            pcsta.FillByPublicVideoID(vds.PublicVideoCategory, pubVidID);  
     
                            if (vds.PublicVideoCategory != null && vds.PublicVideoCategory.Rows.Count > 0)  
                             {  
                                 ((DropDownList) e.Item.FindControl("ddlCategories")).SelectedValue =  
                                   vds.PublicVideoCategory[0].LC_CategoryID.ToString();  
                             }  
                        }  
                        else  
                        {  
                            ((DropDownList) e.Item.FindControl("ddlCategories")).SelectedIndex = 0;  
                        }  
                    }  
                }  
                else  
                {  
                    ((DropDownList)e.Item.FindControl("ddlCategories")).SelectedIndex = 0;  
                    RadGrid1.CurrentPageIndex = 0;  
                }  
                LoadFileDropDownList((ListBox) e.Item.FindControl("lbVideo"), ".flv");  
                LoadFileDropDownList((ListBox) e.Item.FindControl("lbThumbnail"), ".jpg");  
                  
            }  
        }  
          
        /// <summary> 
        /// Loads the pick list.  
        /// </summary> 
        private void LoadFileDropDownList(ListBox ddl, string ext)  
        {  
            var di = new DirectoryInfo(_ftpPath);  
            FileInfo[] rgFiles = di.GetFiles("*" + ext);  
     
            foreach (FileInfo fi in rgFiles)  
            {  
                if (fi.Extension.ToLower().Equals(ext))  
                {  
                    ddl.Items.Add(new ListItem(fi.Name, fi.Name));  
                }  
            }  
        } 
  2. Answer
    Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 30 Dec 2008 Link to this post

    Hello eric,

    According to your code the grid default behavior on edit command is canceled and instead of edit the insert item is shown:

            if(e.CommandName == RadGrid.EditCommandName) 
            { 
                //prepopulate all my textboxes manually 
                 
                // cancel the default operation 
                e.Canceled = true; 
                   
                //Insert the item and rebind 
                e.Item.OwnerTableView.InsertItem(newValues); 
            } 


    Sincerely yours,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top