This topic demonstrates a possible scenario of AJAX Manager updates controls from a composite control. It is important to set IDs of instantiated controls as well as adding them into controls collection. A necessary AJAX setting could be added at CreateChildControls method, however this must be done after controls are added into corresponding controls collection.
The example below includes a single AJAX Manager control on the page. Here is the code-behind:
| C# |
Copy Code |
|
public partial class Default : System.Web.UI.Page { 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.WebControls.AjaxRequestEventArgs e) { //Update control from the composite on AJAX request Tst1.controLbl.Text = Tst1.controTB.Text; } } |
| VB.NET |
Copy Code |
|
Public Partial Class [Default] Inherits System.Web.UI.Page Private Tst1 As TestComposite Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Tst1 = New TestComposite(RadAjaxManager1, "1") Tst1.ID = "tst1" Me.form1.Controls.Add(Tst1) End Sub Protected Sub RadAjaxManager1_AjaxRequest(ByVal sender As Object, ByVal e As Telerik.WebControls.AjaxRequestEventArgs) Tst1.controLbl.Text = Tst1.controTB.Text End Sub End Class |
And the composite control class:
| C# |
Copy Code |
|
public class TestComposite : CompositeControl { private Telerik.WebControls.RadAjaxManager m_radAm; private string m_UniqueId; public TextBox MyTxt; public Label MyLbl; public TestComposite(Telerik.WebControls.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) { MyTxt.RenderControl(output); MyLbl.RenderControl(output); } } |
| VB.NET |
Copy Code |
|
Public Class TestComposite Inherits CompositeControl Private m_radAm As Telerik.WebControls.RadAjaxManager Private m_UniqueId As String Public MyTxt As TextBox Public MyLbl As Label Public Sub New(ByVal radAm As Telerik.WebControls.RadAjaxManager, ByVal UniqueId As String) m_radAm = radAm m_UniqueId = UniqueId End Sub Protected Overloads Overrides Sub RecreateChildControls() EnsureChildControls() End Sub Protected Overloads Overrides Sub CreateChildControls() MyTxt = New TextBox() MyLbl = New Label() MyLbl.ID = "Lbl_" + m_UniqueId MyTxt.ID = "Txt_" + m_UniqueId MyLbl.Text = "LabelText" Me.Controls.Add(MyTxt) Me.Controls.Add(MyLbl) MyTxt.Attributes.Add("onBlur", m_radAm.GetAjaxEventReference(MyTxt.ClientID)) m_radAm.AjaxSettings.AddAjaxSetting(m_radAm, MyLbl) End Sub Public ReadOnly Property controTB() As TextBox Get Return MyTxt End Get End Property Public ReadOnly Property controLbl() As Label Get Return MyLbl End Get End Property Protected Overloads Overrides Sub Render(ByVal output As HtmlTextWriter) MyTxt.RenderControl(output) MyLbl.RenderControl(output) End Sub End Class |
See Also