Hello my fellow Daniwebers, I have a question for you.

I have quite an odd problem that I have researched and have not been able to find a solution.

I have a formview, named frmStatus, from which I am trying to retrieve controls to check for values.

The form is in Insert mode and contains three drop down boxes, from which I am trying to reference.

Here is what I have tryed:

'Declaration of variables'
Dim cboStatus As New Control
Dim cboAction As New Control
Dim cboChange As New Control


'Assigning the controls.'
Protected Sub frmStatus_PreRender(sender As Object, e As System.EventArgs) Handles frmStatus.PreRender
    cboStatus = Me.frmStatus.FindControl("cboStatus")
    cboAction = Me.frmStatus.FindControl("cboAction")
    cboChange = Me.frmStatus.FindControl("cboChange")
End Sub


'Trying to capture the insert and check for default text.'

 Protected Sub frmStatus_ItemInserting(sender As Object, e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles frmStatus.ItemInserting   
    Dim cboS As DropDownList = TryCast(cboStatus, DropDownList)
    Dim cboA As DropDownList = TryCast(cboAction, DropDownList)
    Dim cboC As DropDownList = TryCast(cboChange, DropDownList)

    If IsNothing(cboS) = False And IsNothing(cboA) = False And IsNothing(cboC) = False Then
        If cboS.Text = "-- Select Status --" Then
            MsgBox("Please select status.")
            e.Cancel = True
        ElseIf cboA.Text = "-- Select Action Type --" Then
            MsgBox("Please select action type.")
            e.Cancel = True
        ElseIf cboC.Text = "-- Select Default Change Status --" Then
            MsgBox("Please select change status.")
            e.Cancel = True
        Else
            e.Cancel = False
        End If
    Else
        MsgBox("Could not convert controls.")
    End If
End Sub

From the code above, the top level else is always thrown.

What am I doing wrong?

Thanks,
Begginnerdev

I have found a solution by using the DirectCast function.

Here is what I used for the solution:

Protected Sub frmStatus_ItemInserting(sender As Object, e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles frmStatus.ItemInserting   
    Dim cboS As DropDownList = DirectCast(Me.frmStatus.FindControl("cboStatus"), DropDownList)
    Dim cboA As DropDownList = DirectCast(Me.frmStatus.FindControl("cboAction"), DropDownList)
    Dim cboC As DropDownList = DirectCast(Me.frmStatus.FindControl("cboChange"), DropDownList)

    If IsNothing(cboS) = False And IsNothing(cboA) = False And IsNothing(cboC) = False Then
        If cboS.SelectedItem.Text = "-- Select Status --" Then
            e.Cancel = True
        ElseIf cboA.SelectedItem.Text = "-- Select Action Type --" Then
            e.Cancel = True
        ElseIf cboC.SelectedItem.Text = "-- Select Default Change Status --" Then
            e.Cancel = True
        Else
            e.Cancel = False
        End If
    Else
        MsgBox("Could not convert controls.")
    End If
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.