CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » CodeDigest » Adding Controls dynamically to UpdatePanel control and Setting Triggers Dynamically   You are not logged in.
Search
 

Sponsored Links
 

Technologies
 

Adding Controls dynamically to UpdatePanel control and Setting Triggers Dynamically
Submitted By Satheesh Babu B
On 10/22/2008 6:50:16 AM
Tags: asp.net ajax,CodeDigest  

Adding Controls dynamically to UpdatePanel control and Setting Triggers Dynamically

 

Sometimes, we will require to create UpdatePanel dynamically and load the controls dynamically to it. The below code will help us to do that.

 

protected void Page_Load(object sender, EventArgs e)

    {     

 UpdatePanel up1 = new UpdatePanel();          

       

            TextBox txtName = new TextBox();

            Label lbl = new Label();

            Button btn = new Button();

            lbl.ID = "lblName";

            btn.ID = "btnSave";

            btn.Text = "Save";

            btn.Click += new EventHandler(btn_Click);

            up1.ContentTemplateContainer.Controls.Add(lbl);

            up1.ContentTemplateContainer.Controls.Add(txtName);

            up1.ContentTemplateContainer.Controls.Add(btn);    

            form1.Controls.Add(up1);       

}

          

Setting Triggers Dynamically

When we set the ChildrenAsTriggers to false and UpdateMode to Conditional we need to define triggers explicitly for the UpdatePanel controls. The below code will help us in doing dynamically.

 

protected void Page_Load(object sender, EventArgs e)

    {     

UpdatePanel up1 = new UpdatePanel();

           up1.ChildrenAsTriggers = false;

           up1.UpdateMode = UpdatePanelUpdateMode.Conditional;

       

            TextBox txtName = new TextBox();

            Label lbl = new Label();

            Button btn = new Button();

            lbl.ID = "lblName";

            btn.ID = "btnSave";

            btn.Text = "Save";

            btn.Click += new EventHandler(btn_Click);

            up1.ContentTemplateContainer.Controls.Add(lbl);

            up1.ContentTemplateContainer.Controls.Add(txtName);

            up1.ContentTemplateContainer.Controls.Add(btn);           

            AsyncPostBackTrigger trig = new AsyncPostBackTrigger();

            trig.ControlID = btn.ID;

            trig.EventName = "Click";

            up1.Triggers.Add(trig);

            form1.Controls.Add(up1);

}

protected void btn_Click(object sender, EventArgs e)

{

}

 

We can access the these dynamic controls through the FindControl() method of the Form.

      

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..