New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Update in Composite Control

Environment

ProductProgress® Telerik® UI for ASP.NET AJAX

Description

How can I use AjaxManager updates controls from a composite control with Telerik UI for ASP.NET AJAX?

Solution

To use AjaxManager updates controls from a composite control, set IDs of the instantiated controls and add them to the controls collection. You can add a necessary AJAX setting in the CreateChildControls method only after the controls are added into the corresponding controls collection.

The example below includes a single AjaxManager control on the page and shows the code-behind:

C#
	 
TestComposite Tst1;
	
protected void Page_Load(object sender, EventArgs e)
{
	       Tst1 = new TestComposite(RadAjaxManager1, "1");
	       //set an ID of instantiated control!
	       Tst1.ID = "tst1";
	       this.form1.Controls.Add(Tst1);
}
	
protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
{
	       //Update control from the composite on AJAX request
	       Tst1.controLbl.Text = Tst1.controTB.Text;
}
				

The following example demonstrates how to set the composite control class.

C#
	
public class TestComposite : CompositeControl
{
	        private Telerik.Web.UI.RadAjaxManager m_radAm;
	        private string m_UniqueId;
	        public TextBox MyTxt;
	        public Label MyLbl;
	
public TestComposite(RadAjaxManager radAm, string UniqueId)
{
	            m_radAm = radAm;
	            m_UniqueId = UniqueId;

	
protected override void RecreateChildControls()
{
	            EnsureChildControls();
}
	
protected override void CreateChildControls()
{
	            MyTxt = new TextBox();
	            MyLbl = new Label();
	
	            //set IDs of instantiated controls!
	            MyLbl.ID = "Lbl_" + m_UniqueId;
	            MyTxt.ID = "Txt_" + m_UniqueId;
	
	            MyLbl.Text = "LabelText";
	
	            //add the controls into controls collection!
	            this.Controls.Add(MyTxt);
	            this.Controls.Add(MyLbl);
	
	            //Manually add AJAX request triggered on onBlur client event of the text box
	            MyTxt.Attributes.Add("onBlur", m_radAm.GetAjaxEventReference(MyTxt.ClientID));
	
	            //Add the ajax setting - this should be done after controls are added into controls collection
	            m_radAm.AjaxSettings.AddAjaxSetting(m_radAm, MyLbl);
}
	
public TextBox controTB
{
	            get
	            {
	                return MyTxt;
	            }
}
	
public Label controLbl
{
	            get
	            {
	                return MyLbl;
	            }
}
	
protected override void Render(HtmlTextWriter output)
{
	            base.Render(output);
	
	            MyTxt.RenderControl(output);
	            MyLbl.RenderControl(output);
	
}
}  
				

See Also