英文:
Hide or show certain form elements based on Checkbox value
问题
以下是您要翻译的内容:
I have two form elements separated by table row (tr).
The first <tr id="installCityStateZip" runat="server">
contains install address information while the second contains mailing address information <tr id="mailCityStateZip" runat="server" runat="server">
.
There is a field name called InstallSameAsMailing, a BIT datatype in sql server database.
If install address is same as mailing address, InstallSameAsMailing has a value of 1.
If install address is not same as mailing address, the value of InstallSameAsMailing is either 0 or null.
Our requirement is that if InstallSameAsMailing = true (the value is 1) then hide everything under <tr id="mailCityStateZip" runat="server" runat="server">
and show everything under <tr id="installCityStateZip" runat="server">
.
The code below is not working for me. When I run it, it is not hiding <tr id="mailCityStateZip" runat="server" runat="server">
and everything inside it.
//Markup
<tr id="installCityStateZip" runat="server">
<td><asp:DropDownList ID="city" OnSelectedIndexChanged="cityChanged" AutoPostBack="true" runat="server" /></td>
<td><asp:TextBox ID="state" runat="server" Text="WI" ReadOnly="true" /></td>
<td><asp:DropDownList ID="zip" AppendDataBoundItems="true" runat="server"><asp:ListItem Text="--Select--" Value="" /></asp:DropDownList></td>
<td><asp:TextBox ID="AccountNo" runat="server" /></td>
</tr>
<tr id="mailCityStateZip" runat="server">
<td><asp:TextBox ID="mailCity" runat="server" /></td>
<td><asp:TextBox ID="mailState" runat="server" /></td>
<td><asp:TextBox ID="mailZip" runat="server" /></td>
<td><asp:TextBox ID="AccountNumber" runat="server" /></td>
</tr>
//VB
Dim myConnection As New SqlConnection(conString)
Dim sqlStatement As String = "Select * FROM Addresses Where AUTOID=@addressid"
myConnection.Open()
Dim sqlCmd2 As SqlCommand = New SqlCommand(sqlStatement, myConnection)
sqlCmd2.Parameters.Add("@addressid", SqlDbType.Int).Value = addressID
Dim reader As SqlDataReader = sqlCmd2.ExecuteReader()
If reader.Read() Then
streetAddress.Text = reader("InstallAddress").ToString()
city.Text = reader("InstallCity").ToString()
state.Text = reader("InstallState").ToString()
If zip.Items.FindByText(reader.ToString()) IsNot Nothing Then
zip.Text = reader("InstallZip").ToString()
End If
yearBuilt.Text = reader("YearBuilt").ToString()
waterAccountNo.Text = reader("WaterAcctNo").ToString()
Dim sameAddress As Boolean = reader("InstallSameAsMailing").ToString()
If sameAddress = True Then
installCityStateZip.Visible = True
mailCityStateZip.Visible = True
Else
installCityStateZip.Visible = True
mailCityStateZip.Visible = False
End If
End If
**UPDATE:** The following code seems to be working:
If reader("InstallSameAsMailing").Value.ToString() = "Yes" Then
installCityStateZip.Visible = True
mailCityStateZip.Visible = True
IsInstallation.Checked = True
IsMailing.Checked = True
Else
installCityStateZip.Visible = True
mailCityStateZip.Visible = False
IsInstallation.Checked = True
IsMailing.Checked = False
End If
Notice the two form fields added, IsInstallation and IsMailing.
If it is install is same as mailing, check both the IsInstallation and IsMailing checkboxes.
Otherwise, check the IsInstallation checkbox only.
So, our tests consistently show that whether whether or not install address is same as mailing address, only the IsInstall checkbox is checked.
Thanks for your assistance.
<details>
<summary>英文:</summary>
I have two form elements separated by table row (tr).
The first `<tr id="installCityStateZip" runat="server">` contains install address information while the second contains mailing address information `<tr id="mailCityStateZip" runat="server" runat="server">`.
There is a field name called InstallSameAsMailing, a BIT datatype in sql server database.
If install address is same as mailing address, InstallSameAsMailing has a value of 1.
If install address is not same as mailing address, the value of InstallSameAsMailing is either 0 or null.
Our requirement is that if InstallSameAsMailing = true (the value is 1) then hide everything under `<tr id="mailCityStateZip" runat="server" runat="server">` and show everything under `<tr id="installCityStateZip" runat="server">`.
The code below is not working for me. When I run it, it is not hiding `<tr id="mailCityStateZip" runat="server" runat="server">` and everything inside it.
Any ideas what I am doing wrong?
'//Markup
<tr id="installCityStateZip" runat="server">
<td><asp:DropDownList ID="city" OnSelectedIndexChanged="cityChanged" AutoPostBack="true" runat="server" /></td>
<td><asp:TextBox ID="state" runat="server" Text="WI" ReadOnly="true" /></td>
<td><asp:DropDownList ID="zip" AppendDataBoundItems="true" runat="server"><asp:ListItem Text="--Select--" Value="" /></asp:DropDownList></td>
<td><asp:TextBox ID="AccountNo" runat="server" /></td>
</tr>
<tr id="mailCityStateZip" runat="server">
<td><asp:TextBox ID="mailCity" runat="server" /></td>
<td><asp:TextBox ID="mailState" runat="server" /></td>
<td><asp:TextBox ID="mailZip" runat="server" /></td>
<td><asp:TextBox ID="AccountNumber" runat="server" /></td>
</tr>
'//VB
Dim myConnection As New SqlConnection(conString)
Dim sqlStatement As String = "Select * FROM Addresses Where AUTOID=@addressid"
myConnection.Open()
Dim sqlCmd2 As SqlCommand = New SqlCommand(sqlStatement, myConnection)
sqlCmd2.Parameters.Add("@addressid", SqlDbType.Int).Value = addressID
Dim reader As SqlDataReader = sqlCmd2.ExecuteReader()
If reader.Read() Then
streetAddress.Text = reader("InstallAddress").ToString()
city.Text = reader("InstallCity").ToString()
state.Text = reader("InstallState").ToString()
If zip.Items.FindByText(reader.ToString()) IsNot Nothing Then
zip.Text = reader("InstallZip").ToString()
End If
yearBuilt.Text = reader("YearBuilt").ToString()
waterAccountNo.Text = reader("WaterAcctNo").ToString()
Dim sameAddress As Boolean = reader("InstallSameAsMailing").ToString()
If sameAddress = True Then
installCityStateZip.Visible = True
mailCityStateZip.Visible = True
Else
installCityStateZip.Visible = True
mailCityStateZip.Visible = False
End If
End If
**UPDATE:** The following code seems to be working:
If reader("InstallSameAsMailing").Value.ToString() = "Yes" Then
installCityStateZip.Visible = True
mailCityStateZip.Visible = True
IsInstallation.Checked = True
IsMailing.Checked = True
Else
installCityStateZip.Visible = True
mailCityStateZip.Visible = False
IsInstallation.Checked = True
IsMailing.Checked = False
End If
Notice the two form fields added, IsInstallation and IsMailing.
If it is install is same as mailing, check both the IsInstallation and IsMailing checkboxes.
Otherwise, check the IsInstallation checkbox only.
So, our tests consistently show that whether whether or not install address is same as mailing address, only the IsInstall checkbox is checked.
Thanks for your assistance.
</details>
# 答案1
**得分**: 0
Ok, this worked perfectly for me. I hope it helps someone else some day.
如果不是 DBNull 值且转换为布尔值的话,IsInstallation 可见,installation 已选中,mailCityStateZip 不可见,IsMailing 已选中。否则,installCityStateZip 可见,IsInstallation 已选中,mailCityStateZip 不可见,IsMailing 未选中。
<details>
<summary>英文:</summary>
Ok, this worked perfectly for me. I hope it helps someone else some day.
If Not reader("InstallSameAsMailing") Is DBNull.Value AndAlso Convert.ToBoolean(reader("InstallSameAsMailing")) Then
IsInstallation.Visible = True
installation.Checked = True
mailCityStateZip.Visible = False
IsMailing.Checked = True
Else
installCityStateZip.Visible = True
IsInstallation.Checked = True
mailCityStateZip.Visible = False
IsMailing.Checked = False
End If
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论