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"/
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
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.
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?
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.
Shaun Kline
No response from asker, and no other experts submitted solutions. The provided solutions do address the initial and follow on questions.