Having difficulties retrieving data from the Value attribute of HtmlInputHidden

I've developed a custom control that extends .NET's CompositeControl class. This control overrides the CreateChildControls method to dynamically create its child controls. In order for the page to post back after specific javascript events on the client side, I have implemented a solution involving two hidden controls.

To achieve this functionality, I have included the following code snippet to create the hidden controls:

Protected Overrides Sub CreateChildControls()
    hdEventName = New HiddenField()
    Controls.Add(hdEventName)
    hdEventName.ID = "hdEventName"
    hdEventArgs = New HiddenField()
    Controls.Add(hdEventArgs)
    hdEventArgs.ID = "hdEventValue"
    ' other controls
    ' ...
End Sub

Whenever a javascript event is triggered, I set the values of these hidden controls and submit the page using the following script:

hdEventName.value = 'EventName';
hdEventArgs.value = 'arg1,arg2';
document.forms[0].submit();

In the OnLoad method of my control, I encountered an issue where the Value property of the hidden controls (hdEventName and hdEventArgs) appeared empty, even though Page.Request.Form(hdEventName.UniqueID) and Page.Request.Form(hdEventArgs.UniqueID) returned the expected values. The HTML markup also displayed the correct values post-back.

This discrepancy between the Value property of the HtmlInputHidden controls and the actual client-side value prompted me to investigate further. By moving the code that retrieves the hidden fields' values to either the OnPreRender method or implementing a dedicated Event_Handler method as below, I was able to resolve the issue:

Private Sub Event_Handler(ByVal sender As Object, ByVal e As EventArgs)
                                          Handles hdEventName.ValueChanged
    ' do stuff with hiddens
    ' ...

    ' reset the values back
    hdEventName.Value = String.Empty
    hdEventArgs.Value = String.Empty
End Sub

Answer №1

After reloading the page, the variable hdEventName loses its connection to the control that was previously established. It's similar to declaring an integer at the class level and assigning it a value of 5 when creating child controls. This approach doesn't retain the value in the variable between postbacks.

If you want to retrieve a reference to the previously created control, you will need to use

hdEventName = CType(Page.FindControl("hdEventName"), HiddenField)

(this is just a guess) or utilize Request if only the value is important.

Answer №2

After some investigation, I've discovered that a control's properties are actually loaded from the form post OnLoad event. To resolve my issue, I ended up either relocating the code responsible for checking the two hidden fields to the OnPreRender method or incorporating the following method into my code -

Private Sub HandleEvent(ByVal sender As Object, ByVal e As EventArgs)
                                      Handles hdEventName.ValueChanged
    ' perform actions with hidden fields
    ' ...

    ' reset values
    hdEventName.Value = String.Empty
    hdEventArgs.Value = String.Empty
End Sub

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

Trigger JS event as soon as new elements are inserted

