Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Add hyperlink to day cell of calendar control

Posted on 2007-11-21
3
1,152 Views
Last Modified: 2008-02-01
Hello, I'm trying to create a calender control that gets date and title from the events table in an access database. Then display the title for a given date in a linkbutton or hyperlink so that when the link is displayed and then clicked by the user I can then grab the corresponding event data and populate a datalist. Using the code below that I modified from http://msdn2.microsoft.com/en-us/library/ms228044(VS.80).aspx I was able to display the dates that have an event. Now I need help displaying the title in the linkbutton. How can this be accomplished? Any help would be great!

Here is the code that I'm working with:

    Protected dsEvents As DataSet

    Protected Sub FillEventsDataset()
        Dim firstDate As New DateTime(calHREvents.VisibleDate.Year, _
             calHREvents.VisibleDate.Month, 1)
        Dim lastDate As DateTime = GetFirstDayOfNextMonth()
        dsEvents = GetCurrentMonthData(firstDate, lastDate)
    End Sub

    Protected Function GetFirstDayOfNextMonth() As DateTime
        Dim monthNumber, yearNumber As Integer
        If calHREvents.VisibleDate.Month = 12 Then
            monthNumber = 1
            yearNumber = calHREvents.VisibleDate.Year + 1
        Else
            monthNumber = calHREvents.VisibleDate.Month + 1
            yearNumber = calHREvents.VisibleDate.Year
        End If
        Dim lastDate As New DateTime(yearNumber, monthNumber, 1)
        Return lastDate
    End Function

    Protected Sub calHREvents_VisibleMonthChanged(ByVal sender As Object, _
            ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) _
            Handles calHREvents.VisibleMonthChanged
        FillEventsDataset()
    End Sub

    Function GetCurrentMonthData(ByVal firstDate As DateTime, _
            ByVal lastDate As DateTime) As DataSet
        Dim dsMonth As New DataSet


        'create connection to HRData database
        Dim objConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("~/App_Data/HRData.mdb"))
        Dim connString As String = objConn.ConnectionString
        Dim dbConnection As New OleDbConnection(connString)
        Dim query As String
        query = "SELECT Date, Title FROM Events " & _
            " WHERE Date >= @firstDate AND Date < @lastDate"
        Dim dbCommand As New OleDbCommand(query, dbConnection)
        dbCommand.Parameters.Add(New OleDbParameter("@firstDate", firstDate))
        dbCommand.Parameters.Add(New OleDbParameter("@lastDate", lastDate))

        Dim oleDataAdapter As New OleDbDataAdapter(dbCommand)
        Try
            oleDataAdapter.Fill(dsMonth)
        Catch
        End Try
        Return dsMonth
    End Function

    Protected Sub calHREvents_DayRender(ByVal sender As Object, _
            ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) _
            Handles calHREvents.DayRender
        Dim nextDate As DateTime
        If Not dsEvents Is Nothing Then
            For Each dr As DataRow In dsEvents.Tables(0).Rows
                nextDate = CType(dr("Date"), DateTime)
                If nextDate = e.Day.Date Then
                    e.Cell.BackColor = System.Drawing.Color.Pink
                End If
            Next
        End If
    End Sub


Here is the markup:
<asp:Calendar ID="calHREvents" Width="725px" Height="500px" runat="server" BackColor="White" BorderColor="Black" DayNameFormat="Shortest" Font-Names="Times New Roman" Font-Size="10pt" ForeColor="Black" NextPrevFormat="FullMonth" TitleFormat="Month">
                <SelectedDayStyle BackColor="#CC3333" ForeColor="White" />
                <TodayDayStyle BackColor="#CCCC99" />
                <SelectorStyle BackColor="#CCCCCC" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt"
                    ForeColor="#333333" Width="1%" />
                <DayStyle Width="14%" />
                <OtherMonthDayStyle ForeColor="#999999" />
                <NextPrevStyle Font-Size="8pt" ForeColor="White" />
                <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" ForeColor="#333333"
                    Height="10pt" />
                <TitleStyle BackColor="Black" Font-Bold="True" Font-Size="12pt" ForeColor="White"
                    Height="10pt" />
            </asp:Calendar>
0
Comment
Question by:gogetsome
  • 2
3 Comments
 
LVL 9

Expert Comment

by:codeclay
ID: 20332998
If you want to add a link in your calendar day columns , do it like this in the calHREvents_DayRender event
 Dim hl As New HyperLink
        hl.NavigateUrl = "#" 'Some Link here
        hl.Text = "Some Text from ur Dataset"
        e.Cell.Controls.Add(hl)
0
 

Author Comment

by:gogetsome
ID: 20334889
That works! very cool.
The points are yours. If you have time can you answer this? The link with my title does show up and I'm able to put it in a session and retrieve it with no problem. BUT, the link that we added is right next to the day number. Is there a way to issue a line feed or a space or something after the day number for each cell that has one of these links added?
0
 
LVL 9

Accepted Solution

by:
codeclay earned 500 total points
ID: 20337520
HEre you go
      Dim hl As New HyperLink
        Dim lc As New LiteralControl
        lc.Text = "</BR>"
        hl.NavigateUrl = "#" 'Some Link here
        hl.Text = "Some Text from ur Dataset"
        e.Cell.Controls.Add(lc)
        e.Cell.Controls.Add(hl)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC 2 40
Need to start a web service from Visual Studio 2015 Pro 2 35
ASP.NET MVC identity 6 30
How to repeat the data 4 17
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question