1

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

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks very much, Jupanol. Anoter thing: 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: PopulateControls??
Why don't you just pass as a parameter to HowManyClicked the button from buttonHandler like this HowManyClicked(Button button) and buttonHandler{HowManyClicked(sender as Button)}
Well, HowManyClicked() is meant to calculate/update after each click, how many buttons were clicked in total/overall. So that's why I didn't use parameters for the method signature. It's more of a general inquiry, doesn't refer to a button.
Great, many thanks Jupanol! I just figured another way to do this: 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; } }
This works too, by just redefining HowManyClicked() but still not sure what the exaplanation is of why my first method didn't work.
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;
   ....
}

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.