Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET and C# Redirect

Tags:

I am working on a project for school, and this is an extra credit part. I have a project started in VS 2010 using master pages, and what I'm trying to do is get a "Submit" button to redirect people to the "MyAccounts.aspx" page. My current code for the ASP part for the button looks like this:

<asp:Button ID="btnTransfer" runat="server" Text="Submit"/>

I have tried adding in the OnClick option, as well as the OnClientClick option. I have also added this code to the Site.Master.cs file as well as the Transfer.aspx.cs file:

protected void btnTransfer_Click(object sender, EventArgs e)
{
    Response.Redirect(Page.ResolveClientUrl("/MyAccounts.aspx"));
}

When I run this and view the project in my browser, the whole thing runs fine, but when I click on the "Submit" button, it just refreshes the current page and does not properly redirect to the MyAccounts page. Anyone have any ideas for me?

like image 214
Skrubb Avatar asked Apr 13 '13 02:04

Skrubb


People also ask

Is ASP.NET a C?

ASP.NET is part of the . NET framework allowing you to write web applications using any CLS compliant language such as C#, VB.NET, F#, ... What you are referring to original asp language is called Classic ASP and it is not a language.

What is ASP.NET and C#?

ASP.NET is a web application development framework used to develop web applications using different back-end programming languages like C# where C# is used as an object-oriented programming language to develop web applications along with ASP.NET.

Is .NET and C same?

C# is a programming language, . NET is a blanket term that tends to cover both the . NET Framework (an application framework library) and the Common Language Runtime which is the runtime in which . NET assemblies are run.

Can I use .NET with C?

. NET Framework is an object oriented programming framework meant to be used with languages that it provides bindings for. Since C is not an object oriented language it wouldn't make sense to use it with the framework.


1 Answers

You are doing it almost correctly, you just haven't put the correct pieces together. On Transfer.aspx, your button should be:

<asp:Button ID="btnTransfer" OnClick="btnTransfer_Click" runat="server" Text="Submit"/>

and your code behind should be like what @KendrickLamar said:

protected void btnTransfer_Click(object sender, EventArgs e)
{
    Response.Redirect("~/MyAccounts.aspx");
}

The OnClick event tells it what to execute on post-back when the users clicks the button. This is in the code-behind for Transfer.aspx, not the site master.

like image 68
MikeSmithDev Avatar answered Sep 20 '22 15:09

MikeSmithDev