Custom Widget - Event Handler

Posted by Community Admin on 04-Aug-2018 20:14

Custom Widget - Event Handler

All Replies

Posted by Community Admin on 23-Apr-2013 00:00

I am developing a custom widget to display/manage podcasts. I am able to pull up the edit dialog box for the widget and have it display a list of podcasts in a repeater. However, when I click on the "Delete" button on one items, my RptPodcast_ItemCommand function is never hit. I am adding an event handler for this function in the InitializeControls method, but it seems to lose this association when the page is posted back. Any ideas why this code isn't firing?

Here is my repeater code:

<asp:Repeater ID="rptPodcast" runat="server">
        <HeaderTemplate>
            <h2>Podcasts</h2>
            <table style="width:775px;">
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><b><span><%#Eval("Title") %></span></b></td>
            </tr>
            <tr>
                <td><span><%#Eval("Date") %></span></td>
            </tr>
            <tr>
                <td>
                    <asp:LinkButton ID="btnDelete" runat="server" Text="Delete" CommandName="Delete"
                        CommandArgument='<%#Eval("PodcastId") %>' />
                </td>
            </tr>           
        </ItemTemplate>
        <SeparatorTemplate>
            <tr><td><hr /></td></tr>
        </SeparatorTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>

Here is my class file that is associated with the podcast user control:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Web.UI;
using Telerik.Sitefinity.Web.UI.ControlDesign;
 
namespace SitefinityWebApp.UserControls
    public class ViewPodcastManage : ContentViewDesignerView
    
        protected virtual Repeater RptPodcast
        
            get return base.Container.GetControl<Repeater>("rptPodcast", true);
        
 
        public override string ViewTitle
        
            get return "Podcast Manage";
        
 
        public override string ViewName
        
            get
            
                return this.GetType().Name;
            
        
 
        protected override void InitializeControls(GenericContainer container)
        
            var ddlPodcast = container.GetControl<DropDownList>("PodcastName", true);
            using (var context = new Work.Business.Entities.EnterpriseEntities())
            
                ddlPodcast.DataSource = from o in context.PodcastHeaders
                                        where o.Delete_Flag == false
                                        orderby o.Name
                                        select o;
                ddlPodcast.DataTextField = "Name";
                ddlPodcast.DataValueField = "Id";
                ddlPodcast.DataBind();
            
 
            RptPodcast.ItemCommand += new RepeaterCommandEventHandler(RptPodcast_ItemCommand);
        
 
        protected override void OnPreRender(EventArgs e)
        
            base.OnPreRender(e);
 
            LoadPodcasts();
        
 
        protected override void OnLoad(EventArgs e)
        
            base.OnLoad(e);
        
 
        protected void LoadPodcasts()
        
            using (var context = new Work.Business.Entities.EnterpriseEntities())
            
                RptPodcast.DataSource = from p in context.Podcasts
                                         orderby p.Date descending
                                         select p;
 
                RptPodcast.DataBind();
            
        
         
        protected override string LayoutTemplateName
        
            get return "SitefinityWebApp.UserControls.Podcast.UcViewPodcastManage.ascx";
        
 
        public override IEnumerable<System.Web.UI.ScriptDescriptor> GetScriptDescriptors()
        
            var descriptor = new ScriptControlDescriptor(this.GetType().FullName, this.ClientID);
 
            return new[] descriptor ;
 
        
 
        public override IEnumerable<System.Web.UI.ScriptReference> GetScriptReferences()
        
            var assemblyName = this.GetType().Assembly.GetName().ToString();
 
            var res = new List<System.Web.UI.ScriptReference> new ScriptReference("SitefinityWebApp.UserControls.Podcast.ViewPodcastManage.js", assemblyName) ;
            return res.ToArray();
        
 
        protected void RptPodcast_ItemCommand(object source, RepeaterCommandEventArgs e)
        
            if (e.CommandName == "Delete" && e.CommandArgument.ToString() != "")
            
                using (var context = new Work.Business.Entities.EnterpriseEntities())
                
                    //TODO: Delete item instead of selecting it
                    var deleteItem = (from p in context.Podcasts
                                     where p.PodcastId == new Guid(e.CommandArgument.ToString())
                                     select p).First();
 
                    System.Diagnostics.Debug.WriteLine("Item to delete: " + deleteItem.Title);
 
                    //Re-bind the repeater after deleting the podcast item
                    //LoadPodcasts();
                
            
        
    

Posted by Community Admin on 25-Apr-2013 00:00

Try moving your

RptPodcast.ItemCommand += new RepeaterCommandEventHandler(RptPodcast_ItemCommand);

Line into your OnLoad function. I'm not sure where InitializeControls falls in the page lifecycle, but it will need to be before the PreRender event.

Thanks,

Chris

Posted by Community Admin on 26-Apr-2013 00:00

Hi Chris,

Thank you for your suggestion. I tried moving the line that you mentioned to the OnLoad function, but still can't seem to get my itemcommand code to fire. I stepped through the code and verified that the InitializeControls function is fired before the PreRender event. Any other ideas?

Thanks,
Dustin

Posted by Community Admin on 16-Sep-2014 00:00

Any luck with this ? I am also having same issue.

Posted by Community Admin on 19-Sep-2014 00:00

Hi all,

I have tested your control and it works fine on my end. The command is fired and I am able to get the command argument. Please, make sure that your page where the widget is placed, has Viewstate enabled.

Regards,
Nikola Zagorchev
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed