Click here to Skip to main content
15,946,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm trying to Hide a button inside gvResults (gridview) based on the value of another control inside the gridview.

both controls are templatefield.

VB
Protected Sub gvResults_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim SPass As String = TryCast(e.Row.FindControl("lblSharePass"), Label).Text
            Dim ShareButton As Button = TryCast(e.Row.FindControl("btnShare"), Button)

            If Not String.IsNullOrEmpty(SPass) Then
                ShareButton.Visible = False
            Else
                ShareButton.Visible = True
            End If
        End If

    End Sub


lblSharePass is binded to a column in the database
ASP.NET
<asp:Label ID="lblSharePass" runat="server" Text='<%#Bind("SharePass") %>'></asp:Label>


I can't seem to get it right. btnShare should not be visible if lblSharePass contains a value.. However, I tried to specify the value like SPass = "1234" and it works..

Can someone please help.

What I have tried:

i tried isnothing, "", vbnullstring but non of them works. I also created the same sub in RowCreated but with unsuccessful results.
Posted
Updated 1-Sep-16 3:12am

1 solution

You can bind the Visible property in the markup, without using the RowDataBound event:
ASP.NET
<asp:Button ID="btnShare" runat="server" 
    Visible='<%# String.IsNullOrWhiteSpace(Eval("SharePass", "{0}")) %>'
    ...
/>

(Using IsNullOrWhiteSpace will handle cases where the value is a string that only contains whitespace characters. This would look empty in HTML, but would not pass IsNullOrEmpty, and would not be equal to an empty string.)
 
Share this answer
 
Comments
Cute Girly Geek 1-Sep-16 22:04pm    
Thank you R.D. It worked.... whew!!! Thanks again! :*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900