44

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)
    }
1

4 Answers 4

66

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");
}
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"

2
  • What's the advantage of using btn.PostBackUrl versus Response.Redirect? Commented Feb 4, 2015 at 20:57
  • 5
    check this daniweb.com/web-development/aspnet/threads/69851/… 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
    – user3618774
    Commented Feb 4, 2015 at 21:37
9

u can use this:

protected void btnConfirm_Click(object sender, EventArgs e)
{
  Response.Redirect("Confirm.aspx");
}
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>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.