Utilizzare la proprietà IsPostBack per evitare di caricare più volte i dati in una DropDownList
<%@ Page language="VB" Debug="false" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<HTML>
<script language="vbscript" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack then
Dim strConn as string = "server=NOMEDATABASESERVER;uid=USERID;
pwd=PASSWORD;database=NOMEDATABASE"
Dim MySQL as string = "Select LastName from Employees"
Dim MyConn as New SQLConnection(strConn)
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
myDropDownList.DataSource = objDR
myDropDownList.DataBind()
End If
End Sub
</script>
<body>
<form Name="Form1" Runat="server">
<asp:DropDownList ID="myDropDownList" Runat="server"></asp:DropDownList>
</form>
</body>
</html>