开发者

How to call a method on image button click dynamically created in C Sharp?

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 conc开发者_StackOverflowept 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);

        }

    }

}
>


It's not clear what is actually happening, but your subscription code would be much better as (assuming .NET 3.5, so you can use LINQ):

foreach (ImageButton button in parent.Controls
                                     .OfType<ImageButton>()
                                     .Where(c => c.ID == null))
{
    button.ImageUrl = @"pictures/close-icon.png";
    buttonID = "Imagebtn" + count;

    button.Click += new UserControl1_ImageButtonClickEvent;
}

Note that that will only iterate over the controls directly under parent. If there are other container controls, you may need to recurse through those, e.g.

foreach (CompositeControl container in parent.Controls
                                             .OfType<CompositeControl>())
{
    IterateThroughChildren(container);
}

It's possible that that was what was actually wrong... but you haven't said what you've actually observed to be happening, which makes it harder to help you...


In CreateTextBoxes()

   for (int i = 0; i < n; i++)
   {
             ImageButton iM = new ImageButton();//
             TextBoxesHere.Controls.Add(new Label());
             TextBoxesHere.Controls.Add(new TextBox());                 
             TextBoxesHere.Controls.Add(iM );                           
             LiteralControl literalBreak = new LiteralControl("<br />");                 
             TextBoxesHere.Controls.Add(literalBreak);                 
             LiteralControl literalBreak1 = new LiteralControl("<br />");                 
             TextBoxesHere.Controls.Add(literalBreak1);              
 } 

In CreateRadioButtons()

for (int i = 0; i < m; i++)                      
{  
            ImageButton iM = new ImageButton();//
            PlaceHolder1.Controls.Add(iM);                          
            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);                      
}         

Can you try the above code after omitting c.ID == null and ((ImageButton)c).ID = "Imagebtn" +count.ToString();?That is just what I would try.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