?
Solved

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

Posted on 2011-10-20
10
Medium Priority
?
1,123 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:megnin
  • 5
  • 4
10 Comments
 
LVL 16

Expert Comment

by:MikeMCSD
ID: 37004046
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
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37005269
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?
0
 
LVL 1

Author Comment

by:megnin
ID: 37024658
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37027587
You can try

 Checked='<%# IIf(Eval("SeeSuccessCoach").ToString() = "True", True, False) %>'
0
 
LVL 1

Author Comment

by:megnin
ID: 37027628
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.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 37027639
Simply change to

Enabled='<%# IIf(Eval("SeeSuccessCoach").ToString() = "True", True, False) %>'

or

Visible='<%# IIf(Eval("SeeSuccessCoach").ToString() = "True", True, False) %>'
0
 
LVL 1

Author Comment

by:megnin
ID: 37027720
Mmmmm, yes it does seem rather simple now.  ;-)

Let me see if I can stick it in there and get it to work...
0
 
LVL 1

Author Comment

by:megnin
ID: 37027889
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!
0
 
LVL 1

Author Closing Comment

by:megnin
ID: 37027910
Thank you again.  The perfect one-line solution.  I like that it is right in the GridView and no code behind was necessary.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37027917
Glad to help :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question