Home > OS >  Change next page based on choice user has made on previous page?
Change next page based on choice user has made on previous page?

Time:02-24

Apologies if this has been answered somewhere else, but I'm having trouble going about a website I've made using ASP.NET.

I'm trying to make a music player-type website, and have a page where users can choose an album to listen to. Once an album has been selected they are redirected to a different page where the music player is.

I want to make it where the album that has been selected on the previous page calls a function to fill in elements on the page. e.g., the album cover, album title etc. (using TagLib to retrieve the metadata from the audio files themselves.)

namespace Music_Streaming_Web_Application
{
    public partial class MusicPlayer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User selects imgbtnalbum1)
                    loadalbum1();
            else if (User selects imgbtnalbum2)
                    loadalbum2();
            else
                loadalbum3;
        }

        void loadalbum1() { }
        void loadalbum2() { }
        void loadalbum3() { }
    }
}

As you can see above, I have some empty functions which will be called based from an if statement which determines what button has been clicked-on on the previous page. Not too sure if this is the right way of going about it, but oh well.

How would I go about referring to asp page elements on a different aspx file?

CodePudding user response:

How would I go about referring to elements on a different aspx file?

You wouldn't. This is the wrong approach.

Whether by way of clicking a link which has values in its URL or by clicking a button which then builds some information server-side and redirects the user, either way your goal is to direct the user to the page with values in the URL that will tell the page what it needs to know.

For example, if the user is going to a page on which they would view an "album", then the URL for that page should have some way to identify that album. Something like this, for example:

/Album.aspx?album=123

The "123" can be anything you want. An ID which identifies an album in your data, the name of the album, etc. Maybe you even need the name of the artist too:

/Album.aspx?artist=Rush&album=Moving Pictures

Either way, the information is in the URL.

Then, the Album.aspx page will use those values when loading the page to read from data (a database, files on the filesystem, wherever your data is) and build its output to the user.

It's the job of the page to populate its own elements with what they need. Not the job of the page which directed the user there.


As an aside, you can follow this same approach to improve your code. After all, creating a method for every possible album certainly won't scale well. But imagine if the goal is just to do this:

Response.Redirect("/Album.aspx?album=123");

Then you can parameterize the "123" part:

Response.Redirect($"/Album.aspx?album={albumId}");

And pass that value into the operation:

public void LoadAlbum(int albumId)
{
    Response.Redirect($"/Album.aspx?album={albumId}");
}

Now you only have one method. The buttons on your page, however it is that you build them, should individually indicate which "album" they refer to. Clicking on the button should pass that information into this method.

From there you'll likely find that you don't need this method at all, you can just do this in the button click handler.

From there you'll likely find that you don't need a button at all, you can just use links to the URL instead of buttons that post back to this server-side code.

And you can continue to simplify, focusing the code on functionality and separating that from the data which that functionality uses.

  • Related