Transmit updated value to masterpage upon selection alteration

In my current project using ASP.NET, I have a MasterPage that includes a navigation bar with different options. One of the options leads to another page where the company now requires me to pass a parameter through the link. After some research, I managed to set up a property in the Main.Master class to handle this. However, the tricky part is that the parameter needs to be based on the value selected from a dropdown menu on the main page, and it should update the property as soon as a selection is made by the user.

The challenge arises because I can't utilize the onChange event directly due to the element not being an ASP component. Additionally, the element must remain unchanged for compatibility with an API (Select2) which requires it to be a regular HTML element. If there is a workaround for this issue, I would greatly appreciate any insights or suggestions.

I attempted to retrieve the selected value using JavaScript and then pass it to the master page through AJAX, but encountered difficulties as it only allowed me to call a static method. Unfortunately, a static method cannot make use of the MasterType reference required to modify the Main.Master file.

Therefore, I am seeking advice on whether there is a feasible solution to achieve the desired functionality. I apologize for not providing code snippets, as this information is considered confidential.

Answer №1

Absolutely, you have the capability to do so. Perhaps the simplest method available?

In your main menu (likely based on the bootstrap default menu), instead of using an href link, you can insert a menu button (using a link button).

For example, if we take a look at this menu markup:

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li><a runat="server" href="~/">Home</a></li>
        <li><a runat="server" href="~/About">About</a></li>
        <li><a runat="server" href="~/Contact">Contact</a></li>
        <li>
            <asp:LinkButton ID="LinkButton1" runat="server" Text="My Test Update" OnClick="LinkButton1_Click"></asp:LinkButton>
        </li>
    </ul>
</div>

You will see that you have standard menus with hrefs which direct without any additional code.

However, make note of the last menu item - "My Test Update."

This is a link button. Now you have an event stub in your master page code.

Here is an image of how it appears:

https://i.sstatic.net/vZrXO.png

I have also added a text box on that page. Like so:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %>.</h2>
    <p>Your app description page.</p>
    <p>Use this area to provide additional information.</p>


    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</asp:Content>

So far, everything is straightforward.

Now, how can we access the text box content? You can achieve this by adding the following code to the master page code-behind:

 Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)

    Dim MyContent As ContentPlaceHolder
    MyContent = Me.FindControl("MainContent")

    Dim MyTextBox As TextBox = MyContent.FindControl("TextBox1")

    Debug.Print("text box value = " & MyTextBox.Text)
    Response.Redirect("~/RadioFun.aspx")

End Sub

As shown above, it's easy to retrieve the text box value. Next, how can you pass this value to the next page? You could use session variables or include the value as a parameter in the URL you navigate to, among other options.

By swapping out the menu navigation from an href to a link button, the appearance remains the same but now you have event-handling code in your master page code. With the provided example, it's simple to extract values and then utilize them accordingly. One suggestion is to store the value in session() and proceed to the linked page upon clicking the menu button (link button). Alternatively, consider utilizing PostBackURL. This setting in a button/control not only facilitates page navigation without requiring event-handling code behind but also grants access to the previousPage property, enabling retrieval of values from the entire preceding form or page.

Either of these approaches will prove effective.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using switch statement upon page loading in AngularJS

I am currently working on refactoring a switch statement that identifies specific classes upon page load into one of my controllers. Does anyone have suggestions on how I can transform this straightforward task using Angular? $('.radior').eac ...

JavaScript code altered link to redirect to previous link

I added a date field to my HTML form. <input type="date" name="match_date" id="matchDate" onchange="filterMatchByDate(event)" min="2021-01-01" max="2021-12-31"> There is also an anchor tag ...

Tips on how to update the status variable to true depending on the index value

Once I click on the close button, the value immediately changes to "Fruit." How can I achieve this? For instance: Apple close Grapes close Pineapples close Alternatively, is there a way to set the state of "cancel" to true ...

Using jqGrid to load additional JSON data after the initial local data has already been populated in the

I encountered a specific issue: I have a compact form with four choices. Users can choose to fill them out or not, and upon clicking 'Ok', a jqGrid is loaded with data based on those selections. To accommodate dynamic column formatting, my servle ...

