troubleshooting Question

Make a panel appear and disappear using a checkbox.  And do validation on the controls if visible

Avatar of cdlciddit
cdlciddit asked on
ASP.NETVisual Basic.NET.NET Programming
13 Comments4 Solutions219 ViewsLast Modified:
Hello. I would like to make a "same as mailing address" section on a form.  If the checkbox labeled "Address same as client" is checked then the panel with the textboxes to record address information will disappear and the person can submit the form without filling out the information.  However, if that box is not checked the the panel with the textboxes to record address information should be visible and the user should be forced to fill out that information before the form can be submitted.  I know how to do validation but not in this situation.  Here is the code I have so far.  

This is the code for the checkbox:
Mailing Address:<asp:CheckBox ID="chkAddrSameAsClient" runat="server" Text="Address same as client" AutoPostBack="true" OnCheckedChanged="chkAddrSameAsClient_CheckedChanged"/

Here is the panel code for the textboxes that will record address information:
<asp:Panel ID="pnlGuarantorAddress" runat="server">
           <table style="width: 100%;">
                       <asp:TextBox ID="txtGuarantorAddress" runat="server" Width="400px">Street Address</asp:TextBox>
                       <asp:RequiredFieldValidator ID="txtGuarantorAddressValidator1" runat="server" ControlToValidate="txtGuarantorAddress" Display="Dynamic" ErrorMessage="Please enter guarantor's address"></asp:RequiredFieldValidator>
                       <asp:TextBox ID="txtGuarantorCity" runat="server">City</asp:TextBox>
                       &nbsp;<asp:DropDownList ID="dlGuarantorState" runat="server">
                           <asp:ListItem Value="AK">Alaska</asp:ListItem>
                           <asp:ListItem value="AL">Alabama</asp:ListItem>
                           <asp:ListItem value="AR">Arkansas</asp:ListItem>
                           <asp:ListItem value="AZ">Arizona</asp:ListItem>
                           <asp:ListItem value="CA">California</asp:ListItem>
                           <asp:ListItem value="CO">Colorado</asp:ListItem>
                           <asp:ListItem value="CT">Connecticut</asp:ListItem>
                           <asp:ListItem value="DC">Washington D.C.</asp:ListItem>
                           <asp:ListItem value="DE">Delaware</asp:ListItem>
                           <asp:ListItem value="FL">Florida</asp:ListItem>
                           <asp:ListItem value="GA">Georgia</asp:ListItem>
                           <asp:ListItem value="HI">Hawaii</asp:ListItem>
                           <asp:ListItem value="IA">Iowa</asp:ListItem>
                           <asp:ListItem value="ID">Idaho</asp:ListItem>
                           <asp:ListItem value="IL">Illinois</asp:ListItem>
                           <asp:ListItem value="IN">Indiana</asp:ListItem>
                           <asp:ListItem value="KS">Kansas</asp:ListItem>
                           <asp:ListItem value="KY">Kentucky</asp:ListItem>
                           <asp:ListItem value="LA">Louisiana</asp:ListItem>
                           <asp:ListItem value="MA">Massachusetts</asp:ListItem>
                           <asp:ListItem value="MD">Maryland</asp:ListItem>
                           <asp:ListItem value="ME">Maine</asp:ListItem>
                           <asp:ListItem value="MI">Michigan</asp:ListItem>
                           <asp:ListItem value="MN">Minnesota</asp:ListItem>
                           <asp:ListItem value="MO">Missourri</asp:ListItem>
                           <asp:ListItem value="MS">Mississippi</asp:ListItem>
                           <asp:ListItem value="MT">Montana</asp:ListItem>
                           <asp:ListItem value="NC">North Carolina</asp:ListItem>
                           <asp:ListItem value="ND">North Dakota</asp:ListItem>
                           <asp:ListItem value="NE">Nebraska</asp:ListItem>
                           <asp:ListItem value="NH">New Hampshire</asp:ListItem>
                           <asp:ListItem value="NJ">New Jersey</asp:ListItem>
                           <asp:ListItem value="NM">New Mexico</asp:ListItem>
                           <asp:ListItem value="NV">Nevada</asp:ListItem>
                           <asp:ListItem value="NY">New York</asp:ListItem>
                           <asp:ListItem value="OH">Ohio</asp:ListItem>
                           <asp:ListItem value="OK">Oklahoma</asp:ListItem>
                           <asp:ListItem value="OR">Oregon</asp:ListItem>
                           <asp:ListItem value="PA">Pennsylvania</asp:ListItem>
                           <asp:ListItem value="PR">Puerto Rico</asp:ListItem>
                           <asp:ListItem value="RI">Rhode Island</asp:ListItem>
                           <asp:ListItem value="SC">South Carolina</asp:ListItem>
                           <asp:ListItem value="SD">South Dakota</asp:ListItem>
                           <asp:ListItem value="TN">Tennessee</asp:ListItem>
                           <asp:ListItem value="TX">Texas</asp:ListItem>
                           <asp:ListItem value="UT">Utah</asp:ListItem>
                           <asp:ListItem value="VA">Virginia</asp:ListItem>
                           <asp:ListItem value="VT">Vermont</asp:ListItem>
                           <asp:ListItem value="WA">Washington</asp:ListItem>
                           <asp:ListItem value="WI">Wisconsin</asp:ListItem>
                           <asp:ListItem value="WV">West Virginia</asp:ListItem>
                           <asp:ListItem value="WY">Wyoming</asp:ListItem>
                       &nbsp;<asp:TextBox ID="txtGuarantorZip" runat="server">Zip Code</asp:TextBox>

When the box is checked I thought I could run some code like this to make the panel visible or not visible.  This is the chkAddrSameAsClient_CheckedChanged function that is supposed to fire each time the checkbox is clicked on. Or at least this is what was trying to do:
Protected Sub chkAddrSameAsClient_CheckedChanged(sender As Object, e As EventArgs)
        Response.Write("same address checked: " & chkAddrSameAsClient.Checked)
        If (chkAddrSameAsClient.Checked = "True") Then
            pnlGuarantorAddress.Visible = True
            pnlGuarantorAddress.Visible = False
        End If
    End Sub

When I added this the panel would appear and disappear each time the checkbox was clicked.  It didn't matter what the value was.  I want the panel's visibility to be false if the checkbox value is "True".  And vise versa if the checkbox value is "False" .  But for some reason that seems to be ignored and the panel appears and disappears on clicks no matter the value of the checkbox.   So I also added some code that I thought would check the value of the checkbox each time I did a autopostback.  But that didn't work either but here it is.  

 If (chkAddrSameAsClient.Text = "False") Then
            pnlGuarantorAddress.Visible = True
        End If
Shaun Kline
Lead Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 4 Answers and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros