Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michael Falch Madsen 70 posts 92 karma points
    Feb 08, 2011 @ 17:15
    Michael Falch Madsen
    0

    Usercontrol with dynamic buttons + click events

    Have searched google but not found anything usefull so i will try here.

    I have a usercontrol which renders X forms.

    email = new TextBox()
                {
                    ID = "email"+node.Id
                };

                Panel1.Controls.Add(email);

                message = new TextBox()
                {
                    ID = "message" + node.Id
                };

                Panel1.Controls.Add(message);
                ImageButton button = new ImageButton()           
                {
                    ID = "button" + node.Id,
                    CommandArgument = node.Id.ToString()+"~~"+email.Text+"~~"+message.Text,               
                };
               
                Panel1.Controls.Add(button);
                button.Command += new CommandEventHandler(button_Command);

     

    The Click event only works for the last form added to the Panel?

    Can i name ImageButton dynamic.. like ImageButton "button"+node.Id and the have an click event for that button?

    ImageButton "button"+node.Id does'nt work.. any idea to where to go from here?

    Appriciate any help :)

  • Michael Falch Madsen 70 posts 92 karma points
    Feb 08, 2011 @ 19:17
    Michael Falch Madsen
    0

    Have made a simple test. Hope it's simpler to see what i want to do.

     

    using System;
    using System.Web.UI.WebControls;

     

    public partial class _Default : System.Web.UI.Page

    {

        public TextBox txt;

        protected void Page_Load(object sender, EventArgs e)

        {

            for (int i = 0; i < 10; i++)

            {

                TextBox txt = new TextBox() {

                    Text = "Text"+i

                };

                Panel1.Controls.Add(txt);

                Button btn = new Button()

                {

                    Text = "Button" + i

                };

                Panel1.Controls.Add(btn);

                btn.Click += new EventHandler(btn_Click);

            }

        }

     

        void btn_Click(object sender, EventArgs e)

        {

            Button clicked = (Button)sender;

     

            Response.Write("You have clicked button " + clicked.Text);

            Response.Write("Value of corresponding textbox ?? WHAT TO PUT HERE???? = ");

        }

    }

  • Michael Falch Madsen 70 posts 92 karma points
    Feb 08, 2011 @ 19:50
    Michael Falch Madsen
    0

    Found a solution that i can live with.. but there have to be a better way..

     

    using System;

    using System.Web.UI.WebControls;

     

    public partial class _Default : System.Web.UI.Page

    {

        public TextBox txt;

        protected void Page_Load(object sender, EventArgs e)

        {

            for (int i = 0; i < 10; i++)

            {

                TextBox txt = new TextBox() {

                    Text = "Text"+i,

                    ID = "txt"+i

                };

                Panel1.Controls.Add(txt);

     

                txt = new TextBox()

                {

                    Text = "sdfsdfsdf",

                    ID = "msg" + i

                };

                Panel1.Controls.Add(txt);

     

                Button btn = new Button()

                {

                    Text = "Button" + i,

                    CommandArgument = i.ToString()

                };

     

                Panel1.Controls.Add(btn);

                btn.Command += new CommandEventHandler(btn_Command);

            }

        }

     

        void btn_Command(object sender, CommandEventArgs e)

        {

            Button clicked = (Button)sender;

     

            Response.Write("You have clicked button " + clicked.Text);

     

            TextBox txt = (TextBox)FindControl("txt" + e.CommandArgument.ToString());

            Response.Write("Value of textbox"+txt.Text);

     

            txt = (TextBox)FindControl("msg" + e.CommandArgument.ToString());

            Response.Write("Value of msg" + txt.Text);

     

     

        }

    }

Please Sign in or register to post replies

Write your reply to:

Draft