无法转换类型为#System; Web.UI.WebControls.Button'的对象键入' System.Web.UI.WebControls.DropDownList'

时间:2017-08-22 00:57:20

标签: sql vb.net telerik

错误:

  

无法转换类型为#System; Web.UI.WebControls.Button'的对象键入' System.Web.UI.WebControls.DropDownList'

为什么?

<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

1 个答案:

答案 0 :(得分:2)

错误消息非常简单:尝试将Button控件强制转换为DropDownList&amp;投掷InvalidCastException

其中一个按钮控件的OnClick事件处理程序绑定到SendMail方法:

<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>

但是在SendMail方法内部尝试强制转换为DropDownList

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

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

请注意sender argument contains control object that the event action is bound for,因此您需要将事件处理程序绑定到适当的控件中。您可以尝试将其投射到Button以证明它:

Dim button As Button = DirectCast(sender, Button)

由于EditFormSettings使用了Template编辑模式,我认为您在这里使用RadGrid,因此您可以使用ItemUpdated事件代替标准按钮事件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