CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2013
    Posts
    1

    Question How to hide button in GridView?

    Hello all!

    Simple question as always, but i can't find question.

    I need to hide some Button, but it doesn't work.

    ASPX:

    <asp:ButtonField buttontype="Button" commandname="AddCustomValue" headertext="" Text="ButtonText" />

    ASPX.CS:

    foreach (GridViewRow gr in gvQuota.Rows)
    {

    Button buttonFromGV= (Button )gr.FindControl("TextCustomValue");
    buttonFromGV.Visible = false;
    }


    What i'm doing wrong?

    Thanks a lot!

  2. #2
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: How to hide button in GridView?

    You cannot access the button control rendered by the ButtonField in this way. There are 2 ways you can do this. Both involve using a TemplateField;

    Declaratively bind the visible property of the button to a field in your datasource like this:
    Code:
    <asp:GridView runat="server" ID="DemoGrid" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button id="buttonFromGV" runat="server" Text="ButtonText" CommandName="AddCustomValue" Visible='<%# (bool)Eval("ShowButton")) %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    Procedurally adjust the property like you were trying to do. Use the RowDataBound event of the grid:

    Code:
    protected void DemoGrid_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            if (condition) 
            {
                Button buttonFromGV = (Button)e.Row.FindControl("buttonFromGV");
                buttonFromGV.Visible = false;
            }
        }   
    }
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured