获得两次查询结果



嗨我在这个页面上得到了两次结果,我不知道为什么。当我在DropdownList1中选择选项2后查询数据库时,结果会出现两次。是因为结果显示在更新面板中吗?非常感谢你的帮助。我真的看不出代码有什么问题,这让我很生气!

page.aspx

   <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="Select">Select</asp:ListItem>
 <asp:ListItem value="Option 1">Option 1</asp:ListItem>
   <asp:ListItem Value="Option 2">Option 2</asp:ListItem>
</asp:DropDownList>

   <asp:UpdatePanel id="FirstPanel" runat="server" RenderMode="Inline">
   <ContentTemplate>   
      <div id="tohide1" visible="False" runat="server"> 
           +++Do something+++
           </div>
<div id="tohide2" visible="false" runat="server"> 
    <asp:Label ID="Label1" runat="server"></asp:Label>
   </div>
   </ContentTemplate>
   <Triggers>
   <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
    </Triggers>
    </asp:UpdatePanel>

page.aspx.vb

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    If DropDownList1.SelectedValue = "Option 1" Then
        tohide1.Visible = "True"
        tohide2.Visible = "False"
    ElseIf DropDownList1.SelectedValue = "Option 2" Then
        tohide1.Visible = "False"
        tohide2.Visible = "True"
     Dim connectionString As String = ConfigurationManager.ConnectionStrings("myConnString").ToString()
        Dim SqlQuery As String
        SqlQuery = "SELECT CourseCode, Q1 FROM Courses WHERE (Q1 = 1)"
        Dim conn As SqlConnection = New SqlConnection(connectionString)
        Dim Cmd1 As SqlCommand = New SqlCommand(SqlQuery, conn)
        Try
            conn.Open()
            Dim myReader As SqlDataReader = Cmd1.ExecuteReader

            While myReader.Read()
                Dim CourseCode As String = myReader("CourseCode")
                Label1.Text += CourseCode & "<br />"
          End While
        Catch ex As SqlException
            lblmessage.Text = ex.Message()
        Finally
            conn.Close()
        End Try
    End If
End Sub

我会将Label1.Text重置为过程顶部附近的"。这应该会阻止结果的重复。

最新更新