+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Custom Widget - Event Handler

Custom Widget - Event Handler

5 posts, 0 answered
  1. Dustin
    Dustin avatar
    14 posts
    Registered:
    14 Jun 2011
    23 Apr 2013
    Link to this post
    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();
                    }
                }
            }
        }
    }
  2. Chris
    Chris avatar
    11 posts
    Registered:
    10 Mar 2011
    25 Apr 2013
    Link to this post
    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
  3. Dustin
    Dustin avatar
    14 posts
    Registered:
    14 Jun 2011
    26 Apr 2013
    Link to this post
    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
  4. Praneeth
    Praneeth avatar
    26 posts
    Registered:
    22 Jun 2012
    16 Sep 2014
    Link to this post

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

  5. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    19 Sep 2014
    Link to this post
    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
     
5 posts, 0 answered