getting query results twice
Hi I'm getting results twice in this page and I'm not sure why. When I query the database after I select Option 2 in DropdownList1 results appear twice. Is it because the results appear in an updatepanel? Many thanks for your help. I can't really see the problem with the code and it is driving me mad!
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 />"
E开发者_如何学Cnd While
Catch ex As SqlException
lblmessage.Text = ex.Message()
Finally
conn.Close()
End Try
End If
End Sub
I would reset the Label1.Text to "" near the top of the procedure. That should stop the duplication in the results.
精彩评论