Click events on Array of buttons

How can I get the name of the object last clicked on a panel? The trick is there is a big array of buttons on the panel (btn[1] … btn [200]). How can I check if I clicked on button b[180], or b[11] or even outside the panel (no button)? Also the buttons are generated at page load via coding.
Thank you. Anna

EDIT:
Thank you! Another issue that arose (this generated a NULL object reference):
I have a method on the same level as buttonHandler(), it is named HowManyClicked() and it’s called from within buttonHandler(). Inside HowManyClicked() I want to identify Button btn1 = Panel2.FindControl(x) as Button; where x is, for example, buttonArray[2,3]. But I always get NULL. Is the button array buttonArray not identifiable by name once out of the method that generated it??

        public void buttonHandler(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            //string tt = btn.ToolTip.ToString();
            btn.BackColor = Color.Red;
            statusL.Text = HowManyClicked().ToString();

        }

        public int HowManyClicked()
        {
            int sum=0;
            for (int a = 0; a < 10; a++)
                for (int b = 0; b < 14; b++)
                {
                    string x = "buttonArray[" + a + ", " + b + "]";
                    statusL.Text = x;
                    Button btn1 = Panel2.FindControl(x) as Button;
                    if (btn1.BackColor == Color.Red) sum += 1;

                }
            return sum;
        }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As @AVD commented you can get the button originating the postback casting the sender object, you can also use the CommandName and CommandArgument properties from the button object (they are usually used when the button is inside a Grid, DataList etc but you can use them in this context if you need):

    protected void Page_Init(object sender, EventArgs e)
    {
        var s = Enumerable.Range(1, 10);
        foreach (var item in s)
        {
            var b = new Button();
            b.Text = "My Button " + item.ToString();
            b.CommandName = "custom command";
            b.CommandArgument = item.ToString();
            b.Click += new EventHandler(b_Click);
            this.myPanel.Controls.Add(b);
        }
    }

    void b_Click(object sender, EventArgs e)
    {
        var current = sender as Button;
        this.lblMessage2.Text = "Clicked from array buttons: <br/>Command Name: " + current.CommandName + "<br/>Args: " + current.CommandArgument + "<br/>Button Unique ID: " + current.UniqueID + "<br/>Client ID: " + current.ClientID;
    }

Page:

<asp:Panel runat="server" ID="myPanel">
</asp:Panel>

<asp:Label ID="lblMessage2" runat="server" />

This code generates something like:

enter image description here

As an additional note, Microsoft recommends to create dynamic controls in the PreInit event or in case you are using a master page, in the Init event

source

Edited

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.ViewState["count"] = 0;
        }

    }


    protected void Page_Init(object sender, EventArgs e)
    {
        var s = Enumerable.Range(1, 10);
        foreach (var item in s)
        {
            var b = new Button();
            b.Text = "My Button " + item.ToString();
            b.Click += new EventHandler(buttonHandler);
            this.myPanel.Controls.Add(b);
        }
    }

    void buttonHandler(object sender, EventArgs e)
    {
        // update here your control
        var b = sender as Button;
        b.BackColor = Color.Red;
        HowManyClicked();
    }

    private void HowManyClicked()
    {
        var c = (int)this.ViewState["count"];
        c++;
        this.ViewState["count"] = c;
        this.lblMessage2.Text = "Clicked controls: " + this.ViewState["count"].ToString();
    }

This produced:

enter image description here

Method 2

How can I get the name of the object last clicked on a panel?

The first parameter of click handler returns the reference of control/object has raised the event.

public void buttonHandler(object sender, EventArgs e)
{
   Button btn=sender as Button;
   ....
}

Method 3

I just figured out another fix by just redefining HowManyClicked() so I am adding it here below. Not sure still why the first method (the one in my question) didn’t work also. Here goes:

public int HowManyClicked()
        {
            int sum=0;
            foreach (Control cnt in this.Panel2.Controls)
                if (cnt is Button)
                {
                    Button btn = (Button)cnt;
                    if (btn.BackColor == Color.Red)
                        sum += 1;
                }
            return sum;
           }

        }

Thanks everyone!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x