locked
GridView and how to hide programmatically ImageButton? RRS feed

  • Question

  • User1478071981 posted

    Hi,

    I have a page where I would like to use a GridView (I have to show some data from a SQL DB) and show 1 to 5 ImageButtons (that the user may click).
    My goal is to show those ImageButtons only when required (there are some conditions based on the value of some fields of the SQL table) but I didn't find a valid way to do that.
    The GridView will have 6 columns for each row: the first column is a description, the other 5 is where I want to show 1 ImageButton for every "cell" if a condition is valid.
    So I may have one row with, for example, 3 ImageButtons (each one on an individual cell), another row with just one ImageButton and so on.
    When the user click the ImageButton, some other info appear on the page.
    I know how to manage the "click event" but not how to "hide" not necessary ImageButtons.
    How can I do this thing code-behind? (C#)

    Thank you, regards.

    Roberto

    Sunday, March 24, 2019 10:09 AM

Answers

  • User-943250815 posted

    There are 2 ways to do it:
    1) Set GridView DataKeyNames and use in Visible property to show/hide
    2) Use GridView.RowDatabound to change Visible property based on DataKeyNames or any other logic you need

    In this sample there are 5 buttons, Visbility of first four is based on DataKayNames, the last one will be set on GridView.RowDataBound

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="linqDSData" AllowPaging="True" DataKeyNames="CanDelete, CanImport, CanApprove, CanEmail" >
        <Columns>
          <asp:TemplateField ShowHeader="false" ItemStyle-Wrap="false" HeaderText="Actions">
            <ItemTemplate>
              <asp:ImageButton ID="ImgBtnDelete" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/trash.png" CommandName="Delete" CommandArgument='<%# Eval("RecID")%>' Visible='<%# Eval("CanDelete") %>'/>
              <asp:ImageButton ID="ImgBtnImport" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/import.png" CommandName="Import" CommandArgument='<%# Eval("RecID")%>' Visible='<%# Eval("CanImport")  %>'/>
              <asp:ImageButton ID="ImgBtnApprove" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/reload.png" CommandName="Approve" CommandArgument='<%# Eval("RecID")%>' Visible='<%# Eval("CanApprove")%>' />
              <asp:ImageButton ID="ImgBtnEmail" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/email.png" CommandName="Email" CommandArgument='<%# Eval("RecID")%>' Visible='<%# Eval("CanEmail")%>' />
              <asp:ImageButton ID="ImgBtnDocStored" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/stored.png" CommandName="Email" CommandArgument='<%# Eval("RecID")%>' />
            </ItemTemplate>
            <ItemStyle Wrap="False" />
          </asp:TemplateField>
          <asp:BoundField DataField="RecID" HeaderText="#" ReadOnly="True" SortExpression="RecID" />
        </Columns>
      </asp:GridView>

    For RowDataBound

    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
      If e.Row.RowType = DataControlRowType.DataRow Then
        Dim CanDelete As String = GridView1.DataKeys(e.Row.RowIndex).Values("CanDelete")
        Dim CanApprove As String = GridView1.DataKeys(e.Row.RowIndex).Values("CanApprove")
    
        If CanApprove = True And CanDelete = False Then
          e.Row.Cells(1).FindControl("DocStored").Visible = True
        Else
          e.Row.Cells(1).FindControl("DocStored").Visible = False
        End If
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 24, 2019 11:57 AM

All replies

  • User-943250815 posted

    There are 2 ways to do it:
    1) Set GridView DataKeyNames and use in Visible property to show/hide
    2) Use GridView.RowDatabound to change Visible property based on DataKeyNames or any other logic you need

    In this sample there are 5 buttons, Visbility of first four is based on DataKayNames, the last one will be set on GridView.RowDataBound

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="linqDSData" AllowPaging="True" DataKeyNames="CanDelete, CanImport, CanApprove, CanEmail" >
        <Columns>
          <asp:TemplateField ShowHeader="false" ItemStyle-Wrap="false" HeaderText="Actions">
            <ItemTemplate>
              <asp:ImageButton ID="ImgBtnDelete" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/trash.png" CommandName="Delete" CommandArgument='<%# Eval("RecID")%>' Visible='<%# Eval("CanDelete") %>'/>
              <asp:ImageButton ID="ImgBtnImport" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/import.png" CommandName="Import" CommandArgument='<%# Eval("RecID")%>' Visible='<%# Eval("CanImport")  %>'/>
              <asp:ImageButton ID="ImgBtnApprove" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/reload.png" CommandName="Approve" CommandArgument='<%# Eval("RecID")%>' Visible='<%# Eval("CanApprove")%>' />
              <asp:ImageButton ID="ImgBtnEmail" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/email.png" CommandName="Email" CommandArgument='<%# Eval("RecID")%>' Visible='<%# Eval("CanEmail")%>' />
              <asp:ImageButton ID="ImgBtnDocStored" runat="server" CausesValidation="False" ImageUrl="~/CDN/img/stored.png" CommandName="Email" CommandArgument='<%# Eval("RecID")%>' />
            </ItemTemplate>
            <ItemStyle Wrap="False" />
          </asp:TemplateField>
          <asp:BoundField DataField="RecID" HeaderText="#" ReadOnly="True" SortExpression="RecID" />
        </Columns>
      </asp:GridView>

    For RowDataBound

    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
      If e.Row.RowType = DataControlRowType.DataRow Then
        Dim CanDelete As String = GridView1.DataKeys(e.Row.RowIndex).Values("CanDelete")
        Dim CanApprove As String = GridView1.DataKeys(e.Row.RowIndex).Values("CanApprove")
    
        If CanApprove = True And CanDelete = False Then
          e.Row.Cells(1).FindControl("DocStored").Visible = True
        Else
          e.Row.Cells(1).FindControl("DocStored").Visible = False
        End If
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 24, 2019 11:57 AM
  • User1478071981 posted

    Hi jzero,

    thanks a lot !
    Your example helped me a lot !
    Best regards,

    Roberto

    Sunday, March 24, 2019 1:09 PM