Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calendar Control/SQLServer

Status
Not open for further replies.

hf28

Programmer
Oct 23, 2003
54
US
Hi everyone.
I'm trying to build the calendar for the housing registration. When the user opens the calendar, it should show already not available (taken) days. this information should come from the SQL Server. Then the user can choose something from the available days, highliting those days on the calendar . This Info now should be also inserted into the DB.
How does the calendar control work with SQL Server.
Thanks a lot in advance.
 
Something to the extent of:

Query the sql db for the dates.

then on the dayRender event determine if the date being rendered is in the query results.

(just something to get you started.)

-Jer
 
KnotGoblin is correct

try this code:

Code:
'Use Item(<index of date column> OR
'    Item("date col name")
Dim i as integer
        For i = 0 To <ds>.Tables(0).Rows.Count - 1
            If e.Day.Date.ToString = <ds>.Tables(0).Rows(i).Item(0) Then
                e.Cell.BackColor = Yellow
            End If
        Next
 
The calendar control does not work directly with SQL server like you are expecting. You still need to write some code in the dayrender like knotgoblin pointed out.

This is an interesting challange so I wrote one tonight.

ASPX Page
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm7.aspx.vb" Inherits="Examples2.WebForm7" %>
<HTML>
	<body>
		<form id="Form1" method="post" runat="server">
			<asp:Calendar id="Calendar1" runat="server" ShowNextPrevMonth="False"></asp:Calendar>
			<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
		</form>
	</body>
</HTML>

ASPX Code Behind File
Code:
Public Class WebForm7
    Inherits System.Web.UI.Page
    Protected WithEvents Calendar1 As System.Web.UI.WebControls.Calendar
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            Me.Calendar1.VisibleDate = Convert.ToDateTime("7/1/2005")
        End If

    End Sub
    Private Function GetDateTable() As DataTable
        Dim FromDate As DateTime = Me.Calendar1.VisibleDate.AddMonths(-1).Month & "/" & "1" & "/" & Me.Calendar1.VisibleDate.AddMonths(-1).Year
        Dim ToDate As DateTime = Me.Calendar1.VisibleDate.AddMonths(2).Month & "/" & "1" & "/" & Me.Calendar1.VisibleDate.AddMonths(1).Year
        Dim d As DateTime = FromDate
        Dim tbl As New DataTable

        tbl.Columns.Add(New DataColumn("MyDate", Type.GetType("System.DateTime")))
        tbl.Columns.Add(New DataColumn("Status", Type.GetType("System.Boolean")))
        tbl.PrimaryKey = New DataColumn() {tbl.Columns(0)}

        Do
            Dim rw As DataRow = tbl.NewRow
            rw("MyDate") = d
            rw("Status") = False

            'Select some fictitious preset dates
            If d = Convert.ToDateTime("7/1/2005") Then rw("Status") = True
            If d = Convert.ToDateTime("7/5/2005") Then rw("Status") = True

            tbl.Rows.Add(rw)
            d = d.AddDays(1)
            If d > ToDate Then Exit Do
        Loop

        Return tbl
    End Function
    Private DateTable As DataTable
    Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        Dim Cal As Calendar = CType(sender, Calendar)
        e.Cell.Controls.RemoveAt(0)
        If DateTable Is Nothing Then DateTable = GetDateTable()

        Dim chk As New HtmlControls.HtmlInputCheckBox
        Dim lbl As New Label
        lbl.Text = e.Day.Date.Day.ToString
        If (e.Day.Date.Year = Cal.VisibleDate.Year) And (e.Day.Date.Month = Cal.VisibleDate.Month) Then
            lbl.Font.Bold = True
        End If

        Dim rw As DataRow = DateTable.Rows.Find(Convert.ToDateTime(e.Day.Date.ToShortDateString))
        If Not rw Is Nothing Then
            If rw("Status") = True Then
                chk.Checked = True
                lbl.Style.Item("color") = "red"
                chk.Style.Item("visibility") = "hidden"
            End If
        End If
        chk.ID = "Date_" & e.Day.Date.ToShortDateString
        e.Cell.Style.Item("border") = "1px solid black"
        e.Cell.Style.Item("width") = "25px"
        e.Cell.Style.Item("height") = "25px"

        e.Cell.Controls.Add(lbl)
        e.Cell.Controls.Add(chk)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If DateTable Is Nothing Then DateTable = GetDateTable()
        For Each rw As DataRow In DateTable.Rows
            If Request.Form("Date_" & Convert.ToDateTime(rw("MyDate")).ToShortDateString) = "on" Then
                rw("Status") = True
                Response.Write(Convert.ToDateTime(rw("MyDate")).ToShortDateString & " was checked.<br>")
            Else
                rw("Status") = False
            End If
        Next

        'Your datatable "DateTable" is loaded with your posted back values and ready to update your database
    End Sub
End Class

 
ok here's an idea. if you want to keep the person from selecting a date. you can do the following (building on what jbenson001 suggested) and change the selectable property of the day to false:
Code:
Sub DayRender(source As Object, e As DayRenderEventArgs)
    Dim i as integer
    For i = 0 To <ds>.Tables(0).Rows.Count - 1
        If e.Day.Date.ToString = <ds>.Tables(0).Rows(i).Item(0) Then
                [b]e.Day.IsSelectable = False[/b]
            End If
        Next
End Sub

got the code from here:

then to have them highlight multiple date(s) you can change the selectionMode property
see:

..and add this in the html calendar element to specify a different style for selected days:
Code:
<SelectedDayStyle BackColor="Yellow"></SelectedDayStyle>

i'm not completely sure on the selectionMode thing. someone else could probably answer that concern better.

hope this helps. i'd like to know if it does. post the code when you get it to work.

-Jer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top