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:
Here is the panel code for the textboxes that will record address information:
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_Checke dChanged 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_Checke dChanged(s ender As Object, e As EventArgs)
Response.Write("same address checked: " & chkAddrSameAsClient.Checke d)
If (chkAddrSameAsClient.Check ed = "True") Then
pnlGuarantorAddress.Visibl e = True
Else
pnlGuarantorAddress.Visibl e = 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.
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%;">
<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> </td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtGuarantorCity" runat="server">City</asp:TextBox>
<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>
<asp:TextBox ID="txtGuarantorZip" runat="server">Zip Code</asp:TextBox>
</td>
<td> </td>
</tr>
</table>
</asp:Panel>
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_Checke
Protected Sub chkAddrSameAsClient_Checke
Response.Write("same address checked: " & chkAddrSameAsClient.Checke
If (chkAddrSameAsClient.Check
pnlGuarantorAddress.Visibl
Else
pnlGuarantorAddress.Visibl
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So instead of me checking it like this:
Should I do it like this?
I'm asking b'c I'm not sure how to check boolean values of a checkbox in vb.net.
If (chkAddrSameAsClient.Checked = "True") Then
pnlGuarantorAddress.Visible = True
Else
pnlGuarantorAddress.Visible = False
End If
Should I do it like this?
If (chkAddrSameAsClient.Checked) Then
pnlGuarantorAddress.Visible = True
Else
pnlGuarantorAddress.Visible = False
End If
I'm asking b'c I'm not sure how to check boolean values of a checkbox in vb.net.
ASKER
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.Visibl e = Not chkAddrSameAsClient.Checke d
pnlGuarantorAddress.Visibl
ASKER
Ok. Do I do that in the chkAddrSameAsClient_Checke dChanged function? And just get rid of the IF/ELSE/END IF?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
ASKER
That worked. Thank you. I wasn't aware how to do booleans in vb.net.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER