views:

149

answers:

4

I know it's a simple question but I really can't find anything on Google. Sorry if I'm not searching right. I created 2 pages and in the first one I have a button.
What should I write in the C# code to change to redirect me on the second page?
I usually know my way around C# but I'm totally new in ASP.

+9  A: 

Add the button onclick event handler.

In the event handler put: Response.Redirect("YOUR_NEW_PAGE");

Response.Redirect: http://msdn.microsoft.com/en-us/library/ms524309(VS.90).aspx

or Server.Transfer:
http://www.developer.com/net/asp/article.php/3299641/ServerTransfer-Vs-ResponseRedirect.htm

Slightly more complicated, and probably not what you need a cross page post:
http://msdn.microsoft.com/en-us/library/ms178139.aspx

Kevin
Both your method and Mask Mayo's work out nice.
Sanctus2099
@Kevin: +1 for mentioning CPP.
James
Yes. Actually he answered what whould have probably been my next question with that CPP article. Thanks again.
Sanctus2099
+3  A: 

Not quite sure from your question whether you're after ASP VB or C#...so...

// C#

private void Button1_Click(object sender, System.EventArgs e)
{
   Server.Transfer("Webform2.aspx");
}

' Visual Basic

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Server.Transfer("Webform2.aspx")
End Sub

For more information, I direct you to:

http://msdn.microsoft.com/en-us/library/540y83hx%28VS.71%29.aspx

Mark Mayo
I did write C# and the tag is also C#. Thank you very much :)
Sanctus2099
+1  A: 

You can also do this in the aspx itself (without writing any code) by using the PostBackUrl property of the button.

adrianos
+1  A: 

Use one of these methods:

One-time redirect (HTTP 301)

Response.Redirect("page to redirect to");

Permanent redirect (HTTP 302), only available in ASP.NET 4.0

Response.RedirectPermanent("page to redirect to");
Venemo