Search code examples
asp.netvb.netbuttongridviewcode-behind

Making an ASP.Net command button invisible or hidden with code-behind coding


We have a command button on an ASP.Net web form with a GridView that the user sends the data displayed in the GridView as an email.

On this GridView is a "Select" command button that we would like to temporaraly remove from the GridView when the user clicks on an Image Button and have the button appear again when the email has been sent.

We want to remove the button because it shows up rendered in the email which we don't want included in the email.

Can you tell me how to use coding in a code-behind file that will refresh the GridView without the button?

Here is some markup showing the button:

                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:Button 
                            ID="ButtonSelect" 
                            runat="server" 
                            CausesValidation="False" 
                            CommandName="Select" 
                            Text="Select Schedule Item Details" />
                    </ItemTemplate>

This is the coding we are using that sends out the email of the GridView:

Protected Sub ImageButtonEmailThisList_Click(sender As Object, e As ImageClickEventArgs)

    ' Get the rendered HTML.
    '-----------------------
    Dim SB As New StringBuilder()
    Dim SW As New StringWriter(SB)
    Dim htmlTW As New HtmlTextWriter(SW)

    ' Remove the select button for a short while.
    '--------------------------------------------

    GridViewSummary.RenderControl(htmlTW)

    ' Get the HTML into a string.
    ' This will be used in the body of the email report.
    '---------------------------------------------------
    Dim dataGridHTML As String = SB.ToString()
    Dim SmtpServer As New SmtpClient()

    ObjMailMessage = New MailMessage()

    Try
        With ObjMailMessage
            .To.Add(New MailAddress(TextBoxEmailRecipient.Text))
            .Subject = "Knowledge Academy Teacher's Schdule"
            .Body = dataGridHTML
            .IsBodyHtml = True
            .DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
        End With

        SmtpServer.Send(ObjMailMessage)

        LabelEmailMessage.Text = "<i>Email sent to " & TextBoxEmailRecipient.Text & "!</i>"

        ImageButtonEmailThisList.Visible = True

    Catch ex As Exception
        MsgBox(ex.ToString())

    Finally

        ' Refresh the GridView with select button back in place.
        '-------------------------------------------------------

    End Try

End Sub

The commented sections show where we would like to add the coding to hide and show the button again.


Solution

  • Maybe you can hide the column :

    GridViewSummary.Columns(11).Visible = False
    

    And then :

    GridViewSummary.Columns(11).Visible = True