42

How do I code the button such that when I click the button and it brings me to another web form? Let's say the button name is Confirm and the wed form is confirm.aspx ?

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        (guessing that there should be an input here)
    }
dotNET
  • 33,414
  • 24
  • 162
  • 251
user3290048
  • 425
  • 1
  • 4
  • 8

4 Answers4

65

You can either do a Response.Redirect("YourPage.aspx"); or a Server.Transfer("YourPage.aspx"); on your button click event. So it's gonna be like the following:

protected void btnConfirm_Click(object sender, EventArgs e)
{
    Response.Redirect("YourPage.aspx");
    //or
    Server.Transfer("YourPage.aspx");
}
Rex
  • 1,134
  • 15
  • 26
15

You can use PostBackUrl="~/Confirm.aspx"

For example:

In your .aspx file

<asp:Button ID="btnConfirm" runat="server" Text="Confirm" PostBackUrl="~/Confirm.aspx" />

or in your .cs file

btnConfirm.PostBackUrl="~/Confirm.aspx"

  • What's the advantage of using btn.PostBackUrl versus Response.Redirect? – Marwan مروان Feb 04 '15 at 20:57
  • 5
    check this https://www.daniweb.com/web-development/aspnet/threads/69851/postbackurl-vs-response-redirect basically PostBackUrl is meant to be used with in same server because it will save viewState. Response.Redirect can take you to a brand new server/website where brand new session will start –  Feb 04 '15 at 21:37
9

u can use this:

protected void btnConfirm_Click(object sender, EventArgs e)
{
  Response.Redirect("Confirm.aspx");
}
yusefnejad
  • 176
  • 2
1
<button type ="button" onclick="location.href='@Url.Action("viewname","Controllername")'"> Button name</button>

for e.g ,

<button type="button" onclick="location.href='@Url.Action("register","Home")'">Register</button>
Ersoy
  • 8,816
  • 6
  • 34
  • 48
Soniya
  • 21
  • 1