Postback does not reload all controls in the NameValueCollection

Recently, I designed a custom User Control in asp.net.

public class InputMask : CompositeControl, IPostBackDataHandler, IPostBackEventHandler

Imagine the custom control is enclosed within a div element:

<div runat="server" id="inputArea">
    <asp:MaskInput runat="server" ID="ssn" ToolTip="SSN" Format="SSN" PartialMask="true" />
</div>

In the code behind, there is the following snippet:

 inputArea.Visible = False

During postback,

LoadPostData(string postDataKey, NameValueCollection postCollection)
does not contain the MaskInput key.

However, if you try this approach:

inputArea.Attributes.Add("style", "display:none")

The postCollection will have the MaskInput key present.

It seems that when using CSS to hide the div, the control is still rendered but invisible. On the other hand, with visible=false, the control is not even rendered at all.

Is there any workaround to retrieve the key in LoadPostData while using visible=false?

Answer №1

To address and fix the issue at hand:

I made adjustments to the LoadPostData method as follows:

public override bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
        this.Text = postCollection[ClientID + InputHiddenPostfix];
        return false;
    }

and updated it to:

public override bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
        var key = ClientID + InputHiddenPostfix;
        string text = this.Text;
        if (postCollection.AllKeys.Any(k => k.Equals(key))) {
            string item = postCollection[key];
            if (this.ReadOnly || text.Equals(item, StringComparison.Ordinal)) {
                return false;
            }
            this.Text = item;
        } else {
            return false;
        }           
        return true;
    }

In addition, I modified the Text property to utilize ViewState

From:

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    public string Text { get; set; }

to

    [Localizable(true)]
    [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
    [WebCategory("Appearance")]
    [WebSysDescription("MaskBase_Text")]
    [Bindable(true, BindingDirection.TwoWay)]
    [DefaultValue("")]
        public string Text {
        get {
            string item = (string)this.ViewState["Text"];
            if (item != null) {
                return item;
            }
            return string.Empty;
        }
        set {
            this.ViewState["Text"] = 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

When a Mui Button is clicked, it changes the color of all tabs instead of just the active

My website footer contains 5 links represented by Mui Buttons with the component set to {Link} in order to transform them into clickable links. Currently, all the buttons are black and change to green when hovered over. However, I want the button color to ...

What impact does the use of CSS in emails have on various email clients and reading formats?

As I delve into customizing my very first (woocommerce) email template, I am considering the option of concealing data using CSS display: none. However, a few questions arise regarding the compatibility and support of CSS and display: none: Do all email ...

Establish a direct connection using an instance in C#

Can you establish a direct connection to an instance without the need to specify a server, username, and password? Instead of connectionString = @"server=serverName" + "other things and password" It would look like: connectionString = [directly conne ...

Unusual occurrences observed with table cell padding in CSS

When I include a TD padding in my CSS like this: #maincontent table td, #maincontent table th { padding: .333em 0 .333em .25em; } In Safari and IE, the padding is applied correctly to the TD, but not in Firefox. If I apply the TD padding only to th ...

Comparing Embedded and Linked JS/CSS

In my experience, I understand the advantages of using linked CSS over embedded and inline styles for better maintainability and modularity. However, I have come across information suggesting that in certain mobile web development applications, it may be m ...

Encountering an error during the construction of a selenium solution with Specflow Specrun

As a beginner in the world of Selenium and SpecFlow SpecRun, I am currently utilizing a framework for automating tests. My setup involves using SpecRun for test runs and SpecFlow in C#. However, I have encountered an error while trying to build my soluti ...

Rows nested within the Bootstrap grid system

Looking to implement a nested grid within a parent grid in Bootstrap 3.3.7. Check out the HTML below: Parent component <div> <div class="row"> <div class="col-md-3 border">.col-md-3</div> // <div class="col-md-9 ...

In React, when utilizing the grid system, is there a way to easily align items to the center within a 5 by

I need to center align items within the material-UI grid, with the alignment depending on the number of items. For instance, if there are 5 items, 3 items should be aligned side by side in the center and the remaining 2 items should also be centered. Pleas ...

"Sending ctrl+z: easy steps to exit a program gracefully

Is there a way to change the key combination ctrl+z into a string format? I need to send this as an AT COMMAND to a device connected to my computer. All I want is to add some characters to a string and include ctrl+z in that same string. ...

Disabling the scrollbar in Selenium screenshots

When using Chromedriver to capture screenshots of webpages, my code effectively does the job. However, I am now facing an issue with removing the unsightly scrollbars from the image. Is it feasible to inject CSS into the webpage in order to achieve this? W ...

Verify the validation of the text box

Checking a textbox control for validation is necessary. Validation Criteria: Value should be between 0 and 1000, with up to 2 decimal places (e.g. 1.00, 85.23, 1000.00). Once 2 decimal points are used, users should not be able to enter additional ze ...

The hover effect in CSS brings life to list items when filled with content

I am attempting to create an animation for an <li> element that gives the illusion of a fill effect moving from left to right when hovered over. This is my current progress: ul { list-style-type: none; } .hoverTest { float: left; margin-right: 1 ...

The 600 pixel wide page is not completely filling the 640 pixel width screen on a mobile device

Currently, I am conducting a test on a webpage using an iPhone 5s with a screen resolution of 1136 x 640 pixels. The meta tag is set as follows: <meta name="viewport" content="user-scalable=yes, initial-scale=1, width=device-width, shrink-to-fit=no"&g ...

Using React Typescript to create a button component with an attached image

I am currently utilizing React with Typescript. How can I incorporate an image into my button? I've attempted to do so without any errors, but the button appears blank. What's the best way to style my button element using Emotion CSS in this ...

Using jQuery to animate a div when clicked by the mouse

Here's the scenario: I have 3 block elements arranged in a specific order: Image Hidden Text Block (.hidden) Footer Block (.blockimage) When the page loads, the image is displayed over the hidden text block (which contains further infor ...

Why does my calendar keep displaying dates from the previous month?

For some reason, when I try to select May 24th it keeps selecting April 24th. Oddly enough, if I select any date between May 1st and May 23rd it works fine. I am using a dropDown Calendar List<IWebElement> dateOfList = new List<IWebElement>(d ...

Previewing multiple images with multiple file inputs

I am trying to find a way to preview multiple images uploaded from different inputs. When the button is pressed, the content from a textarea containing <img src="$img" id="img1"> is written inside a div called seeimg. The IDs for these images range f ...

What is the best way to set the screen height without needing to scroll vertically?

How can I make the orange and blue area extend to the end of the screen or have the full screen size minus the height of the navbar? When using `vh-100`, it fills the full height but creates a vertical scrollbar which is not desired. Is there a way in Boot ...

Concealing Bootstrap 3 elements on a split-screen layout

I encountered a peculiar issue with Bootstrap 3. When I resize my browser width to half the screen, there is a slight 1px threshold where both "hidden-xs" and "hidden-sm" elements become visible simultaneously. Is there a way to fix this so that only one o ...