0

When I click on a row in the gridview I want to change the Visible property of just the button in that row to true (I want all the other buttons in the other rows to remain invisible). With my current code when I click on a row, the button in every row becomes visible. I'm trying to make this change within the c# code behind the aspx.

This is the current SelectedIndexChanged method:

protected void gridviewAnnouncement_SelectedIndexChanged(object sender, EventArgs e) {

this.gridviewAnnouncement.Rows[gridviewAnnouncement.SelectedIndex].Cells[1].Text = getHeaderAndBody();

            foreach (GridViewRow row in gridviewAnnouncement.Rows)
            {
                Button btn = row.FindControl("btnInterested") as Button;
                btn.Visible = true;
            }
}

This is the aspx code for the gridview and button:

<asp:GridView ID="gridviewAnnouncement" runat="server" AutoGenerateColumns="false" 
                        AutoGenerateSelectButton="true" OnSelectedIndexChanged="gridviewAnnouncement_SelectedIndexChanged" 
                        GridLines="Horizontal" BorderStyle="Solid">
                        <Columns>
                            <asp:BoundField DataField="Header" HeaderText="Announcement" HtmlEncode="false" /> 
                            <asp:TemplateField ShowHeader="false">
                                <ItemTemplate>
                                    <asp:Button runat="server" ID="btnInterested" Text="Interested" 
                                        OnClick="btnInterested_Click" Visible="false"/>
                                </ItemTemplate>
                                </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

Here are two screenshots of what currently happens:

Before 'Details' is clicked

After 'Details' is clicked on just one row

The only thing I'm trying to fix is having only the button show up for the selected row, not all of them. Thanks to anyone who can help!

1 Answer 1

1

but why do you use a foreach when you only want to operate on the ONE row?

And I assume that you can't just click anywhere on that row, but are clicking on the details button, since your selected index would not trigger, then right?

so,

GridViewRow gvR = GridView1.SelectedRow;

Button btn = gvR.FindControl("btnInterested") as Button;
btn.Visible = true;

So, you are using a foreach - and that will operate on all rows in the grid.

You can get the current row in SelectedIndexChange as per above, and then operate ONLY on the one row.

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

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.