Wrox Programmer Forums
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2009, 07:52 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default AddHandler Issue...

Ok what i'm trying to do is add an ImageButton to a Calendar control by writing it to the cell on the DayRender. I'm using AddHandler to assign the OnCommand so i can access a public sub. How ever the command never executes. I know it works because i can create a static imagebutton anywhere on the page and assign the command and it does what it's suppose to. Now i'm pretty sure it's because i'm creating the imagebutton in the dayrender event and not the page load event but if i create it in the page_load i can't access it in the DayRender.

Here is my DayRender code and the code for the Sub i want to add the handler for. I'm sure most of it you don't need but here it is anyway. Any ideas or suggestions would be greatly appreciated

Code:
         Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)

        Dim Item As MyDates
        Dim DayHold As DateTime = CDate("01/01/1900")
        Dim MultipleItemDay As Boolean = False
        Dim DayTextHasChanged As Boolean = False
        Dim temp As New StringBuilder
        Dim imgButton As New ImageButton

        If MyCollection Is Nothing Then
            GetSchedule()
        End If

        For Each Item In MyCollection
            If DayHold <> Item.dtDate Then
                If DayTextHasChanged = True Then
                    Exit For
                End If
                MultipleItemDay = False
                DayHold = Item.dtDate
            Else
                MultipleItemDay = True
            End If

            Dim Diff As Long = DateDiff(DateInterval.Day, e.Day.Date, Date.Today)
            If Diff > 0 Then
                e.Day.IsSelectable = False
                e.Cell.ToolTip = ""
            Else
                e.Cell.ToolTip = ""
                e.Day.IsSelectable = False
            End If

            If e.Day.Date = CDate(Item.dtDate.ToString("d")) Then
                If MultipleItemDay = False Then
                    temp = New StringBuilder()
                Else
                    temp.Append("<br />")
                End If
                temp.Append("<br />")
                temp.Append("<span style='font-size:10pt; color: saddlebrown'>")
                If Item.TakenSlots = Item.ScheduledSlots Then
                    imgButton = Nothing
                    temp.Append("There are no more available appointments today")
                Else
                 
                    imgButton.ID = "lnk" & e.Day.DayNumberText
                    imgButton.ImageUrl = "~/Images/Reserve.jpg"
                    imgButton.ToolTip = "Click here to schedule an appointment."
                    imgButton.CommandName = "ShowTimeSlots"
                    imgButton.CommandArgument = e.Day.Date.ToShortDateString

                    AddHandler imgButton.Command, AddressOf ShowTimeSlots

                    temp.Append((Item.ScheduledSlots - Item.TakenSlots).ToString & " Slots open.</span><br />")
                End If
                temp.Append("<br />")
                temp.Append("</span><br />")
                e.Cell.Controls.Add(New LiteralControl(temp.ToString()))
                If Not imgButton Is Nothing Then
                    e.Cell.Controls.Add(imgButton)
                End If
            End If
        Next
    End Sub

    Protected Sub ShowTimeSlots(ByVal sender As Object, ByVal e As CommandEventArgs)

        Dim SelectedDate As Date = CDate(e.CommandArgument)

        Dim sqlCon As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cnsGMS").ConnectionString)
        Dim sqlCmd As New System.Data.SqlClient.SqlCommand("sp_GetScheduleByDate")
        Dim sqlDR As System.Data.SqlClient.SqlDataReader = Nothing

        Try
            sqlCon.Open()
            sqlCmd.Connection = sqlCon
            sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
            sqlCmd.Parameters.Add("@SearchDate", SqlDbType.DateTime)
            sqlCmd.Parameters("@SearchDate").SqlValue = SelectedDate
            sqlDR = sqlCmd.ExecuteReader

            Dim temp As String = ""
            Do While sqlDR.Read()
                temp = temp & "<table style='text-align:left'>"
                temp = temp & "<tr>"
                temp = temp & "<td>" & sqlDR("TimeSlot").ToString & "</td>"
                temp = temp & "<td><a href='Reserve.aspx?date=" & Calendar1.SelectedDate.ToShortDateString & "&time=" & sqlDR("TimeSlot").ToString & "' target='_blank'>Reserve This Time</a>"
                temp = temp & "</tr>"
                temp = temp & "</table>"
            Loop
            Dim Diff As Long = DateDiff(DateInterval.Day, SelectedDate, Date.Today)
            If Diff > 0 Then
                daydetail_render.InnerHtml = "<h3 style='color:red'>You Can't Reserve Appointments In The Past.</h3>"
            Else
                If temp.Length > 0 Then
                    daydetail_render.InnerHtml = temp
                Else
                    daydetail_render.InnerHtml = "<h3 style='color:red'>There Are No Appointments Available On This Day</h3>"
                End If
            End If
            daydetail.Visible = True


        Finally
            sqlCon.Close()
            sqlDR.Close()
            sqlCmd = Nothing
            sqlDR.Close()
            sqlCon = Nothing
        End Try

        selectedday.InnerHtml = SelectedDate.ToShortDateString
        tempDate = SelectedDate
        GetSchedule()
    End Sub
__________________
Jason Hall

Follow me on Twitter @jhall2013
 
Old March 9th, 2009, 08:43 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Jason
You are right. Once you get to DayRender, the event firing phase is done with, so you are adding the handler too late.

