Results 1 to 14 of 14

Thread: [RESOLVED] [02/03] Click Events Problem (Very Easy)

  1. #1

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Resolved [RESOLVED] [02/03] Click Events Problem (Very Easy)

    Hi Guys!!!

    How do I know which button fired the event button.

    Let's say I have:

    Button1
    Button2
    Button3

    Their click events calls the TestClick Procedure.

    How do I know if Button1 Called TestClick Procedure.

    Thanks in advance.

  2. #2

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: [02/03] Click Events Problem (Very Easy)

    Nobody!!!!

  3. #3

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: [02/03] Click Events Problem (Very Easy)

    Is there a property like IsClicked or something that gets the control that is clicked?

  4. #4
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: [02/03] Click Events Problem (Very Easy)

    So you have 3 buttons that all call the same function and you want to know which button was clicked?

    You can use the buttons CommandArgument attributes and then check this value to work out which button was clicked.
    VB Code:
    1. Private Sub Button_Command(ByVal sender As System.Object, ByVal e As CommandEventArgs)
    2.         Dim test As String = e.CommandArgument()
    3.     End Sub

  5. #5

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: [02/03] Click Events Problem (Very Easy)

    Quote Originally Posted by Fishcake
    So you have 3 buttons that all call the same function and you want to know which button was clicked?

    You can use the buttons CommandArgument attributes and then check this value to work out which button was clicked.
    VB Code:
    1. Private Sub Button_Command(ByVal sender As System.Object, ByVal e As CommandEventArgs)
    2.         Dim test As String = e.CommandArgument()
    3.     End Sub
    Hmm... there is no CommandArgument in a LinkButton Control.

    What the equivalent of CommandArgument in a LinkButton?

  6. #6
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: [02/03] Click Events Problem (Very Easy)

    All buttons (link button, image button etc....) have a CommandArgument.

    Try copying and pasting this...
    Code:
    <asp:LinkButton ID="btnLink" OnCommand="Command_Test" CommandArgument="1" Runat="server" text="Link Button"></asp:LinkButton>
    VB Code:
    1. Public Sub Command_Test(ByVal sender As System.Object, ByVal e As CommandEventArgs)
    2.         Dim Test As String = e.CommandArgument
    3.     End Sub

  7. #7
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [02/03] Click Events Problem (Very Easy)

    Can you post the code what you have done so far?
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  8. #8

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: [02/03] Click Events Problem (Very Easy)

    So far, I have this.

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.         Dim c As New TextBox
    4.         Automaton.CreateTable(Table1, 5, 5)
    5.         Table1.Rows(0).Cells(0).Controls.Add(a)
    6.         Table1.Rows(1).Cells(0).Controls.Add(b)
    7.         Table1.Rows(0).Cells(1).Controls.Add(c)
    8.         ViewState.Add("Test", c.Text)
    9.         c.Text = viewstate.Item("Test")
    10.         a.Text = "Test Link"
    11.         b.Text = "B Link"
    12.         a.CommandArgument = "1"
    13.     End Sub
    14.  
    15.     Public Sub Test(ByVal sender As System.Object, ByVal e As CommandEventArgs)
    16.         Dim d As String
    17.         d = e.CommandArgument
    18.         Response.Write("Yes!!!")
    19.     End Sub

  9. #9

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: [02/03] Click Events Problem (Very Easy)

    Quote Originally Posted by Fishcake
    All buttons (link button, image button etc....) have a CommandArgument.

    Try copying and pasting this...
    Code:
    <asp:LinkButton ID="btnLink" OnCommand="Command_Test" CommandArgument="1" Runat="server" text="Link Button"></asp:LinkButton>
    VB Code:
    1. Public Sub Command_Test(ByVal sender As System.Object, ByVal e As CommandEventArgs)
    2.         Dim Test As String = e.CommandArgument
    3.     End Sub
    Hmmm....

    How do you call OnCommand in code?

    Or how do you get "Command_Test" in code?

  10. #10
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: [02/03] Click Events Problem (Very Easy)

    VB Code:
    1. a.Command += New CommandEventHandler(test)

  11. #11

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: [02/03] Click Events Problem (Very Easy)

    Quote Originally Posted by Fishcake
    VB Code:
    1. a.Command += New CommandEventHandler(test)
    I don't have withevents declared and I can't use withevents.

    I get an error message that I can't directly call a.Command

  12. #12
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: [02/03] Click Events Problem (Very Easy)

    Sorry, I've been working in C# and converting it from memory (although looking back I'm not sure why I thought you were using vb in the first place).

    Here's what i do in C#
    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
            LinkButton a = new LinkButton();
            a.Command += new CommandEventHandler(TestCommand);
            a.Text = "Click Me";
            this.plhTest.Controls.Add(a);
        }
    
        private void TestCommand(object sender, CommandEventArgs e)
        {
            this.lbltest.Text = "Test";
        }
    I can't remember how to add event handlers in vb.net....something to do with AddressOf?

  13. #13

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: [02/03] Click Events Problem (Very Easy)

    Quote Originally Posted by Fishcake
    Sorry, I've been working in C# and converting it from memory (although looking back I'm not sure why I thought you were using vb in the first place).

    Here's what i do in C#
    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
            LinkButton a = new LinkButton();
            a.Command += new CommandEventHandler(TestCommand);
            a.Text = "Click Me";
            this.plhTest.Controls.Add(a);
        }
    
        private void TestCommand(object sender, CommandEventArgs e)
        {
            this.lbltest.Text = "Test";
        }
    I can't remember how to add event handlers in vb.net....something to do with AddressOf?
    OH MY GOD!!!!!!!!!! THANK YOU THANK YOU THANK YOU!!!

    I've been searching the whole day for this kind of solution.

    I'M NOT WORTHY!!!!!!


  14. #14
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: [RESOLVED] [02/03] Click Events Problem (Very Easy)

    You're welcome. A response like that makes it all seem worthwhile

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width