I'm not sure if the title makes any sense, but this is what I'm trying to do. I have multiple buttons and it's connected to this single event handler. The ID for each button is btnXX, XX being the row and column.

protected void btn_Click(object sender, EventArgs e)

        Button btn = (Button)sender;
        int clicked_row = int.Parse(btn.ID.Substring(3, 1));
        int clicked_col = int.Parse(btn.ID.Substring(4, 1));

As you can see, it takes the row and column number into an int value. When a button is clicked, the buttons above and right of it needs to change colors. I put the int value into a loop so it would know which buttons to change colors.

What I don't understand is how would I put those integer values back into the button ID names so it knows which button to change colors? So if I clicked on btn24, clicked_row would be read as 2 and once it goes through the loop, it would now be 1 and should tell the program btn14 needs to be changed.


Edited by kyeong: n/a

7 Years
Discussion Span
Last Post by kyeong

I believe what you want to do is use the FindControl method. You can programmatically piece together your control's ID and then retrieve that control. This is an example:

        <asp:Button ID="btnDemo" runat="server" OnClick="btnDemo_Click" Text="Demo" />
        <asp:Button ID="btnTarget" runat="server" Text="Target" />
protected void btnDemo_Click(object sender, EventArgs e)
        Button button = (Button)this.FindControl("btnTarget");
        button.BackColor = System.Drawing.Color.Red;

Thank you, I'm still new to C# so was trying to find something that did this. FindControl() was it and I just need to fix the loop now. Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.