Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to handle a click of a hyperlink

How to handle a click of a hyperlink

Scheduled Pinned Locked Moved ASP.NET
csharphtmlgraphicstutorial
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    widget1
    wrote on last edited by
    #1

    I have created a table on my page dynamically and in each cell I want to create a hyperlink that is invisible to the user but one 'clicked' will allow me to program the event in my code behind page which is vb.net? I cannot access the html code of the hyperlink as it is created on the fly. From what I understand does the hyperlink not have an onclick event that can be responded to? I am trying to mimic an excel spreadsheet with my table and want the user to click in a cell and have the background change to black to make the cell look selected. I have this: (this is just part of it) MyTable.Rows(i + 1).Cells(k + 1).BackColor = System.Drawing.Color.Black Dim strHello As String strHello = "Edit" Dim e_link As HyperLink = New HyperLink MyTable.Rows(i + 1).Cells(k + 1).Controls.Add(e_link) If IsPostBack Then MyTable.Rows(i + 1).Cells(k + 1).HorizontalAlign = HorizontalAlign.Center e_link.ForeColor() = System.Drawing.Color.Black e_link.Text = strHello AddHandler e_link.Click, AddressOf e_link_Click with this event handler placed lower Private Sub e_link_Click(ByVal sender As Object, ByVal e As EventArgs) Response.Write("You clicked Me!") End Sub Thanks

    R D P 3 Replies Last reply
    0
    • W widget1

      I have created a table on my page dynamically and in each cell I want to create a hyperlink that is invisible to the user but one 'clicked' will allow me to program the event in my code behind page which is vb.net? I cannot access the html code of the hyperlink as it is created on the fly. From what I understand does the hyperlink not have an onclick event that can be responded to? I am trying to mimic an excel spreadsheet with my table and want the user to click in a cell and have the background change to black to make the cell look selected. I have this: (this is just part of it) MyTable.Rows(i + 1).Cells(k + 1).BackColor = System.Drawing.Color.Black Dim strHello As String strHello = "Edit" Dim e_link As HyperLink = New HyperLink MyTable.Rows(i + 1).Cells(k + 1).Controls.Add(e_link) If IsPostBack Then MyTable.Rows(i + 1).Cells(k + 1).HorizontalAlign = HorizontalAlign.Center e_link.ForeColor() = System.Drawing.Color.Black e_link.Text = strHello AddHandler e_link.Click, AddressOf e_link_Click with this event handler placed lower Private Sub e_link_Click(ByVal sender As Object, ByVal e As EventArgs) Response.Write("You clicked Me!") End Sub Thanks

      R Offline
      R Offline
      RichardGrimmer
      wrote on last edited by
      #2

      It would probably be simpler to achieve this using client side scripting. The problem will be that a) no OnClick event, and b) even if you do manage to catch SOMETHING, it'll need a postback to update the cell colour. If you were to do it on the client side, you can assign an onclick handler to each element, then use a simple style.background='#xxxxxx' kinda thing to change the cell's background color. "Now I guess I'll sit back and watch people misinterpret what I just said......" Christian Graus At The Soapbox

      W 1 Reply Last reply
      0
      • W widget1

        I have created a table on my page dynamically and in each cell I want to create a hyperlink that is invisible to the user but one 'clicked' will allow me to program the event in my code behind page which is vb.net? I cannot access the html code of the hyperlink as it is created on the fly. From what I understand does the hyperlink not have an onclick event that can be responded to? I am trying to mimic an excel spreadsheet with my table and want the user to click in a cell and have the background change to black to make the cell look selected. I have this: (this is just part of it) MyTable.Rows(i + 1).Cells(k + 1).BackColor = System.Drawing.Color.Black Dim strHello As String strHello = "Edit" Dim e_link As HyperLink = New HyperLink MyTable.Rows(i + 1).Cells(k + 1).Controls.Add(e_link) If IsPostBack Then MyTable.Rows(i + 1).Cells(k + 1).HorizontalAlign = HorizontalAlign.Center e_link.ForeColor() = System.Drawing.Color.Black e_link.Text = strHello AddHandler e_link.Click, AddressOf e_link_Click with this event handler placed lower Private Sub e_link_Click(ByVal sender As Object, ByVal e As EventArgs) Response.Write("You clicked Me!") End Sub Thanks

        D Offline
        D Offline
        Dustin Metzgar
        wrote on last edited by
        #3

        You could always use a LinkButton.


        Logifusion[^]

        W 1 Reply Last reply
        0
        • W widget1

          I have created a table on my page dynamically and in each cell I want to create a hyperlink that is invisible to the user but one 'clicked' will allow me to program the event in my code behind page which is vb.net? I cannot access the html code of the hyperlink as it is created on the fly. From what I understand does the hyperlink not have an onclick event that can be responded to? I am trying to mimic an excel spreadsheet with my table and want the user to click in a cell and have the background change to black to make the cell look selected. I have this: (this is just part of it) MyTable.Rows(i + 1).Cells(k + 1).BackColor = System.Drawing.Color.Black Dim strHello As String strHello = "Edit" Dim e_link As HyperLink = New HyperLink MyTable.Rows(i + 1).Cells(k + 1).Controls.Add(e_link) If IsPostBack Then MyTable.Rows(i + 1).Cells(k + 1).HorizontalAlign = HorizontalAlign.Center e_link.ForeColor() = System.Drawing.Color.Black e_link.Text = strHello AddHandler e_link.Click, AddressOf e_link_Click with this event handler placed lower Private Sub e_link_Click(ByVal sender As Object, ByVal e As EventArgs) Response.Write("You clicked Me!") End Sub Thanks

          P Offline
          P Offline
          patt
          wrote on last edited by
          #4

          As far as I can tell there's no click event associated to the Hyperlink webcontrol. There is a ServerClick event for the HtmlAnchor control however, that will qualify for what you're trying to do. Personally I'd try to work with the Attributes collection of the HyperLink or the Style collection of the table cell. Hope this helps, Patt

          1 Reply Last reply
          0
          • D Dustin Metzgar

            You could always use a LinkButton.


            Logifusion[^]

            W Offline
            W Offline
            widget1
            wrote on last edited by
            #5

            Ok I have switched out my hyperlink for a linkbutton as such: Dim e_link As LinkButton = New LinkButton MyTable.Rows(i + 1).Cells(k + 1).Controls.Add(e_link) Now how do I actually test if the button was clicked? I am not sure how to handle events in vb.net as much as c#. I have this line: AddHandler e_link.Click, AddressOf e_link_Click With this sub below: Private Sub e_link_Click(ByVal sender As Object, ByVal e As EventArgs) Response.Write("You clicked Me!") End Sub How to I actually call the event based on whether the link is clicked? Sorry if this sounds very simple - I am new to all of this. I want to check that the cell(linkbutton) was clicked and if so perform my event. Something like : if e_link.click = true then . . . . .

            D 1 Reply Last reply
            0
            • W widget1

              Ok I have switched out my hyperlink for a linkbutton as such: Dim e_link As LinkButton = New LinkButton MyTable.Rows(i + 1).Cells(k + 1).Controls.Add(e_link) Now how do I actually test if the button was clicked? I am not sure how to handle events in vb.net as much as c#. I have this line: AddHandler e_link.Click, AddressOf e_link_Click With this sub below: Private Sub e_link_Click(ByVal sender As Object, ByVal e As EventArgs) Response.Write("You clicked Me!") End Sub How to I actually call the event based on whether the link is clicked? Sorry if this sounds very simple - I am new to all of this. I want to check that the cell(linkbutton) was clicked and if so perform my event. Something like : if e_link.click = true then . . . . .

              D Offline
              D Offline
              Dustin Metzgar
              wrote on last edited by
              #6

              Well, what you're doing with the event handler sounds good to me. I'm also a C# programmer starting to dabble in VB, so I'm not 100% sure. All I would suggest you add is a CommandName and CommandArgument. That way, when you get the handler method, you can pull out the CommandArgument which will have whatever pertinent information you wanted to be attached to that link. For instance, you could be the index of the cell in there.


              Logifusion[^]

              1 Reply Last reply
              0
              • R RichardGrimmer

                It would probably be simpler to achieve this using client side scripting. The problem will be that a) no OnClick event, and b) even if you do manage to catch SOMETHING, it'll need a postback to update the cell colour. If you were to do it on the client side, you can assign an onclick handler to each element, then use a simple style.background='#xxxxxx' kinda thing to change the cell's background color. "Now I guess I'll sit back and watch people misinterpret what I just said......" Christian Graus At The Soapbox

                W Offline
                W Offline
                widget1
                wrote on last edited by
                #7

                I want to test the condition of whether or not the cell was clicked in and if it is presently a white background then change it to black to signify that it was selected. How can I write that in vb.net? If the buttonlink does not yet exist untilt he page loads how can I access it from client script? Sorry if I appear stupid - I just may be. . . . . but hopefully just new to it all. I have this so far: If e_link.OnClick = True Then if (e_link.ForeColor() = System.Drawing.Color.Black)then e_link.ForeColor() = System.Drawing.Color.White End If End if I have an error on this line: e_link.OnClick can you test this at all? Thx, tammy

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups