Efficiently managing and saving properties across User Controls

Currently, I am working on an ASP.NET application that utilizes a collection of UserControls.

Within this application, there is a page that displays various custom UserControls. One of these controls, known as ucA, includes a small JavaScript Popup that renders another UserControl, referred to as ucB. In ucA, I have defined a public property that interacts with a hidden field also defined within ucA:

<asp:HiddenField ID="hidWorkDirName" runat="server" />

The property is defined as follows:

public string _hidWorkDirName
{
    get { return hidWorkDirName.Value; }
    set { hidWorkDirName.Value = value; }
}

In ucB, there is a TextBox that, upon submission, should update the value of hidWorkDirName:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    ucA parent = (ucA)this.Parent; //referring to ucB in this context
    parent._hidWorkDirName = txtName.Text; //updating the TextBox value in ucA
}

During debugging, I can observe that the value is being successfully updated.

However, in ucA, there is a separate Submit button (different from the one in

ucB</code) where I need to retrieve the value stored in <code>hidWorkDirName
. Yet, despite multiple attempts, the retrieved value always appears as an empty string, as if it was never set.

I have attempted to access the value directly from the hidden field and through the property (_hidWorkDirName) itself but I cannot seem to retrieve the previously set value.

What could be causing this issue?

Answer №1

The reason for this issue is the potential reset of the Hiddenfield named hidWorkDirName during the execution of the Page_Load event. To overcome this, it is recommended to opt for an alternative method involving the use of ViewState.

Below is a revised property utilizing ViewState:

public string _hidWorkDirName
{
    get
    {
        if (ViewState["WorkDirName"] != null)
        {
            return (string)ViewState["WorkDirName"];
        }
        return string.Empty;
    }
    set
    {
        ViewState["WorkDirName"] = value;
    }
}

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

Updates to .NET 5.0 impacting how `IEnumerable<T>.OrderBy` handles string comparisons

I've written the following code snippet public class Model { public int Id { get; set; } public string OrderNumber { get; set; } } class Program { static void Main(string[] args) { var m ...

The speed of CSS loading is sluggish when navigating to a new page without refreshing

Every time I try to load a different page with refresh using jQuery and window.history.pushState('','',url); function goto(event,el) { url=el.href; event.preventDefault(); window.history.pushState('','', ...

Executing a .NET program from a shared file location without the need for code signing

Encountering security errors with the .NET security model when running a .NET exe from a file share, but no issues when running from a local drive. Any potential solutions to bypass this without code signing? ...

Show or hide a component based on a mouse click in Vue JS

After a prolonged absence from working with Vue JS, I find myself in the process of displaying a list of data with a button for each item. The goal is to conditionally render a component when a button is clicked. I am wondering if there is a recommended a ...

JavaScript issue: event.target.innerText not preserving line breaks

Within a conteneditable div, I am displaying pre-populated grey text with the data-suggestion attribute. Upon clicking the text: The text color changes to black The data-suggestion attribute is removed from the selected line An input event on the conten ...

Step-by-step guide on setting up individual FTP accounts for new registered users

Upon a user signing up on my website, an FTP account will automatically be generated for them. This will allow every user to access their personal folder using FileZilla. ...

Express is having trouble providing data to React

Currently, I am delving into mastering the realms of React and Express. My ongoing project involves crafting a learning application that fetches data from MySQL and renders it visually for analysis. To kickstart this endeavor, I set up a basic express ser ...

Calculating the screen-space coordinates for FBO value retrieval using a depth texture

EDIT: I have updated the JSFiddle link because it was not displaying correctly in Chrome on Windows 7. Situation I am experimenting with particles in THREE.JS and utilizing a frame buffer / render target (double buffered) to write positions to a texture. ...

Similar to jQuery's ajaxStart event handler, AngularJS provides an equivalent event handler for handling Ajax requests

Seeking a way to detect Ajax start and end events in an AngularJS application. In JQuery, one can utilize the ajaxStart, ajaxComplete functions to capture start, complete, end, and fail events on a global or local level. How can I globally monitor Ajax ev ...

Error message: ParseError: Received an unexpected character '<' while running tests using AVA

Initially encountering an issue within the project built on nuxt, I attempted to resolve it by creating a new project using vue-cli and re-installing AVA via npm. However, this did not have any effect on the problem at hand. Upon researching, I came across ...

Pros and cons of using multiple small files versus a single large file for HTTP requests

Curious about the best approach: Is it better to have multiple small files requested by multiple HTTP requests, or one big file requested all at once? Please outline the advantages and disadvantages of both methods as my online research has provided conf ...

Error found in join statement syntax

I encountered a challenging error while working on my program. The issue arises when attempting to extract data from a database and display it in a table. I've successfully extracted data from a single table, but the complication arises when I need to ...

Loading state with suggestions from autocomplete feature on React

In my current project, I have a component that consists of input fields and a button. The button is set to be disabled until the correct values are entered in the input fields. Everything works as expected, but there's an issue with Chrome auto-fillin ...

What is the best way to set up v-models for complex arrays or nested objects?

I'm looking to efficiently create multiple v-models for random properties within a deep array, where the position of attributes in arrays/objects can change dynamically. While I've managed to achieve my goal with the current setup, I'll need ...

MongoDB does not treat aggregate match pipeline as equal to in comparisons

I've been tackling an aggregate pipeline task for MongoDB where I need to retrieve items that do not have a specific user ID. Despite my efforts, I'm struggling to get it right. I attempted using $not, $ne, and $nin in various ways but couldn&ap ...

The requested URL /api/users/register does not exist. Error 404

While creating a money manager application utilizing the MERN Stack, I encountered an issue with posting data to the database. Whenever I click on the register button, an error is thrown stating that it Cannot POST /api/users/register. Despite setting up a ...

Background image not displaying in new tab after Chrome extension installation

I have been developing a Chrome extension that alters the background image of a new tab. However, I have encountered an issue where the background image doesn't change the first time the extension is loaded. This problem has also occurred very occasi ...

Re-activate video playback after 30 seconds of user inactivity (using RxJs)

My intention is to provide a brief explanation of the functionality I am looking for. Essentially, when a video is playing, I want it to pause if a user clicks on it, allowing them to do something else. Then, after 30 seconds of idle time, I need the video ...

Issue with NPM Scripts Post Hook Triggering

Currently, I am exploring the use of npm scripts to gradually phase out my dependency on Gulp. To begin, I have created a single custom script called watch. This script will eventually execute all other scripts prefixed with the name watch, such as watch:s ...

Integrating Highcharts Annotations with a donut chart

I'm struggling to find a way to position annotations for a donut chart outside of the donut itself. I've experimented with the distance, x, and y properties, but none have given me the desired outcome. Is there a method to properly position the a ...