How to call a method on image button click dynamically created in C Sharp?
<p>I have made a form which adds controls dynamically with user selecting the type of control he wants which is placed inside a placeholder .. Now i want add a Image Button Click event  to dynamically created Image Button .. Which I have no success in for the past two days .. I have tried using delegates n event handling which is a new concept for me .. 
I am using this to iterate and assign properties</p>

<pre><code> &lt;
     void IterateThroughChildren(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.ImageButton") &amp;&amp;
               c.ID == null)
            {
                ((ImageButton)c).ImageUrl = @"pictures/close-icon.png";
                ((ImageButton)c).ID = "Imagebtn" +count.ToString();

                ((ImageButton)c).Click += new ImageButtonClickEventHandler(UserControl1_ImageButtonClickEvent);            



            }&gt;
</code></pre>

<p>And m using this delegate and event handlers to specify the function </p>

<pre><code>  &lt;public delegate void ImageButtonClickEventHandler(Object sender, EventArgs args);
    public event ImageButtonClickEventHandler ImageButtonClickEvent;

    private void imageButton_Click(object sender, System.EventArgs e)
    {
        if (ImageButtonClickEvent != null)
        {
            ImageButtonClickEvent(sender, e);
        }
    }

    private void UserControl1_ImageButtonClickEvent(Object sender, EventArgs args)
    {
        CreateRadioButtons(sender , args);
    }&gt;
</code></pre>

<p>**</p>

<blockquote>
  <p>I would also like to know other methods of calling a function on
  dynamically created conlrols ..</p>
</blockquote>

<p>**</p>

<p>Thanks in advance for any kind of assistant ..
here is the complete code ..</p>

<pre><code>    &lt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    int count = 1;
    static int m = 0;
    static int n = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        btnAddControl.Visible = false;
        btnAddText.Visible = false;
    }

    void IterateThroughChildren(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") &amp;&amp;
                  c.ID == null)
            {
                ((TextBox)c).ID = "txtQ" + count.ToString();
                ((TextBox)c).Text = "Add Text Here" + count.ToString();
                ((TextBox)c).Columns = 90;
                ((TextBox)c).Focus();
                 //((TextBox)c).TextChanged = "CreateRadioButtons()";
               // ((TextBox)c).Attributes["onclick"] = "this.CreateRadiobuttons()";
                //((TextBox)c).TextChanged();
                    //+= CreateRadioButton;                
                //((TextBox)c).Focus = CreateRadioButtons();

                count++;
            }
            if (c.GetType().ToString().Equals("System.Web.UI.WebControls.RadioButton") &amp;&amp;
               c.ID == null)
            {

            }
            if (c.GetType().ToString().Equals("System.Web.UI.WebControls.ImageButton") &amp;&amp;
               c.ID == null)
            {
                ((ImageButton)c).ImageUrl = @"pictures/close-icon.png";
                ((ImageButton)c).ID = "Imagebtn" +count.ToString();
                ((ImageButton)c).Click += new ImageClickEventHandler(_Default_Click);
                ((ImageButton)c).Click += new ImageClickEventHandler(CreateRadioButtons);

            }
            if (c.GetType().ToString().Equals("System.Web.UI.WebControls.Label") &amp;&amp;
                 c.ID == null)
            {
                ((Label)c).Text = count.ToString() + ".";
            }

            if (c.Controls.Count &gt; 0)
            {
                IterateThroughChildren(c);
            }
        }
    }

    void _Default_Click(object sender, ImageClickEventArgs e)
    {
        CreateRadioButtons(sender,e);
        throw new NotImplementedException();
    }

    protected void abc_acb( object sender, EventArgs e)
    { 
    }

    protected void CreateRadioButtons(object sender, EventArgs e)
    {
        if (Page.IsPostBack == true)
        {
            btnAddControl.Visible = true;
            m++;

            // now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
            for (int i = 0; i &lt; m; i++)
            {

                PlaceHolder1.Controls.Add(new RadioButton());
                PlaceHolder1.Controls.Add(new TextBox());
                PlaceHolder1.Controls.Add(new ImageButton());
                LiteralControl literalBreak3 = new LiteralControl("&lt;br /&gt;");
                PlaceHolder1.Controls.Add(literalBreak3);
                LiteralControl literalBreak = new LiteralControl("&lt;br /&gt;");
                PlaceHolder1.Controls.Add(literalBreak);
                LiteralControl literalBreak2 = new LiteralControl("&lt;br /&gt;");
                PlaceHolder1.Controls.Add(literalBreak2);
            }
            EventHandler eh = new EventHandler(CreateRadioButtons);
            TextBox Mytextbox = new TextBox();
            Mytextbox.ID = "123";
            Mytextbox.Columns = 90;
            Mytextbox.AutoPostBack = true;
            Mytextbox.Text = "Click to create another option";
            Mytextbox.TextChanged += eh;
            //Mytextbox.TextChanged += CreateRadioButtons;
            PlaceHolder1.Controls.Add(new RadioButton());
            PlaceHolder1.Controls.Add(Mytextbox);
            LiteralControl literalBreak4 = new LiteralControl("&lt;br /&gt;");
            PlaceHolder1.Controls.Add(literalBreak4);
            LiteralControl literalBreak5 = new LiteralControl("&lt;br /&gt;");
            PlaceHolder1.Controls.Add(literalBreak5);

            // now, set the Text property of each TextBox
            IterateThroughChildren(this);
        }

    }
    protected void CreateTextBoxes(object sender , EventArgs e)
    {
        btnAddText.Visible = true;


        if (Page.IsPostBack == true)
        {
            n++;

            // now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
            for (int i = 0; i &lt; n; i++)
            {
                TextBoxesHere.Controls.Add(new Label());                
                TextBoxesHere.Controls.Add(new TextBox());
                TextBoxesHere.Controls.Add(new ImageButton()); 
                LiteralControl literalBreak = new LiteralControl("&lt;br /&gt;");
                TextBoxesHere.Controls.Add(literalBreak);
                LiteralControl literalBreak1 = new LiteralControl("&lt;br /&gt;");
                TextBoxesHere.Controls.Add(literalBreak1);

            }

            // now, set the Text property of each TextBox
            IterateThroughChildren(this);
        }

    }


    protected void ddlSelectControl_SelectedIndexChanged(object sender, EventArgs e)
    {

        string a = ddlSelectControl.SelectedValue.ToString();
        if (a == "0")
        {
            CreateRadioButtons(sender, e);
        }
        else
        {
            CreateTextBoxes(sender ,e);

        }

    }

}
&gt;
</code></pre>
