Member Avatar for kyeong

Hello,

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.

Thanks.

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:

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

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.