Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents dlZipCodes As System.Web.UI.WebControls.DataList
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
BindData()
PopulateZipCodeLists()
End If
End Sub
Sub BindData()
'Create Connection
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
strSQL = "SELECT * FROM ZipCodes WHERE UserID = 2 ORDER BY ZipCode"
objConn = New SqlConnection("Server=***.***.***.***;UID=****;PWD=****;database=******")
objCmd = New SqlCommand(strSQL, objConn)
'Set the datagrid's datasource to the datareader and databind
objConn.Open()
dlZipCodes.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
dlZipCodes.DataBind()
End Sub
Sub PopulateZipCodeLists()
Dim dlItem As DataListItem
Dim dlList As DropDownList
Dim dsZip As DataSet
Dim dRow As DataRow
Dim lstItem As System.Web.UI.WebControls.ListItem
'Load ZipCodes from xml file into dataset
dsZip = New DataSet()
dsZip.ReadXml("F:\WebSites\MSMX\Common\ZipCodes.xml")
'For each row in the datalist
For Each dlItem In dlZipCodes.Items
'Set the dropdownlist control to the row's dropdownlist
dlList = dlItem.FindControl("lstZipCodes")
'For each zipcode inthe xml file
For Each dRow In dsZip.Tables(0).Rows
'Create ListItem, assign its text and value from xml file
lstItem = New System.Web.UI.WebControls.ListItem()
lstItem.Text = dRow("Zip")
lstItem.Value = dRow("Zip")
'Add listitem to the dropdownlist in the data row we're on
dlList.Items.Add(lstItem)
Next
'Try to select the correct listitem by using the zipcode value we stored in the invisible label
Try
dlList.Items.FindByValue(CType(dlItem.FindControl("lblZip"), Label).Text).Selected = True
Catch
'The listitem or control could not be found, don't do anything
End Try
Next
End Sub
Sub dlZipCodes_Click(ByVal s As Object, ByVal e As DataListCommandEventArgs)
Dim bolVisible As Boolean
bolVisible = CType(e.Item.FindControl("chkVisible"), CheckBox).Checked
Response.Write(bolVisible)
Dim strZipCode As String
strZipCode = CType(e.Item.FindControl("lstZipCodes"), DropDownList).SelectedItem.Value
Response.Write(strZipCode)
Dim intID As Integer
intID = CType(e.Item.FindControl("lblID"), Label).Text
Response.Write(intID)
Dim dteDate As DateTime
dteDate = Convert.ToDateTime(CType(e.Item.FindControl("lblZIP_Date"), Label).Text)
Response.Write(dteDate.ToShortDateString())
End Sub
End Class