How to handle a click of a hyperlink
-
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
-
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
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
-
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
-
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
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
-
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 . . . . .
-
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 . . . . .
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
andCommandArgument
. That way, when you get the handler method, you can pull out theCommandArgument
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.
-
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
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