Link to home
Start Free TrialLog in
Avatar of David Megnin
David MegninFlag for United States of America

asked on

Hide/Show or Enable/Disable a Checkbox in a GridView based on value of the TextBox next to it.

Hi Experts,

I've got a GridView in an ASP.Net / VB page and I'd like to hide/show or or disable/enable a CheckBox in the eleventh column (index 10) based on the value of a TextBox in the twelvth column (index 11).  If the TextBox in column index 11 contains "False" I need the CheckBox in column index 10 to be hidden or disabled.

I added the CheckBox to the ItemTemplate of the GridView and tweaked it in the code behind to update the database as soon as it's checked.  That may not matter for this problem, just thought I'd mention it in case.
                <asp:TemplateField HeaderText="SC" SortExpression="SCServed" ConvertEmptyStringToNull="False">
                    <ItemTemplate>
                        <asp:CheckBox ID="cbSCServed" runat="server" AutoPostBack="True" OnCheckedChanged="cbSCServed_CheckedChanged"
                            Checked='<%# Convert.ToBoolean(Eval("SCServed")) %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SeeSC" SortExpression="SeeSuccessCoach">
                    <EditItemTemplate>
                        <asp:TextBox ID="SeeSuccessCoach" runat="server" Text='<%# Bind("SeeSuccessCoach") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <%#IIf(Eval("SeeSuccessCoach").ToString() = "True", "&clubs;", "")%>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" ForeColor="Black" />
                </asp:TemplateField>

Open in new window


This may not matter for this problem.  I just thought I'd mention it in case:
    Protected Sub cbSCServed_CheckedChanged(sender As Object, e As System.EventArgs)
        'Using Checkbox in ASP.NET GridView Control Tutorial:
        'http://www.ezzylearning.com/tutorial.aspx?tid=5187857
        Dim cbSCServed As CheckBox = DirectCast(sender, CheckBox)
        Dim row As GridViewRow = DirectCast(cbSCServed.NamingContainer, GridViewRow)

        Dim cid As String = row.Cells(1).Text
        Dim status As Boolean = cbSCServed.Checked

        Dim query As String = "UPDATE Customers SET SCServed = @SCServed, SCUser = @SCUser, SCDateTime = getdate() WHERE CustomerID = @CustomerID"

        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("OneStopSignInConnectionString").ConnectionString)
        Dim com As New SqlCommand(query, con)

        Try
            com.Parameters.Add("@SCServed", SqlDbType.Bit).Value = status
            com.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = cid
            com.Parameters.Add("@SCUser", SqlDbType.VarChar).Value = SCUser  'Database field, "SCUser" = the person checking off the first service provided.
            'com.Parameters.Add("@SCDateTime", SqlDbType.VarChar).Value = SCDateTime  'Database field, "SCDateTime" = Time Stamp of the person checking off the first service provided.

            con.Open()
            com.ExecuteNonQuery()
            con.Close()

            ' LoadData()
        Catch exp As Exception
            Response.Write(exp)
            'Server.Transfer("Error.htm")
        End Try
    End Sub

Open in new window

Avatar of MikeMCSD
MikeMCSD
Flag of United States of America image

You can use the "ItemDataBound" event to hide the check box.
This code is from the older DataGrid and not a GridView so you made need to change it a bit :


Private Sub grid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grid.ItemDataBound

            Dim cbSCServed As Checkbox
            cbSCServed= e.Item.Cells(11).FindControl("cbSCServed")

         If DataBinder.Eval(e.Item.DataItem, "SeeSuccessCoach") Then  
               cbSCServed.Visible = True
         Else
               cbSCServed.Visible = False
     
        End If

If your textbox field is not boolean use this instead :

            If DataBinder.Eval(e.Item.DataItem, "SeeSuccessCoach".ToString) = "True" Then
Avatar of Nasir Razzaq
Are you talking about this checkbox?

<asp:CheckBox ID="cbSCServed" runat="server" AutoPostBack="True" OnCheckedChanged="cbSCServed_CheckedChanged"
                            Checked='<%# Convert.ToBoolean(Eval("SCServed")) %>' />

Is it not working already?
Avatar of David Megnin

ASKER

Sorry for the delay... Our COLO lost power almost all day yesterday.  Happy Monday!

Anyway, yes that's the checkbox that I'd like to only be visible or enabled if this TextBox contains "True":
                    <EditItemTemplate>
                        <asp:TextBox ID="SeeSuccessCoach" runat="server" Text='<%# Bind("SeeSuccessCoach") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <%#IIf(Eval("SeeSuccessCoach").ToString() = "True", "&clubs;", "")%>
                    </ItemTemplate>

Open in new window


If you notice the logic in the <ItemTemplate>, If the TextBox contains "True", the field displays a "Clubs" character.  That's the same logic I need applied to the CheckBox.

The TextBox is in the <EditItemTemplate>.  If it contains "True" then the <ItemTemplate> of the same field will display the "Clubs" character.
The CheckBox in the column next to it should then also be enabled.  
Otherwise the ItemTemplate displays nothing and the CheckBox should be either hidden or just disabled.
You can try

 Checked='<%# IIf(Eval("SeeSuccessCoach").ToString() = "True", True, False) %>'
I don't want it checked or not checked based on "SeeSuccessCoach", I want it Enabled or Disabled (shown or hidden okay too) based on "SeeSuccessCoach" so that if "SeeSuccessCoach" is True, then the CheckBox can be checked or not.  

If SeeSuccessCoach is True then the CheckBox is an option to be checked or not.
If SeeSuccessCoach is not True then there is no option (hide or disable the checkbox).

Yeah, sometimes the requirements we are given for a tool are logical nightmares.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mmmmm, yes it does seem rather simple now.  ;-)

Let me see if I can stick it in there and get it to work...
Sweet!!!!  That did exactly what I needed.  I used the "Visible=...."

Now the check boxes only show up where there is a "Clubs" showing that the person is there to "See a Success Coach".  The check box is for the Success Coach to mark when the person has been seen.  If the person is not there to see a Success Coach, then I didn't want there to be a check box at all.

That worked perfectly!
Thank you again.  The perfect one-line solution.  I like that it is right in the GridView and no code behind was necessary.
Glad to help :-)