Engaging JavaScript Navigation

I am looking to create an interactive JavaScript menu or image map where users can press down, highlight, and hit enter on four different items to reveal hidden messages. However, I struggle with JavaScript and could really use some assistance. I have alr ...

Submitting form based on HTML select input issue

I am facing an issue with a select form where the value resets to "10" every time I send the form. Is there a way to keep the selected option, for example "20", after submitting the form? Below is the code snippet: <form method='get' name=&a ...

Learn how to display only certain elements when triggered by the click of another

I have developed a tab system where each tab contains a unique set of questions and answers. The structure of the tabs is exactly as I envisioned, but I am facing challenges with toggling the display of answers when their respective questions are clicked. ...

Transforming an unconventional date format into a proper date representation

I have a spreadsheet with over 100,000 dates stored in the following format: Thursday 29th of October 2015 01:06:21 PM Converting these dates into a usable format is proving to be a challenge. Whether it's YYYY/MM/DD or any other standard format, I ...

Finding your way to a particular section within a webpage through an external source

Hey there! I'm currently working on creating a link that will direct users to a specific section within my webpage. For example, redirecting them to https://blabla.github.io/my-website. My code is quite straightforward and it functions properly when ...

Oops! It seems like there was an issue trying to access a property that doesn't exist (specifically, the

Encountering an error on the HomeScreen of my project I aim to manipulate the state of my HomeScreen Page using redux. The data is fetched from an API (an array of items) and then displayed on the screen. However, despite all these processes, an error me ...

Forward to the .pdf document

When I visit local.com/test/document, I am trying to redirect to local.com/document/demo.pdf. The redirection is working properly and the demo.pdf file is being downloaded. However, the URL in the browser remains unchanged. The code I am using for the re ...

Utilizing a mutual RxJS subject for seamless two-way data binding in Angular 2

I have a unique service dedicated to managing app configurations class Configuration { get setting() { return dataStore.fetchSetting(); } set setting(value) { dataStore.saveSetting(value); } } This configuration is linked to components t ...

jQuery - How come my string values are getting cut off?

It's been a while since I last worked with jQuery, and I seem to be missing something obvious that's affecting my calculations. I have various text boxes for Weight, Moisture %, and Number of Filled Squares, as well as a multi-select option for ...

Using javascript to locate and substitute a word divided among multiple tags - a step-by-step guide

I need to utilize JavaScript to locate and substitute a word that has been separated into multiple tags. For instance, consider the following HTML code: <html> <body> <div id="page-container"> This is an apple. ...

Angular and Express: Automatically redirecting to the previous page after user login

A question similar in nature can be found here, although not directly relevant to my situation. Within my Single Page Application, I am implementing PassportJS for user authentication. There are multiple routes that necessitate user login. The routing is ...

Sorry, but we couldn't complete your request: User verification unsuccessful: email must be provided

Every time I attempt to save a user's credentials to the mongo database, an error pops up saying: "User validation failed: email: Path email is required." I am clueless as to why this issue keeps happening. It only started occurring when I added the v ...

What is the best way to adjust image size based on different screen sizes while ensuring the buttons at the top remain responsive

How can I make the image and image select and delete buttons responsive? I am looking to eliminate the gap after the delete button. Any suggestions are welcomed. <div class="container mt-0"> <div class="row"> ...

Exploring request parameters within an Express router

I'm currently facing an issue with accessing request parameters in my express router. In my server.js file, I have the following setup: app.use('/user/:id/profile', require('./routes/profile')); Within my ./routes/profile.js fil ...

Stacking items when clicked on with jQuery UI Draggable

Can a draggable's stack procedure be activated by clicking instead of just dragging? I came across this solution, which essentially replicates the library code (with an important addition I included below). Is there a more elegant approach to achieve ...

What are the steps to resolve the error "TypeError: req.next is not a function"?

I've been working on a project and keep encountering this persistent error that I can't seem to fix. I understand the issue, but unfortunately, I'm at a loss as to how to resolve it. Here is the error message: events.js:292 throw er; ...