Change Calendar selection based on data from the database (VB.net)
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<html>
<head><title>Calendar: Hire Dates</title></head>
<body>
<h3>Calendar: Hire Dates</h3>
<form runat="server">
<asp:ListBox id="lstEmployees" runat="server"
Rows="7"
Autopostback="True" /><br/>
<asp:Label id="lblSelectedDate" runat="server" />
<asp:Calendar id="calHire" runat="server"
SelectionMode = "None" />
</form>
</body>
</html>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
Dim strConnection As String = ConfigurationSettings.AppSettings("YourDataName")
Dim objConnection As New SqlConnection(strConnection)
Dim strSQL As String = "SELECT ID, LastName, FirstName, HireDate FROM Employee;"
Dim objAdapter As New SqlDataAdapter(strSQL, objConnection)
Dim objDataSet As New DataSet("dsEmployees")
objAdapter.Fill(objDataSet, "dtEmployees")
If Not IsPostBack Then
lstEmployees.DataSource = objDataSet
lstEmployees.DataTextField = "LastName"
lstEmployees.DataBind()
Else
Dim datHireDate As Date
datHireDate = Convert.ToDateTime( _
objDataSet.Tables("dtEmployees").Rows(lstEmployees.SelectedIndex)("HireDate"))
lblSelectedDate.Text = _
objDataSet.Tables("dtEmployees").Rows(lstEmployees.SelectedIndex)("LastName")
calHire.VisibleDate = datHireDate
calHire.SelectedDate = datHireDate
lblSelectedDate.Text &= " hired on " & datHireDate
End If
End Sub
</script>
Related examples in the same category