Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6681719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:37:19+00:00 2026-05-26T04:37:19+00:00

I have made a form which adds controls dynamically with user selecting the type

  • 0

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

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

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



            }>

And m using this delegate and event handlers to specify the function

  <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);
    }>

**

I would also like to know other methods of calling a function on
dynamically created conlrols ..

**

Thanks in advance for any kind of assistant ..
here is the complete code ..

    <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") &&
                  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") &&
               c.ID == null)
            {

            }
            if (c.GetType().ToString().Equals("System.Web.UI.WebControls.ImageButton") &&
               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") &&
                 c.ID == null)
            {
                ((Label)c).Text = count.ToString() + ".";
            }

            if (c.Controls.Count > 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 < m; i++)
            {

                PlaceHolder1.Controls.Add(new RadioButton());
                PlaceHolder1.Controls.Add(new TextBox());
                PlaceHolder1.Controls.Add(new ImageButton());
                LiteralControl literalBreak3 = new LiteralControl("<br />");
                PlaceHolder1.Controls.Add(literalBreak3);
                LiteralControl literalBreak = new LiteralControl("<br />");
                PlaceHolder1.Controls.Add(literalBreak);
                LiteralControl literalBreak2 = new LiteralControl("<br />");
                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("<br />");
            PlaceHolder1.Controls.Add(literalBreak4);
            LiteralControl literalBreak5 = new LiteralControl("<br />");
            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 < n; i++)
            {
                TextBoxesHere.Controls.Add(new Label());                
                TextBoxesHere.Controls.Add(new TextBox());
                TextBoxesHere.Controls.Add(new ImageButton()); 
                LiteralControl literalBreak = new LiteralControl("<br />");
                TextBoxesHere.Controls.Add(literalBreak);
                LiteralControl literalBreak1 = new LiteralControl("<br />");
                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);

        }

    }

}
>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:37:20+00:00Added an answer on May 26, 2026 at 4:37 am

    The solution to get an image button to work on click is this ..

    }
    protected void img_Click(object sender, ImageClickEventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "img",
        "<script type = 'text/javascript'>alert('ImageButton Clicked');</script>");
    }>
    

    However in my case i am creating the button in CreateRadiobutton so on click the page load function is called therefore the property needs other solution ..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSP form which is made of <input type=file/> tag alone for
I have made a class which a form can inherit from and it handles
I have a form made up of multiple, optional subparts - each of which
I have a form which a user can fill in x times with the
i have a rails form ive made in which the form fields are extracted
I have made a secondary form in my project which can get data from
Say that I have made some software (which has both library form and can
I have started using LWUIT which sounds interesting , i have made a form
I have the following problem. I’ve made a form which change the form elements
I have a form which shows a graph which was made in Microsoft Chart

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.