Friday, November 20, 2009

ASP.Net LinkButton or HyperLink, read on...?

I need to capture the "click event" of a hyperlink, but there is none..so I try using a linkbutton but there doesnt seem to be a URL property, maybe I could capture the click and manually redirect (but I need to specify a frame/target..)



It's p1ss1ng me right off, im a bit of a noob at ASP, but know my way around VB/VB.NET



What's the best solution to this problem?



ASP.Net LinkButton or HyperLink, read on...?windows xp pro



This really depends on what you are trying to accomplish. If you want a basic anchor that is opened in a new window that is essentially static, then just put the HTML on the page. When I say static I mean there is nothing being created as the page is being created by the ASP.NET engine, like from a database.



If so, then maybe the Hyperlink is the best solution. Here you specify the NavigateURL and the Target, just like a normal anchor tag.



%26lt;asp:HyperLink ID="HyperLink1" runat="server" Font-Bold="True" ForeColor="Purple"



NavigateUrl="http://www.yahoo.com" Target="Yahoo"%26gt;Yahoo%26lt;/asp:HyperLink%26gt;



My example is not data driven, but good enough for here.



A LinkButton should be used if you need to do some processing, like some business logic before you send the user to a new page.



%26lt;asp:LinkButton ID="LinkButton1" runat="server" Font-Bold="True" ForeColor="Purple"%26gt;Yahoo Post Back%26lt;/asp:LinkButton%26gt;



Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click



'Process the request here



'



'Redirect them to a new page



Response.Redirect("http://www.yahoo.com"...



End Sub



The response.redirect will not let you open in a new browser since this is a client-side effect. I hope this helps.



ASP.Net LinkButton or HyperLink, read on...?microsoft net framework internet explorer



The linkbutton is the one to use. You can use Server.Transfer or Response.Redirect to send then to the URL.



If the target page is an aspx page, you can put a literal n the top of the page. Use something like this in the page load...



If not page.ispostback then



myLiteral.Text = "javaascript to set focus where you want"



Else



myLiteral.Text = ""



End If



There are other ways to do this, but this is the first one that came to my head.
I don't know enough about your problem to provide you a specific answer, mostly I'm not sure what you mean to do with your hyperlink.



Here's what you are missing about a link button:



The link button will do a "post back" and allow you to handle processing on the server before the page reloads.



If you are just trying to load content of a page into a frame - just use a normal html hyperlink.



If you need more help feel free to email me.



I've been developing in ASP.NET for over 2.5 years
check out http://www.pscode.com for great sample codes. It might even have a way to create a CUSTOM EVENT PROCEDURE to capture the link's CLICK EVENT.

No comments:

Post a Comment

 
nortin