0
嗨 我在本頁獲得兩次結果,我不知道爲什麼。在我選擇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
Doh!非常感謝! – netNewbi3 2011-04-14 14:18:36