0

Big question about code-generated buttons at page load VERSUS manually-added buttons in design view.

You can't manually add 140 buttons, so I prefer code. All buttons will behave similarly at button click (they will change colour). I can't obviously write 140 click event methods, so how can I make a general event that can work for all?

I thought of doing this:

protected void btn_Click(object sender, EventArgs e)
        {
            Button btn1 = Panel2.FindControl("btn") as Button;
            btn1.BackColor = Color.Red;
         }

Because all buttons are called btn.

This is how I add buttons:

for (int a = 1; a <= 10; a++)
            {
                for (char b = 'A'; b <= 'N'; b++)
                {
                    btn = new Button();
                    btn.Text = "";
                    btn.Height = 20; btn.Width = 20;
                    btn.ToolTip = a + " " + b;
                    Panel2.Controls.Add(btn);
                }
                Panel2.Controls.Add(new LiteralControl("<br/>")); // this inserts a paragraph

            }

I don't know if btn_Click(object sender, EventArgs e) works, because after I click a button all disappear. How can I fix that?

At the end, I need to count how many buttons are red. Is my following idea practical? We know how to find the Button control called "btn", but how do I find "a button with a certain property, for example find button named btn with tooltip=1A. I would go through all the tooltips (1A, ... 10N) and check each button (considering they have unique tooltips), and check the color each time. For each red color, I count a button.

Is it a problem is they are all called "btn"? For now I can differentiate them by tooltip: 1A, 1B, ...10N, as I said. Thanks a lot.

EDIT>>>>>>> I tried Ben's advice and the problem is now that all the buttons disappear when I click any of them. Thanks for yoru patience.

public partial class Seatalloc2 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateControls();
            }
        }

        protected void PopulateControls()
        {

            Panel1.Visible = true;
            Button btn;

            Panel2.Visible = true;
            DropDownList nrbileteddl = Panel1.FindControl("nrBileteddl") as DropDownList;
            nrbileteddl.Visible = true;

            for (int i = 1; i <= 5; i++)
            {
                nrbileteddl.Items.Add(i + "");
            }

            for (int a = 1; a <= 10; a++)
            {
                for (char b = 'A'; b <= 'N'; b++)
                {
                    btn = new Button();
                    btn.Text = "";
                    btn.Height = 20;
                    btn.Width = 20;
                    btn.ToolTip = a + " " + b;
                    btn.BackColor = Color.Green;
                    btn.Click += new EventHandler(btn_Click);
                    Panel2.Controls.Add(btn);
                }
                Panel2.Controls.Add(new LiteralControl("<br/>"));
            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.BackColor = Color.Red;
        }
    }
Anna T
  • 1,027
  • 7
  • 21
  • 29

3 Answers3

1

You ask few different questions here.
You can create dynamic buttons (or any other controls), but in order to keep them synchronized with the page life cycle, events and view state, you need to create them at the initialization phase of the page life cycle (and i cant understand from your code if this is what you're doing)
You called your variable "btn", but thats not how the controls are identified in ASP.NET. Each of them has an ID (and a UniqueID), and those IDs should be unique. If you attach the same event handler to your buttons, you dont need to find the firing button in the event handler, it is the sender object that you receive as part of the event handling mehotds.

I strongly recommend for you to read this msdn page, which explains everything you asked about.

YavgenyP
  • 2,113
  • 14
  • 11
0

All you need to do is to hook an event handler on each Button.Click. This will have the Sender as being the Button, so you can cast that to a Button set it's color to Red. And in the same method you will increase some local field to increase the number of red button.

Pseudo code like this :

       int red_ = 0;
    void Method()
    {
        for(int i=0; i< 100; i++)
        {

            Button btn = new Button();
            btn.Click += new EventHandler(btn_Click);
        }
    }

    void btn_Click(object sender, RoutedEventArgs e)
    {
        Button btn = sender as Button;
        // set btn to red
        red_++;
    }
MBen
  • 3,956
  • 21
  • 25
  • Thank you Ben, I tried your advice and will reedit my question. The problem is that after I click a button, all disappear. Why could this be? – Anna T Jun 15 '12 at 19:27
0

Giving the same ID to ASP.NET control will not fetch the desired result. If changing the colors of the button is the only requirement, I would strongly advocate using JQuery for following reasons:

  1. Easy to implement and because it'll run at client, it'll be very fast
  2. Easy to fetch the count of clicked buttons by using appropriate selectors (you can use any property of the button such as ID, CSSClass OR even Name of the button.

My advise:

  1. In JQuery, on document load add input (type=buttons) with different IDs but same name (say "btn").
  2. Write another event in JQuery which would listen to button click of all the buttons with name "btn". Inside that function, change the color of the button using Style property.
  3. At the end, if you wish to find how many buttons are red, that is also possible by using JQuery by using appropriate selector for Style
DotNetFreak
  • 166
  • 5
  • Many thanks DotNetFreak, sounds great but I don't know JQuery. I need to do this task quick so I try to use what I know. I will make sure however to learn JQuery in the future, it seems very popular. I hope using an array is OK for now? `Button[][] buttonArray = new Button[10][14]; for (int a = 1; a <= 10; a++) { for (int b = 1; b <= 14; b++) { buttonArray[a][b] = new Button(); } }` . But I still don't know how to check which button from the array was clicked? I need that syntax so I can proceed and colour it. – Anna T Jun 15 '12 at 19:12
  • You can use this link [http://stackoverflow.com/questions/6123136/add-button-to-an-html-page-using-javascript] to create buttons from javascript. While creating the button, please append 'a' and 'b' variables of your for loop to the Name element of button. At each of the button, call the same javascript function which would do the following: extract the Name and mark another 10X14 two dimensional array so that you can know exactly which button was clicked. – DotNetFreak Jun 15 '12 at 19:28