11

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.

Venemo
  • 18,515
  • 13
  • 84
  • 125
Sanctus2099
  • 1,669
  • 5
  • 22
  • 40
  • How do you write ASP in C#? I've seen C# used for ASP.Net but that isn't the same as ASP to my mind. There should be a form tag if you want to change where the submitted form goes I believe. – JB King May 21 '10 at 14:17
  • @JB King: I think he means asp.net, it's just a pain in the ass to keep adding ".net" every time. – Roman May 21 '10 at 14:29
  • 1
    That's what I thought, but sometimes it is better to ask and get that clarified than assume and step on someone's toes. – JB King May 21 '10 at 14:42
  • 1
    Sorry. I meant Asp.Net but as R0MANARMY said it's a pain to keep adding ".net" every time and that's why I forgot. – Sanctus2099 May 21 '10 at 16:04
  • Possible duplicate of [aspx page to redirect to a new page](http://stackoverflow.com/questions/1093081/aspx-page-to-redirect-to-a-new-page) – Michael Freidgeim Jan 30 '17 at 06:41

4 Answers4

22

Add the button onclick event handler.

In the event handler put:

Response.Redirect("YOUR_NEW_PAGE");

Response.Redirect or Server.Transfer

Slightly more complicated, and probably not what you need a cross page post

Malachi
  • 3,205
  • 4
  • 29
  • 46
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
4

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
  • 12,230
  • 12
  • 54
  • 85
3

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
  • 18,515
  • 13
  • 84
  • 125
2

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

adrianos
  • 1,501
  • 2
  • 18
  • 22
  • I know that this is old but thanks @adrianos, not that my error stopped my program but it was just annoying having the error in log4net. – codeMonger123 May 23 '16 at 18:27