Reference User Control Edit Mode

3 posts, 1 answers
  1. sircutbreaker
    sircutbreaker avatar
    111 posts
    Member since:
    Oct 2007

    Posted 27 Apr 2009 Link to this post

    I am having trouble referencing a user control in edit mode..

    the user control of course is 'gallery1' in the edit mode template of the first column.

    here are the columns of the grid...

    and the code behind follows...


    <Columns> 
            <telerik:GridTemplateColumn DataField="WallID" DataType="System.Int32" 
                HeaderText="WallID" SortExpression="WallID" UniqueName="WallID"
                <EditItemTemplate> 
                    <uc1:gallery ID="gallery1" runat="server" /> 
                </EditItemTemplate> 
                <ItemTemplate> 
                    <asp:Label ID="WallIDLabel" runat="server" Text='<%# Eval("WallID") %>'></asp:Label> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn DataField="Title" HeaderText="Title" 
                SortExpression="Title" UniqueName="Title"
                
                <ItemTemplate> 
                    <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>'></asp:Label> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn DataField="Description" HeaderText="Description" 
                SortExpression="Description" UniqueName="Description"
                 
                <ItemTemplate> 
                    <asp:Label ID="DescriptionLabel" runat="server" 
                        Text='<%# Eval("Description") %>'></asp:Label> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn DataField="Link" HeaderText="Link" 
                SortExpression="Link" UniqueName="Link"
                 
                <ItemTemplate> 
                    <asp:Label ID="LinkLabel" runat="server" Text='<%# Eval("Link") %>'></asp:Label> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn DataField="Date" DataType="System.DateTime" 
                HeaderText="Date" SortExpression="Date" UniqueName="Date"
                 
                <ItemTemplate> 
                    <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>'></asp:Label> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
        </Columns> 



    Here is the code behind... I am having trouble with the edit mode only... I need to reference the control to populate it....

    the insert and everything else is working fine.

     GridDataItem item; 
            GridEditFormItem formitem; 
            GridDataItem dataItem; 
            string id; 
            Wall w; 
            ASP.uc_gallery_ascx gallery; 
            string path = Server.MapPath("~/images/wall"); 
            WebImage wi; 
     
            if (e.Item is GridEditableItem) 
            { 
                if (e.CommandName == "Edit"
                { 
                    item = (GridDataItem)e.Item; 
                    //item = (GridDataItem)e.Item; 
                    gallery = (ASP.uc_gallery_ascx)item.FindControl("gallery1"); 
     
                    Label l = (Label)item.FindControl("WallIDLabel"); 
     
                    id = l.Text; 
     
                    w = DataRepository.WallProvider.GetByWallId(int.Parse(id)); 
     
                    gallery.Title = w.Title; 
     
                    gallery.Description = w.Description; 
                    gallery.DateEntered = (DateTime)w.Date; 
     
     
                    //gallery.Reference.SynchronizeFileSystem(true, Server.MapPath("~/images/wall"), w.WallId.ToString()); 
                    wi = WebImage.Load(Server.MapPath("~/images/wall/"), w.WallId.ToString()); 
                    gallery.Reference.Value = wi; 
     
                } 
     
     
                if (e.CommandName == "Update"
                { 
                    formitem = (GridEditFormItem)e.Item; 
                    //item = (GridDataItem)e.Item; 
                    gallery = (ASP.uc_gallery_ascx)formitem["WallID"].FindControl("gallery1"); 
     
                    dataItem = formitem.ParentItem as GridDataItem; 
                    id = dataItem["WallID"].Text; 
                    w = DataRepository.WallProvider.GetByWallId(int.Parse(id)); 
                    w.Title = gallery.Title; 
                    w.Description = gallery.Description; 
                    w.Date = gallery.DateEntered; 
                     
     
                    DataRepository.WallProvider.Save(w); 
     
                    gallery.Reference.SynchronizeFileSystem(true,path,w.WallId .ToString()); 
                     
     
                } 
     
                if (e.CommandName == "PerformInsert"
                { 
     
     
                    formitem = (GridEditFormItem)e.Item; 
                    //item = (GridDataItem)e.Item; 
                    gallery = (ASP.uc_gallery_ascx)formitem["WallID"].FindControl("gallery1"); 
     
                    dataItem = formitem.ParentItem as GridDataItem; 
                    //id = dataItem["WallID"].Text; 
                    w = new Wall(); 
                    w.Title = gallery.Title; 
                    w.Description = gallery.Description; 
                    w.Date = gallery.DateEntered; 
     
     
                    DataRepository.WallProvider.Save(w); 
     
                    gallery.Reference.SynchronizeFileSystem(true, Server.MapPath("~/images/wall"), w.WallId.ToString()); 
     
                } 
     
                if (e.CommandName == "Delete"
                { 
                    item = (GridDataItem)e.Item; 
     
                    id = item.GetDataKeyValue("WallID").ToString(); 
                    w = DataRepository.WallProvider.GetByWallId(int.Parse(id)); 
                     
                    wi = Radactive.WebControls.ILoad.WebImage.Load(path, w.WallId.ToString()); 
                    if (Radactive.WebControls.ILoad.WebImage.ExistsInFileSystem(path, w.WallId.ToString())) 
                    { 
                        Radactive.WebControls.ILoad.WebImage.DeleteFromFileSystem(path, w.WallId.ToString()); 
                    } 
                    DataRepository.WallProvider.Delete(w); 
                } 
     



    Really would appreciate any help... i cant reference gallery in edit mode... the other modes have no issues.
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Apr 2009 Link to this post

    Hello,

    The EditCommand is too early to access the EditForm or the UserControl since the edit form items would not be rendered then. You could rather try accessing the EditForm in the ItemDataBound event of the grid as shown below:
    c#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        {     
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode
             { 
                   GridEditFormItem formItem = (GridEditFormItem)e.Item; 
                   gallery = (ASP.uc_gallery_ascx)formItem.FindControl("gallery1"); 
     
                   Label l = (Label)formItem.ParentItem.FindControl("WallIDLabel"); 
     
                   id = l.Text; 
     
                    w = DataRepository.WallProvider.GetByWallId(int.Parse(id)); 
     
                    gallery.Title = w.Title; 
     
                    gallery.Description = w.Description; 
                    gallery.DateEntered = (DateTime)w.Date; 
     
                    //gallery.Reference.SynchronizeFileSystem(true, Server.MapPath("~/images/wall"), w.WallId.ToString()); 
                    wi = WebImage.Load(Server.MapPath("~/images/wall/"), w.WallId.ToString()); 
                    gallery.Reference.Value = wi; 
             } 
         }    

    HTH

    -Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. sircutbreaker
    sircutbreaker avatar
    111 posts
    Member since:
    Oct 2007

    Posted 28 Apr 2009 Link to this post

    As always, you guys are right on time :)

    Thank you for unraveling this for me...

    i had to add some references, but the code you provided has solved this mystery.

    here is the working code to edit the user control inside the grid:

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using jsite.Data; 
    using jsite.Entities; 
    using Radactive.WebControls.ILoad; 
    using Telerik.Web.UI; 
     
    public partial class MasterPages_Default : System.Web.UI.Page 
        protected void Page_Load(object sender, EventArgs e) 
        { 
     
        } 
        protected void rgWall_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
        { 
            rgWall.DataSource = this.WallDataSource; 
     
        } 
        protected TList<Wall> WallDataSource 
        { 
            get 
            { 
                TList<Wall> wall = DataRepository.WallProvider.GetAll(); 
                return wall; 
            } 
        } 
        protected void rgWall_ItemDataBound1(object sender, GridItemEventArgs e) 
        { 
            string id; 
            Wall w; 
            ASP.uc_gallery_ascx gallery; 
            string path = Server.MapPath("~/images/wall"); 
            WebImage wi; 
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
            { 
                GridEditFormItem formItem = (GridEditFormItem)e.Item; 
                gallery = (ASP.uc_gallery_ascx)formItem.FindControl("gallery1"); 
                
                try 
                { 
                    id = formItem.GetDataKeyValue("WallID").ToString(); 
                } 
                catch 
                { 
                    return
                } 
                 
                 
                w = DataRepository.WallProvider.GetByWallId(int.Parse(id)); 
     
                gallery.Title = w.Title; 
     
                gallery.Description = w.Description; 
                gallery.DateEntered = (DateTime)w.Date; 
                gallery.Link = w.Link; 
     
                //gallery.Reference.SynchronizeFileSystem(true, Server.MapPath("~/images/wall"), w.WallId.ToString());  
                wi = WebImage.Load(path, w.WallId.ToString()); 
                gallery.Reference.Value = wi;            
            }  
        } 
           
        protected void rgWall_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            GridDataItem item; 
            GridEditFormItem formitem; 
            string id; 
            Wall w; 
            ASP.uc_gallery_ascx gallery; 
            string path = Server.MapPath("~/images/wall"); 
            WebImage wi; 
     
            if (e.Item is GridEditableItem) 
            { 
                if (e.CommandName == "Update"
                { 
                     
                    formitem = (GridEditFormItem)e.Item; 
                    //item = (GridDataItem)e.Item; 
                    gallery = (ASP.uc_gallery_ascx)formitem["WallID"].FindControl("gallery1"); 
                    
                    id = formitem.GetDataKeyValue("WallID").ToString(); 
                    w = DataRepository.WallProvider.GetByWallId(int.Parse(id)); 
                    w.Title = gallery.Title; 
                    w.Description = gallery.Description; 
                    w.Date = gallery.DateEntered; 
                    w.Link = gallery.Link; 
                     
                    DataRepository.WallProvider.Save(w); 
     
                    gallery.Reference.SynchronizeFileSystem(true,path,w.WallId .ToString()); 
                } 
     
                if (e.CommandName == "PerformInsert"
                { 
                    formitem = (GridEditFormItem)e.Item; 
                    //item = (GridDataItem)e.Item; 
                    gallery = (ASP.uc_gallery_ascx)formitem["WallID"].FindControl("gallery1"); 
     
                    w = new Wall(); 
                    w.Title = gallery.Title; 
                    w.Description = gallery.Description; 
                    w.Date = gallery.DateEntered; 
                    w.Link = gallery.Link; 
                     
                    DataRepository.WallProvider.Save(w); 
     
                    gallery.Reference.SynchronizeFileSystem(true, path, w.WallId.ToString()); 
     
                } 
     
                if (e.CommandName == "Delete"
                { 
                    item = (GridDataItem)e.Item; 
     
                    id = item.GetDataKeyValue("WallID").ToString(); 
                    w = DataRepository.WallProvider.GetByWallId(int.Parse(id)); 
                     
                    wi = Radactive.WebControls.ILoad.WebImage.Load(path, w.WallId.ToString()); 
                    if (Radactive.WebControls.ILoad.WebImage.ExistsInFileSystem(path, w.WallId.ToString())) 
                    { 
                        Radactive.WebControls.ILoad.WebImage.DeleteFromFileSystem(path, w.WallId.ToString()); 
                    } 
                    DataRepository.WallProvider.Delete(w); 
                } 
     
                 
            } 
        } 
         
     


    Thank you very much for your help!

    Jeff
Back to Top