我的代码: <%@Page Language="vb" Debug="true"%> <%@Import namespace="EmployeeService"%> <%@Import namespace="System.Data"%> <%@Import namespace="System.Data.OleDb"%> <script language="vb" runat="server">Dim arrEmployee as arrayList=new Arraylist()Private Sub CmbClick(sender As Object,e As EventArgs) Dim Address as New EmployeeService.Employeeinfo() Txt.text=Address.EmployeeAddress(DDL.SelectedItem.Value) End SubSub Page_Load() Dim objEmployeeDR As oleDbdataReader Dim objEmployeeConn As OleDbConnection Dim objEmployeeCmd As OleDbCommand Dim strConn As String="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("Northwind.mdb") & ";" Dim strCmd As String="SELECT (姓氏+名字) as name FROM 雇员" objEmployeeConn=New oleDbConnection(strConn) objEmployeeCmd=New oleDbCommand(strCmd,objEmployeeConn) objEmployeeConn.open() objEmployeeDR=objEmployeeCmd.ExecuteReader(CommandBehavior.CloseConnection) Do While objEmployeeDR.Read()=True arrEmployee.Add(objEmployeeDR("name")) Loop objEmployeeDR.Close() DDL.DataSource=arrEmployee DDL.DataBindEnd Sub</script><html> <body> <form id="form1" method="post" runat="server"> <asp:DropDownList id="DDL" AutoPostBack=True runat="server" OnSelectedIndexChanged="CmbClick"> </asp:DropDownList><br> <asp:label id="Txt" runat="server"></asp:label> </form> </body> </html>
把这两句: DDL.DataSource=arrEmployee DDL.DataBind 修改为: If Not IsPostBack Then DDL.DataSource=arrEmployee DDL.DataBind End If
同意楼上 因为你的DropDownList的AutoPostBack的属性为true,所以每次selectedindexchanged时,重新载入页面。执行Sub Page_Load()函数,重新绑定了你的DDL,所以要将绑定放在 If Not IsPostBack Then //页面第一次载入时执行的程序 End If
<%@Page Language="vb" Debug="true"%>
<%@Import namespace="EmployeeService"%>
<%@Import namespace="System.Data"%>
<%@Import namespace="System.Data.OleDb"%>
<script language="vb" runat="server">Dim arrEmployee as arrayList=new Arraylist()Private Sub CmbClick(sender As Object,e As EventArgs)
Dim Address as New EmployeeService.Employeeinfo()
Txt.text=Address.EmployeeAddress(DDL.SelectedItem.Value)
End SubSub Page_Load()
Dim objEmployeeDR As oleDbdataReader
Dim objEmployeeConn As OleDbConnection
Dim objEmployeeCmd As OleDbCommand
Dim strConn As String="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("Northwind.mdb") & ";"
Dim strCmd As String="SELECT (姓氏+名字) as name FROM 雇员"
objEmployeeConn=New oleDbConnection(strConn)
objEmployeeCmd=New oleDbCommand(strCmd,objEmployeeConn)
objEmployeeConn.open() objEmployeeDR=objEmployeeCmd.ExecuteReader(CommandBehavior.CloseConnection) Do While objEmployeeDR.Read()=True
arrEmployee.Add(objEmployeeDR("name"))
Loop
objEmployeeDR.Close()
DDL.DataSource=arrEmployee
DDL.DataBindEnd Sub</script><html>
<body>
<form id="form1" method="post" runat="server">
<asp:DropDownList id="DDL" AutoPostBack=True runat="server" OnSelectedIndexChanged="CmbClick">
</asp:DropDownList><br>
<asp:label id="Txt" runat="server"></asp:label>
</form>
</body>
</html>
DDL.DataSource=arrEmployee
DDL.DataBind
修改为:
If Not IsPostBack Then
DDL.DataSource=arrEmployee
DDL.DataBind
End If
因为你的DropDownList的AutoPostBack的属性为true,所以每次selectedindexchanged时,重新载入页面。执行Sub Page_Load()函数,重新绑定了你的DDL,所以要将绑定放在
If Not IsPostBack Then
//页面第一次载入时执行的程序
End If