locked
Can't find Calendar control on RowDataBound RRS feed

  • Question

  • User-240333334 posted

    I can't figure out why I'm getting a value type 'control' cannot be converted to 'calendar' error.

    My goal is to have a dynamic Calendar that displays a range of dates in each gridview row. Sounds pretty simple right.

    Here is the Template Field Containing the Calendar Control and 2 labels with Start and End Dates

    <asp:TemplateField>
                    <ItemTemplate>
                       
                        <asp:Calendar ID="AssignmentCa" runat="server"></asp:Calendar>
                         <asp:Label ID="AssignmentStartDateLb" runat="server" Text='<%# Eval("AssignmentStartDate")%>'></asp:Label>
                         <asp:Label ID="AssignmentEndDateLb" runat="server" Text='<%# Eval("AssignmentEndDate")%>'></asp:Label>
                       
                    </ItemTemplate>
                </asp:TemplateField>

    Here is the Gridview RowDataBound subroutine

    Private Sub RoamerAssignmentsGV_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles RoamerAssignmentsGV.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim assignmentCa As Calendar = e.Row.FindControl("AssignmentCa") 'value type 'control' cannot be converted to 'calendar' ERROR
                Dim assignmentStartDateLb As Label = e.Row.FindControl("AssignmentStartDateLb")
                Dim assignmentEndDateLb As Label = e.Row.FindControl("AssignmentEndDateLb")
            End If
        End Sub

    Is there something special about finding a Calendar on RowDataBound events?

    Thursday, March 2, 2017 3:14 PM

Answers

  • User-1509636757 posted

    I do not think that can cause issue, if it is in a total different solution. Just to check, can you try fully qualified names as below:

    Dim assignmentCa As System.Web.UI.WebControls.Calendar = TryCast(e.Row.FindControl("AssignmentCa"), System.Web.UI.WebControls.Calendar)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 2, 2017 5:26 PM
  • User-240333334 posted

    That did it! "Calendar" was Ambiguous with  System.Globalization and  System.Web.UI Removing Imports System.Globalization fixed it. This code works shockingly well at rendering dynamic date ranges onto ASP.NET calendars.

    Private Sub RoamerAssignmentsGV_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles RoamerAssignmentsGV.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim assignmentCa As Calendar = TryCast(e.Row.FindControl("AssignmentCa"), Calendar)
                Dim assignmentStartDateLb As Label = e.Row.FindControl("AssignmentStartDateLb")
                Dim assignmentEndDateLb As Label = e.Row.FindControl("AssignmentEndDateLb")
                Dim selectedDates As SelectedDatesCollection = assignmentCa.SelectedDates
                selectedDates.SelectRange(assignmentStartDateLb.Text, assignmentEndDateLb.Text)
            End If
        End Sub

    Thanks Kaushal!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 2, 2017 6:01 PM

All replies

  • User-1509636757 posted

    Dim assignmentCa As Calendar = e.Row.FindControl("AssignmentCa") 'value type 'control' cannot be converted to 'calendar' ERROR

    Try casting:

    Dim AssignmentCa As Calendar = TryCast(e.Row.FindControl("AssignmentCa"), Calendar)

    Thursday, March 2, 2017 4:07 PM
  • User-240333334 posted

    Still getting the same error.

    Private Sub RoamerAssignmentsGV_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles RoamerAssignmentsGV.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
    Dim assignmentCa As Calendar = TryCast(e.Row.FindControl("AssignmentCa"), Calendar) 'STILL GETTING THE SAME ERROR
    Dim assignmentStartDateLb As Label = e.Row.FindControl("AssignmentStartDateLb")
    Dim assignmentEndDateLb As Label = e.Row.FindControl("AssignmentEndDateLb")
    Dim index As Integer = e.Row.RowIndex
    ViewState("RoamerAssignmentsGV_RowIndex") = index
    ViewState("assignmentStartDateLb") = assignmentStartDateLb.Text
    ViewState("assignmentEndDateLb") = assignmentEndDateLb.Text
    
    End If
    End Sub

    Thursday, March 2, 2017 4:28 PM
  • User-240333334 posted

    I've never tried to find a Calendar control in a Gridview RowDataBound event before, is it NOT possible for some reason?

    Thursday, March 2, 2017 4:30 PM
  • User-240333334 posted

    Maybe there is a way to find AssignmentStartDateLb and AssignmentEndDateLb Labels within a specific gridview index or Row in the DayRender Calendar Event.

    Thursday, March 2, 2017 4:38 PM
  • User-1509636757 posted

    I've never tried to find a Calendar control in a Gridview RowDataBound event before, is it NOT possible for some reason?

    This is strange. In a short demo app, on my side I am able to do it. Is there any other user control or any class with name Calendar exists in your solution?

    Thursday, March 2, 2017 4:48 PM
  • User-240333334 posted

    Not that I know of, any suggestions on how to find out? This is so strange and time-consuming.

    Thursday, March 2, 2017 5:06 PM
  • User-240333334 posted

    I do have Calendar.aspx in another solution. Could that be causing the issue?

    Thursday, March 2, 2017 5:20 PM
  • User-1509636757 posted

    I do not think that can cause issue, if it is in a total different solution. Just to check, can you try fully qualified names as below:

    Dim assignmentCa As System.Web.UI.WebControls.Calendar = TryCast(e.Row.FindControl("AssignmentCa"), System.Web.UI.WebControls.Calendar)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 2, 2017 5:26 PM
  • User-240333334 posted

    That did it! "Calendar" was Ambiguous with  System.Globalization and  System.Web.UI Removing Imports System.Globalization fixed it. This code works shockingly well at rendering dynamic date ranges onto ASP.NET calendars.

    Private Sub RoamerAssignmentsGV_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles RoamerAssignmentsGV.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim assignmentCa As Calendar = TryCast(e.Row.FindControl("AssignmentCa"), Calendar)
                Dim assignmentStartDateLb As Label = e.Row.FindControl("AssignmentStartDateLb")
                Dim assignmentEndDateLb As Label = e.Row.FindControl("AssignmentEndDateLb")
                Dim selectedDates As SelectedDatesCollection = assignmentCa.SelectedDates
                selectedDates.SelectRange(assignmentStartDateLb.Text, assignmentEndDateLb.Text)
            End If
        End Sub

    Thanks Kaushal!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 2, 2017 6:01 PM