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

ajax client events not firing with custom controls

2 Answers 96 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 19 Mar 2009, 08:27 PM
Hi there,

I'm using the RadAjaxManager to handle AJAX in my site. When using standard .NET controls (linkbutton, literal etc.) the AJAX works and client events fire as expected. However, when I use my own controls (from a class library) the AJAX works, but the client events do not fire. Do you have any ideas why that might be?

Thanks,
Jon.

the code in my initiating control is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xach.WebControls;
using Telerik.Web.UI;

namespace Xach.CMS.WebControls
{
    public class LightboxLink : Control, INamingContainer
    {
        public string Control { get; set; }
        public string LightboxControllerID { get; set; }
        public string Text { get; set; }
        public string CssClass { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // wire up ajax
            List<Control> list = Page.Master.FindControlsByType(typeof(RadAjaxManager));
            RadAjaxManager manager = list[0] as RadAjaxManager;
            manager.AjaxSettings.AddAjaxSetting(this, Page.Master.FindControlRecursive(LightboxControllerID));
            
            /*manager.AjaxSettings[manager.AjaxSettings.Count - 1].EventName = "lightbox";*/
        }

        protected override void CreateChildControls()
        {
            LinkButton button = new LinkButton() { Text = Text, CssClass = CssClass };
            button.ID = "LbButton";
            button.Click += new EventHandler(button_Click);
            button.OnClientClick = "lightBox.start()";
            Controls.Add(button);
        }

        protected void button_Click(object sender, EventArgs e)
        {
            LightboxController controller = (LightboxController)Page.Master.FindControlRecursive(LightboxControllerID);
            controller.CreateLightboxControl(Control);
        }
    }
}

and in the updated control:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Xach.CMS.WebControls
{
    public class LightboxController : Control
    {
        protected override void CreateChildControls()
        {

            base.CreateChildControls();
        }

        public void CreateLightboxControl(string control)
        {
            //HttpContext.Current.Response.Write("loaded");
            Control ctrl = Page.LoadControl(control);
            Controls.Add(ctrl);
        }
    }
}





2 Answers, 1 is accepted

Sort by
0
Accepted
SamJ
Top achievements
Rank 1
answered on 22 Mar 2009, 12:30 AM
Hi Jonathan,

Just 2 notes:

  • The best and universal way to access RadAjaxManager on a Page is to use the GetCurrent() method as below:
    RadAjaxManager manager = RadAjaxManager.GetCurrent(Page); 
  • If a user control is loaded dynamically, the you'd better register its client scripts through the ScriptManager.RegisterStartupScript() method. A sample is available here.

Regards,
SamJ
0
Jonathan
Top achievements
Rank 1
answered on 25 Mar 2009, 06:01 PM
Thanks SamJ :)
Tags
Ajax
Asked by
Jonathan
Top achievements
Rank 1
Answers by
SamJ
Top achievements
Rank 1
Jonathan
Top achievements
Rank 1
Share this question
or