Announcement

Collapse
No announcement yet.

ASPX: Argument passing to button click handler

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    ASPX: Argument passing to button click handler

    Hi all,

    I'm new to ASPX programming so please forgive me if this is simple.

    I'm trying to create a page that has several buttons on it and I want to have a single subroutine to handle all of the button clicks. I want to pass "CommandName" and "CommandArgument" parameters to the subroutine but when I code this up I get a "Server Runtime Error" when I click on a button.

    Any help would be greatly appreciated.

    Thanks,
    Ken

    Here's my code:

    Code:
     
        Protected Sub Button_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
            Response.Write("Button_Click: " & e.CommandArgument)
        End Sub
    PHP Code:

    <asp:Button ID="Button1" runat="server" Text="Open" onclick="Button_Click" CommandArgument="open" CommandName="router" />
    <
    asp:Button ID="Button2" runat="server" Text="Close" onclick="Button_Click" CommandArgument="open" CommandName="router" /> 
    "if I have seen further [than others], it is by standing on the shoulders of giants." --Sir Isaac Newton (1675)

    #2
    More data...

    So I was able to turn off customErrors and I got the following error message:

    "Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.CommandEventArgs'."

    This makes some sense since I changed "ByVal e As System.EventArgs" to "ByVal e As CommandEventArgs".

    So my question now is "How do I get to 'CommandName' and 'CommandArgument' from 'System.EventArgs'?"

    As you can plainly see I don't really know what I'm doing here.

    Thanks,
    Ken
    "if I have seen further [than others], it is by standing on the shoulders of giants." --Sir Isaac Newton (1675)

    Comment


      #3
      Code:
      Protected Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
              Dim aButton As Button
              aButton = CType(sender, Button)
              Response.Write("Button_Click: " & aButton.CommandArgument)
          End Sub
      tenholde
      tenholde

      Comment


        #4
        Thank You!

        One more question...

        Is the "Handles Button1.Click" at the end necessary? It seems like it works without it and since I want to have Button1, Button2, Button3, etc. would that cause any problems?

        Thanks again,
        Ken
        "if I have seen further [than others], it is by standing on the shoulders of giants." --Sir Isaac Newton (1675)

        Comment


          #5
          Not necessary as you reference the subroutine in the HTML.

          tenholde
          tenholde

          Comment

          Working...
          X