There is a similar question posted at http://forums.asp.net/p/1075467/1580868.aspx#1580868
In this case, they added a hidden field which is updated using javascript by the button click. The hidden field value is then checked in the back end. You may be able to do the same sort of thing.
It's not the prettiest solution - maybe someone knows a better one (I for one am going to bed).

HTH
Phil
The Following User Says Thank You to philip_cole For This Useful Post:
alliancejhall (March 9th, 2009)
 
Old March 9th, 2009, 08:46 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default thanks....

Thank you i'll check that out. good night :)
__________________
Jason Hall

Follow me on Twitter @jhall2013
 
Old March 9th, 2009, 09:17 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default I feel dumb...

That's so simple and i don't know why i didn't think of that? here's what i did

i added a hidden field to my page

<asp:HiddenField ID="hdnDate" runat="server" />

then my dayrender i got rid of the handler and added an onclick attribute.
Code:
    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)

        Dim mdItem As MyDates
        Dim strCellContent As New StringBuilder
        Dim imgButton As New ImageButton

        If MyCollection Is Nothing Then
            GetSchedule()
        End If

        For Each mdItem In MyCollection

            Dim Diff As Long = DateDiff(DateInterval.Day, e.Day.Date, Date.Today)
            If Diff > 0 Then
                e.Cell.ToolTip = " "
                e.Day.IsSelectable = False
            Else
                e.Day.IsSelectable = True
                e.Cell.ToolTip = "Click date to schedule appointment."
            End If

            If e.Day.Date = mdItem.DateSlot Then

                strCellContent.Append("<br />")
                strCellContent.Append("<span style='font-size:10pt; color: saddlebrown'>")

                If mdItem.TakenSlots = mdItem.ScheduledSlots Then
                    imgButton = Nothing
                    strCellContent.Append("There are no more available appointments today")
                Else
                    imgButton.ID = "lnk" & e.Day.DayNumberText
                    imgButton.ImageUrl = "~/Images/Reserve.jpg"
                    imgButton.ToolTip = "Click here to schedule an appointment."
                    imgButton.Attributes.Add("onclick", "document.getElementById('" & hdnDate.ClientID & "').value='" & e.Day.Date.ToShortDateString & "';") 

                    strCellContent.Append((mdItem.ScheduledSlots - mdItem.TakenSlots).ToString & " Slots open.</span><br />")
                End If

                strCellContent.Append("<br />")
                strCellContent.Append("</span><br />")

                e.Cell.Controls.Add(New LiteralControl(strCellContent.ToString()))

                If Not imgButton Is Nothing Then
                    e.Cell.Controls.Add(imgButton)
                End If

            End If

        Next

    End Sub
then on the page load i put an ispostback

Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Page.IsPostBack Then
            GetSchedule()
        Else
            ShowTimeSlots(hdnDate.value)
        End If

    End Sub
then my sub i want to call when the button is clicked
Code:
    Protected Sub ShowTimeSlots(ByVal SearchDate As String)

        Dim SelectedDate As Date = CDate(SearchDate)

        Dim sqlCon As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cnsGMS").ConnectionString)
        Dim sqlCmd As New System.Data.SqlClient.SqlCommand("sp_GetScheduleByDate")
        Dim sqlDR As System.Data.SqlClient.SqlDataReader = Nothing

        Try
            sqlCon.Open()
            sqlCmd.Connection = sqlCon
            sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
            sqlCmd.Parameters.Add("@SearchDate", SqlDbType.DateTime)
            sqlCmd.Parameters("@SearchDate").SqlValue = SelectedDate
            sqlDR = sqlCmd.ExecuteReader

            Dim strDivContent As String = ""
            Do While sqlDR.Read()
                strDivContent = strDivContent & "<table style='text-align:left'>"
                strDivContent = strDivContent & "<tr>"
                strDivContent = strDivContent & "<td>" & sqlDR("TimeSlot").ToString & "</td>"
                strDivContent = strDivContent & "<td><a href='Reserve.aspx?date=" & Calendar1.SelectedDate.ToShortDateString & "&time=" & sqlDR("TimeSlot").ToString & "' target='_blank'>Reserve This Time</a>"
                strDivContent = strDivContent & "</tr>"
                strDivContent = strDivContent & "</table>"
            Loop

            If strDivContent.Length > 0 Then
                daydetail_render.InnerHtml = strDivContent
            Else
                daydetail_render.InnerHtml = "<h3 style='color:red'>There Are No Appointments Available On This Day</h3>"
            End If

            daydetail.Visible = True

        Finally
            sqlCon.Close()
            sqlDR.Close()
            sqlCmd = Nothing
            sqlDR.Close()
            sqlCon = Nothing
        End Try

        selectedday.InnerHtml = SelectedDate.ToShortDateString
        GetSchedule()

    End Sub
__________________
Jason Hall

Follow me on Twitter @jhall2013
 
Old March 10th, 2009, 05:39 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Great glad you got it sorted. And thanks for showing your solution so clearly.
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is addhandler for linkbutton.click not working rsearing ASP.NET 2.0 Basics 7 November 12th, 2007 06:06 PM
addhandler catch 22 roog ASP.NET 1.0 and 1.1 Professional 2 February 20th, 2006 01:01 PM
Beware the AddHandler etoostr General .NET 0 November 6th, 2004 05:53 PM
AddHandler from String [email protected] Pro VB.NET 2002/2003 2 July 6th, 2003 04:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.