1

Error:

Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.DropDownList'

Why?

<EditFormSettings EditFormType="Template">
    <FormTemplate>
    <asp:Label ID="Assign_t" runat="server" Font-Bold="True" ForeColor="#3366CC">Assign</asp:Label>

    <telerik:RadDropDownList ID="dd_Assign" runat="server" RenderMode="Lightweight" Skin="Bootstrap"  SelectedValue='<%#Bind("Assign_to") %>'>
           <Items>
              <telerik:DropDownListItem runat="server" Text="No Assign" Value="0" />
              <telerik:DropDownListItem runat="server" Text="name1" Value="email1@svi.co.th" />
              <telerik:DropDownListItem runat="server" Text="name2" Value="email2@svi.co.th" />
           </Items>
    </telerik:RadDropDownList>

    <asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf (Container) Is GridEditFormInsertItem), "Insert", "Update") %>' OnClick = "SendMail"
                                        runat="server" CommandName='<%# IIf((TypeOf (Container) Is GridEditFormInsertItem), "PerformInsert", "Update")%>' ></asp:Button>
    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                            CommandName="Cancel"></asp:Button>
    </FormTemplate>
</EditFormSettings>
Protected Sub SendMail(ByVal sender As Object, ByVal e As EventArgs)

    Dim ddlSection As DropDownList = DirectCast(sender, DropDownList)
    Dim editItem As GridEditFormItem = DirectCast(ddlSection.NamingContainer, GridEditFormItem)
    Dim dd_Assign As DropDownList = DirectCast(editItem.FindControl("dd_Assign"), DropDownList)

    Dim strFrom As String = "emailfrom@emailfrom.co.th" 
    Dim strCC As String = dd_Assign.SelectedItem.ToString
    Dim strBCC As String = "BCC@BCC.com" 'BCC
    Dim strSubject As String = "Request JOB Verification Approval" 
    Dim strBody As String = "test"
    Dim mailMessage As New System.Net.Mail.MailMessage(strFrom, strCC, strSubject, strBody)
    Dim mailClient As New System.Net.Mail.SmtpClient("xx.xx.xx.xx", 25)
    mailClient.Credentials = New System.Net.NetworkCredential("email@email.co.th", "pass") 
    mailClient.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
    mailClient.UseDefaultCredentials = True
    mailClient.Send(mailMessage)

End Sub
Pang
  • 9,564
  • 146
  • 81
  • 122
  • The `sender` parameter is the object that raised the event. Obviously you are handling the `Click` event of a `Button` there so why would the `sender` be anything else? – jmcilhinney Aug 22 '17 at 01:28

1 Answers1

2

The error message is pretty straightforward: an attempt to cast a Button control to DropDownList has occurred & throwing InvalidCastException.

One of the button control has OnClick event handler which bound to SendMail method:

<asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf (Container) Is GridEditFormInsertItem), "Insert", "Update") %>' 
            OnClick="SendMail"
            runat="server" CommandName='<%# IIf((TypeOf (Container) Is GridEditFormInsertItem), "PerformInsert", "Update")%>'>
</asp:Button>

But inside SendMail method attempts to cast into DropDownList are exist:

' first cast
Dim ddlSection As DropDownList = DirectCast(sender, DropDownList)

' second cast
Dim dd_Assign As DropDownList = DirectCast(editItem.FindControl("dd_Assign"), DropDownList)

Note that the sender argument contains control object that the event action is bound for, hence you need to bind the event handler into proper control. You can try casting it to Button to prove it:

Dim button As Button = DirectCast(sender, Button)

Since EditFormSettings with Template edit mode was used, I think you're using RadGrid here, so you can utilize ItemUpdated event instead of standard button event handler as given in this example.

Protected Sub RadGrid1_ItemUpdated(ByVal source As Object, ByVal e As Telerik.Web.UI.GridUpdatedEventArgs) Handles RadGrid1.ItemUpdated
    If e.Exception Is Nothing Then
       ' email sending code here
    Else
       ' throw exception
    End If
End If
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61