locked
multiple link buttons in one handler error RRS feed

  • Question

  • User1717218719 posted

    I have the following code which has an error "Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.CommandEventArgs'"

    I am unsure as to why this is happening, any help would be great.

    Many Thanks

      Public Sub lnkbttnApproved_Click(sender As Object, e As CommandEventArgs) Handles lnkbttnApproved.Click, lnkbttnPropsed.Click, lnkbttnAmend.Click, lnkbttnCancel.Click '---add all buttons
    
            Dim lnk As LinkButton = CType(sender, LinkButton)
    
    
            Select Case lnk.CommandName
    
                Case "Approved"
                    Call test(1)
    
                Case "Proposed"
                    Call test(2)
    
                Case "Amend"
                    Call test(3)
                Case "Cancel"
                    Call test(4)
            End Select
    
        End Sub



    Friday, October 4, 2019 11:05 AM

Answers

  • User-719153870 posted

    Hi E.RU,

    According to the code you provided, you are trying to add an OnCommand event rather than a click event for a linkbutton.

    If so, please refer to Link Button. On Command(CommandEventArgs) Method for more information and Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.CommandEventArgs' for your similar situation.

    For vb version, please also refer to below demo:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:LinkButton ID="lnkbttnApproved"  OnCommand="lnkbttnApproved_Command" CommandArgument="1" CommandName="Approved" runat="server">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="lnkbttnPropsed" OnCommand="lnkbttnApproved_Command" CommandArgument="1" CommandName="Proposed" runat="server">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="lnkbttnAmend" OnCommand="lnkbttnApproved_Command" CommandArgument="1" CommandName="Amend" runat="server">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="lnkbttnCancel" OnCommand="lnkbttnApproved_Command" CommandArgument="1" CommandName="Cancel" runat="server">LinkButton</asp:LinkButton>
            </div>
        </form>
    </body>
    </html>

    .vb:

    Private Sub test(ByVal i As Integer)
            Response.Write(i.ToString())
        End Sub
    
        Protected Sub lnkbttnApproved_Command(sender As Object, e As CommandEventArgs)
            Select Case e.CommandName
    
                Case "Approved"
                    Call test(1)
    
                Case "Proposed"
                    Call test(2)
    
                Case "Amend"
                    Call test(3)
                Case "Cancel"
                    Call test(4)
            End Select
        End Sub

    Here's the result of this demo:

    Hope this could help.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 2:31 AM

All replies

  • User409696431 posted

    For commands, you want onCommand, not onClick.

    e.g:

    <asp:LinkButton id="LinkButton1"  
               Text="Order Item 10001" 
               CommandName="Approved"  
               CommandArgument="xxx"  
               OnCommand="LinkButton_Command"  
               runat="server"/>

    and code behind (C# - use your VB.net version)

    void LinkButton_Command(Object sender, CommandEventArgs e)  
    { 
       //test for e.CommandName and e.CommandArgument values (if you use any) and add your logic
    } 

    Sunday, October 6, 2019 8:24 PM
  • User-719153870 posted

    Hi E.RU,

    According to the code you provided, you are trying to add an OnCommand event rather than a click event for a linkbutton.

    If so, please refer to Link Button. On Command(CommandEventArgs) Method for more information and Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.CommandEventArgs' for your similar situation.

    For vb version, please also refer to below demo:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:LinkButton ID="lnkbttnApproved"  OnCommand="lnkbttnApproved_Command" CommandArgument="1" CommandName="Approved" runat="server">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="lnkbttnPropsed" OnCommand="lnkbttnApproved_Command" CommandArgument="1" CommandName="Proposed" runat="server">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="lnkbttnAmend" OnCommand="lnkbttnApproved_Command" CommandArgument="1" CommandName="Amend" runat="server">LinkButton</asp:LinkButton>
                <asp:LinkButton ID="lnkbttnCancel" OnCommand="lnkbttnApproved_Command" CommandArgument="1" CommandName="Cancel" runat="server">LinkButton</asp:LinkButton>
            </div>
        </form>
    </body>
    </html>

    .vb:

    Private Sub test(ByVal i As Integer)
            Response.Write(i.ToString())
        End Sub
    
        Protected Sub lnkbttnApproved_Command(sender As Object, e As CommandEventArgs)
            Select Case e.CommandName
    
                Case "Approved"
                    Call test(1)
    
                Case "Proposed"
                    Call test(2)
    
                Case "Amend"
                    Call test(3)
                Case "Cancel"
                    Call test(4)
            End Select
        End Sub

    Here's the result of this demo:

    Hope this could help.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 2:31 AM