使用代码隐藏编码使ASP.Net命令按钮不可见或隐藏

时间:2012-12-17 15:33:47

标签: asp.net vb.net button gridview code-behind

我们在带有GridView的ASP.Net Web表单上有一个命令按钮,用户可以将GridView中显示的数据作为电子邮件发送。

在这个GridView上是一个“选择”命令按钮,当用户点击图像按钮并在发送电子邮件时再次显示按钮时,我们希望暂时从GridView中删除它。

我们想删除该按钮,因为它显示在我们不希望包含在电子邮件中的电子邮件中呈现。

你能告诉我如何在没有按钮的情况下刷新GridView的代码隐藏文件中使用编码吗?

这是一个显示按钮的标记:

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

这是我们使用的编码,它发送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

评论部分显示了我们想要添加编码以隐藏和再次显示按钮的位置。

1 个答案:

答案 0 :(得分:3)

也许你可以隐藏专栏:

GridViewSummary.Columns(11).Visible = False

然后:

GridViewSummary.Columns(11).Visible = True