tags:

views:

1268

answers:

4

I have a linkbutton in a column of a Gridview.

When the user clicks the linkbutton, I have to redirect to another page.

Can anyone give appropriate code to accomplish this?

A: 

Set the PostBackUrl property to the URL where you want to go.

Fabian Vilers
A: 

Another way is to use a response.redirect or server.transfer in thew click event of the link button, like so:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Response.Redirect("Page2.aspx")
    End Sub

    Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
        Server.Transfer("Page2.aspx")
    End Sub
End Class

The difference is basically that response.redirect shows the new URL, server.transfer does not.

BenB
A: 
     <asp:LinkButton ID="sessionLink" runat="server" Text='Tickets'
    PostBackUrl="SelectTickets.aspx"/>

When you specify the PostBackUrl property then your onclick event will never get fired. It's just redirect you to the other page. What you can do is in your on click event add the below statement and remove the postbackurl property as:

      <asp:LinkButton ID="sessionLink" runat="server" Text='Tickets'
    OnClick="selectTickets" />

which calls the selectTickets function in code behind, so you must make a function in your code behind as so:

Private Sub selectTickets(ByVal sender As Object, ByVal e As CommandEventArgs) 
     'whatever else you want to be done 
      Response.Redirect("SelectTickets.aspx") 
 End Sub
TStamper
+1  A: 

If all you need to do is redirect to another page, why not use a HyperLinkField?

You can set url parameters and text with your data object fields like so:

<asp:hyperlinkfield datatextfield="UnitPrice"
            datatextformatstring="{0:c}"
            datanavigateurlfields="ProductID"
            datanavigateurlformatstring="~\details.aspx?ProductID={0}"          
            headertext="Price"
            target="_blank" />
jrummell
didn't know about hyperlinkfield..thx
Juri