Christoph Brückmann
Christoph Brückmann

Reputation: 1373

How to edit the LinkButton visibility within a GridViewEdit in c#

I wonder how to edit a the visibility of a LinkButton within a GridViewEdit:

I have got a LinkButton named "lbtnActivateConfig" in the ItemTemplate of a GridView in my aspx-File:

<ItemTemplate>
  <asp:LinkButton ID="lbtnActivateConfig" runat="server"
       OnClick="GridViewDeactivate" Visible="false">Deaktivieren
  </asp:LinkButton>
</ItemTemplate>

Now I want to change the visibility of the LinkButton insite this method:

protected void GridViewEdit(object sender, GridViewEditEventArgs e)
    {
        GridViewRow row = this.ConfigGridView.Rows[e.NewEditIndex];

        LinkButton buttonActivate = (LinkButton)ConfigGridView.Rows[row.RowIndex].FindControl("lbtnActivateConfig");
        buttonActivate.Visible = true;
    }

While debugging it catches the LinkButton an seems to set it's visibility. But the LinkButton is still invisible.

It's the same if I change the button to visible inside the aspx-File and try to change it in the method.

For me it seems that the aspx-File is always executed at last and overwrites the changes in the method. Is this right? How can I change the visibility inside the method? Any ideas?

Thank you and Goodbye!

Upvotes: 1

Views: 1585

Answers (1)

Devin Burke
Devin Burke

Reputation: 13820

Try the following:

protected void GridViewEdit(object sender, GridViewEditEventArgs e)
{
    GridViewRow row = this.ConfigGridView.Rows[e.NewEditIndex];
    (ConfigGridView.Rows[row.RowIndex].FindControl("lbtnActivateConfig")).Visible = true;      
} 

You don't need to cast it as LinkButton because all controls have the Visible property. And when you set FindControl to a variable, the variable is set by value rather than by reference which means you aren't referring to the actual control.

Upvotes: 1

Related Questions