I need to trigger an event every time elements are added. I am using JQuery and I know there are built-in functions to add events to all matching elements, including those that will be added later. The code for this usually looks like: jQuery("body").on(" ...

Executing an object method within a function in Javascript: A step-by-step guide

My task is to create an object (instance of a sub-class) within a function, call a method on that object, and then return it using the following classes: // Implement a Person class class Person { constructor(name = "Tom", age = 20, energy = ...

javascript/jquery concatenate all hidden input values into a variable

I am working with a collection of tags that are each added to a hidden input labeled "item[tags][]". <input type="hidden" style="display:none;" value="first" name="item[tags][]"> <input type="hidden" style="display:none;" value="second" name="ite ...

Utilize WinForm or application to print on the client side

I have a dilemma with my ASP.NET program. I am seeking the functionality to allow clients to select their own printer and print directly from the application. While I understand that this capability is challenging, potentially requiring ActiveX which limi ...

Hide the div only if a specific element is found on the page

I am currently exploring different methods to show or hide a left-hand navigation menu on my Volusion store. Is it possible to use JavaScript or jQuery to alter the CSS display property of a div based on the presence of another element on the page? The i ...

Develop a program that can operate offline and automatically synchronize data once an internet connection is established

I am in the process of developing a POS system using C# MVC with SQL server database. My goal is to ensure that the system can operate without an internet connection and automatically synchronize when reconnected online. While researching solutions online ...

Incorporate personalized buttons into the header navigation for each view using VueJS

In my VueJS project, I attempted to include custom buttons in the sub navigation menu which could be customized for each view. Initially, I tried adding a simple property to the main data element, but that didn't yield the desired results. Then, I cam ...

Height of the div dynamically increases upwards

Just a quick question - is there a way to make position: fixed divs at the bottom of an element (such as the body) grow upwards as content is dynamically added? Maybe something like anchor: bottom or increase: up? I'm thinking that using JavaScript m ...

Adjusting the timing of a scheduled meeting

Is there a way for me to update the time of a Subject within my service? I'm considering abstracting this function into a service: date: Date; setTime(hours: number, mins: number, secs: number): void { this.date.setHours(hours); this.date.s ...

Removing items from an array

I'm currently developing an Angular application and I have an array named "*identify duplicates" that looks like the following: [ { Name: "Jack", Id: "1" }, { Name: "Rose", Id: "2" }, { Name: "Jack", Id: "4" }, { ...

Applying the DataBinder.Eval method within an if statement

Here is the code snippet: <asp:Repeater ID="Repeater2" runat="server"> <HeaderTemplate> <table cellspacing="0" cellpadding="0" border="0" width="100%"> </HeaderTemplate> <ItemTemplate> <tr> <td cols ...

Incorporating D3.js into Angular 6 for interactive click events

Currently working on building a visual representation of a tree/hierarchy data structure using d3.js v4 within an Angular environment. I've taken inspiration from this particular implementation https://bl.ocks.org/d3noob/43a860bc0024792f8803bba8ca0d5e ...

Ways to organize a directory structure of folders

I am working with a tree structure of folders that have properties such as id, parent_id, and name. Currently, this tree is stored in an unsorted array. Each element in my array looks like this: var obj = { id: 1, parent_id: null, name: "Folder" } My go ...

Stop draggable element from exiting the boundaries of its container during drag operation

I have a draggable element with id mydiv that is contained within another element with id container. I want to restrict the movement of mydiv so that it stays inside the container without leaving its boundaries. How can I achieve this? Below is the code s ...

Substitute a particular element within a text

I'm currently working on an Angular 9 application where I'm implementing search filters for the Shopify Graphql API. As part of this process, I am constructing a query which looks like: first: 1, query: "tag:'featured-1'", af ...

Scope ng-repeat with Angular's $compile function is not functioning as expected

I am facing an issue with my directive that uses the $compile service to create a template. Despite having the users array defined in my scope, it does not populate the select options using either ng-options or ng-repeat. I am puzzled as to why this is h ...

In JavaScript, what causes the gap between divs in the regular flow of a webpage?

My goal was to align two divs next to each other within a container div, but for some reason the second div ended up below the first with a 4 pixel gap in between. Despite setting margins, padding, and borders to 0px, this gap persisted. Here's the co ...

Using an image for the axis in Wijmo BarGraph

I am currently working with Wijmo barcharts and attempting to create a graph that uses images instead of labels on the x-axis. The code I have at the moment displays the image source as a string rather than displaying the actual image. Does anyone know ho ...

I am encountering problems with the reverse proxy as it seems that asp.net is not recognizing that security should not be applied to the login

My asp.net application utilizes forms authentication, which denies access to anonymous users. Everything works perfectly when I access the server directly, but when I try to access it through a reverse proxy, things don't work as expected. When acces ...

Having difficulty in replicating and obtaining flash video content from a website through HTML coding

I'm having trouble downloading a flash video, as traditional download software isn't working. I attempted to directly access the video from the html script itself, following this tutorial : https://www.youtube.com/watch?v=waE3J0Jej_0 The script ...