根据复选框值隐藏或显示特定表单元素。

huangapple go评论143阅读模式
英文:

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 `&lt;tr id=&quot;installCityStateZip&quot; runat=&quot;server&quot;&gt;` contains install address information while the second contains mailing address information `&lt;tr id=&quot;mailCityStateZip&quot; runat=&quot;server&quot; runat=&quot;server&quot;&gt;`.

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 `&lt;tr id=&quot;mailCityStateZip&quot; runat=&quot;server&quot; runat=&quot;server&quot;&gt;` and show everything under `&lt;tr id=&quot;installCityStateZip&quot; runat=&quot;server&quot;&gt;`.

The code below is not working for me. When I run it, it is not hiding `&lt;tr id=&quot;mailCityStateZip&quot; runat=&quot;server&quot; runat=&quot;server&quot;&gt;` and everything inside it.

Any ideas what I am doing wrong?

&#39;//Markup

       &lt;tr id=&quot;installCityStateZip&quot; runat=&quot;server&quot;&gt;
          &lt;td&gt;&lt;asp:DropDownList ID=&quot;city&quot; OnSelectedIndexChanged=&quot;cityChanged&quot; AutoPostBack=&quot;true&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;
          &lt;td&gt;&lt;asp:TextBox ID=&quot;state&quot; runat=&quot;server&quot; Text=&quot;WI&quot; ReadOnly=&quot;true&quot; /&gt;&lt;/td&gt;
          &lt;td&gt;&lt;asp:DropDownList ID=&quot;zip&quot;  AppendDataBoundItems=&quot;true&quot; runat=&quot;server&quot;&gt;&lt;asp:ListItem Text=&quot;--Select--&quot; Value=&quot;&quot; /&gt;&lt;/asp:DropDownList&gt;&lt;/td&gt;
          &lt;td&gt;&lt;asp:TextBox ID=&quot;AccountNo&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr id=&quot;mailCityStateZip&quot; runat=&quot;server&quot;&gt;
          &lt;td&gt;&lt;asp:TextBox ID=&quot;mailCity&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;
          &lt;td&gt;&lt;asp:TextBox ID=&quot;mailState&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;
          &lt;td&gt;&lt;asp:TextBox ID=&quot;mailZip&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;
          &lt;td&gt;&lt;asp:TextBox ID=&quot;AccountNumber&quot; runat=&quot;server&quot; /&gt;&lt;/td&gt;
      &lt;/tr&gt;

&#39;//VB

                    Dim myConnection As New SqlConnection(conString)
                    Dim sqlStatement As String = &quot;Select * FROM Addresses Where AUTOID=@addressid&quot;
                    myConnection.Open()

                    Dim sqlCmd2 As SqlCommand = New SqlCommand(sqlStatement, myConnection)
                    sqlCmd2.Parameters.Add(&quot;@addressid&quot;, SqlDbType.Int).Value = addressID
                    Dim reader As SqlDataReader = sqlCmd2.ExecuteReader()

                    If reader.Read() Then
                        streetAddress.Text = reader(&quot;InstallAddress&quot;).ToString()
                        city.Text = reader(&quot;InstallCity&quot;).ToString()
                        state.Text = reader(&quot;InstallState&quot;).ToString()
                        If zip.Items.FindByText(reader.ToString()) IsNot Nothing Then
                            zip.Text = reader(&quot;InstallZip&quot;).ToString()
                        End If
                        yearBuilt.Text = reader(&quot;YearBuilt&quot;).ToString()
                        waterAccountNo.Text = reader(&quot;WaterAcctNo&quot;).ToString()
                        Dim sameAddress As Boolean = reader(&quot;InstallSameAsMailing&quot;).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(&quot;InstallSameAsMailing&quot;).Value.ToString() = &quot;Yes&quot; 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(&quot;InstallSameAsMailing&quot;) Is DBNull.Value AndAlso Convert.ToBoolean(reader(&quot;InstallSameAsMailing&quot;)) 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>



huangapple
  • 本文由 发表于 2023年6月29日 23:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定