Link to home
Start Free TrialLog in
Avatar of cdlciddit
cdlciddit

asked on

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

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"/

Open in new window


Here is the panel code for the textboxes that will record address information:
<asp:Panel ID="pnlGuarantorAddress" runat="server">
           <table style="width: 100%;">
                    <tr>
                   <td>
                       <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>
                   </td>
                   <td>&nbsp;</td>
               </tr>
               <tr>
                   <td>
                       <asp:TextBox ID="txtGuarantorCity" runat="server">City</asp:TextBox>
                       &nbsp;<asp:DropDownList ID="dlGuarantorState" runat="server">
                           <asp:ListItem>State</asp:ListItem>
                           <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>
                       </asp:DropDownList>
                       &nbsp;<asp:TextBox ID="txtGuarantorZip" runat="server">Zip Code</asp:TextBox>
                   </td>
                   <td>&nbsp;</td>
               </tr>
           </table>
        </asp:Panel>

Open in new window


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
        Else
            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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America 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
Avatar of cdlciddit
cdlciddit

ASKER

Ahhhh.  Yes. You are right.  Let me try that. I was taking the string literal of it b'c when I do a response.write I get the text True or False.  So I was trying to compare the .text property.  That is the first place I went wrong. Thanks.  I will report back.
SOLUTION
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
So instead of me checking it like this:
If (chkAddrSameAsClient.Checked = "True") Then
            pnlGuarantorAddress.Visible = True
        Else
            pnlGuarantorAddress.Visible = False
        End If

Open in new window


Should I do it like this?
If (chkAddrSameAsClient.Checked) Then
            pnlGuarantorAddress.Visible = True
        Else
            pnlGuarantorAddress.Visible = False
        End If

Open in new window


I'm asking b'c I'm not sure how to check boolean values of a checkbox in vb.net.
Ok. Thanks. I'll try that.
If you are only changing the visibility of the panel, you do not need to do the IF/Else/End IF, just do:

pnlGuarantorAddress.Visible = Not chkAddrSameAsClient.Checked
Ok.  Do I do that in the chkAddrSameAsClient_CheckedChanged function? And just get rid of the IF/ELSE/END IF?
SOLUTION
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
I did get rid of the IF/ELSE/END IF and that worked for the panel.  Now How can I do validation if the panel is visible but not validate if the panel is not visible?
That worked. Thank you. I wasn't aware how to do booleans in vb.net.
SOLUTION
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
wow.  ok.  Yes. I am using .Net RequiredValidators.   I will try to look up how to enable/disable them in the check event.  I guess I can google that phrase.  Never heard of a validation group.  I will look into that also.  Because I am having that  problem on some of the other validators I have on that page.
No response from asker, and no other experts submitted solutions. The provided solutions do address the initial and follow on questions.