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

Make the text in the SCSS file placeholder bold

Within my Angular front end application, there is a form containing a textarea: <mat-form-field class="full-width"> <textarea class="left-aligned" formcontrolname="x1" matInput placeholder="some text"/> </mat-form-field> In the co ...

Utilizing Inline Placement within Email Templates

Seeking assistance in finding an alternative method to achieve the desired functionality in an email template. Having trouble with inline position styling being removed. table { width: 80%; border-spacing: 0; } table tr td.label-dots { positio ...

Preventing background style from taking precedence over backgroundColor style in React's inline styling

What causes background to take precedence over backgroundColor in React inline-style? In this scenario, the lack of a specified green background-color results in the background gradient with transparency transitioning into white rather than green within t ...

Tips for downsizing a large image to fit into a smaller area

I am working on a page layout that features a small circular navigation element. However, I am facing an issue with fitting a large picture within the boundaries of this small circle without it overflowing and causing alignment problems. Does anyone have ...

Avoid auto-scaling of div content

I need the .content within the main container .box to resize proportionally to match the width of the .box exactly, without being smaller. .box { width: 95%; background-color: red; padding: 20px; text-align: center; margin: 55px auto 40px; } . ...

What's the name of the auto-triggered dropdown menu feature?

Visit Amazon's official website Description: Within the 'Shop by Department' section- A drop-down menu that remains visible without requiring you to hover over it. However, when the browser is in a non-full-screen mode, the menu disappears ...

Add a new division to a component when a specific event handler is triggered by another component using React and the reduce method

Currently, I am developing an interactive drag-and-drop application using React and Redux. My goal is to insert a new div element into a container when the ondragstart event handler is triggered by a component. The component responsible for the dragging o ...

Steps to animate a div expanding to fit its content dimensions

I'm looking for a way to animate the opening of a div so that it adjusts to the size of its content. The content is fetched from a separate search using .load, which means it could be just a single line (no result) or multiple results that vary in hei ...

CSS Table columns are set to have a fixed width for the first and last columns, while the middle two columns have dynamic widths

I was able to create a table layout like this: fixed - dynamic(50%) - dynamic(50%) - fixed http://jsfiddle.net/ihtus/ksucU/ Now, I'm wondering how to achieve this layout instead: fixed - dynamic(30%) - dynamic(70%) - fixed This is the CSS code I c ...

ASP repeater exceeding container boundaries

Having trouble with my asp repeater that reads from a database and generates repeatable divs. The issue arises when the h4 spills over the div, particularly when using <%Eval. Any suggestions on how to fix this? Manual input divs don’t seem affected o ...

Shifting Icon to the Right within the Drawer Navigator Toolbar

While modifying the example code for Material UI's drawer navigator, I decided to enhance it by adding a notification icon and a checkout icon with the Admin Panel typography in the toolbar. However, I encountered an issue where the checkout icon app ...

Why are the radio buttons not aligned with the text in the center?

Currently, I am in the process of building a form that includes radio buttons. However, I've encountered an issue where the text next to the radio buttons appears on a different level than the buttons themselves. How can I go about fixing this partic ...

The iPage server has encountered a 403 Forbidden Error, preventing

My website is linked with WordPress, but I'm encountering a 403 forbidden error in the sub-menu. Can someone assist me with this issue? I have uploaded a screenshot of the folder in iPage here Additionally, I want my WordPress page to show up correc ...

Here's a new take on the topic: "Implementing image change functionality for a specific div in Angular 8 using data from a loop"

I need to create a list with dynamic data using a loop. When I click on any item in the list, I want the image associated with that item to change to a second image (dummyimage.com/300.png/09f/fff) to indicate it's active. This change should persist e ...

What methods can be used to carry out personalized deserialization for specific fields?

I'm working on extracting data from my Firefox bookmarks for further manipulation. To my surprise, the process went smoothly with the following code: class Bookmark { public string title; public int? id; public int? parent; public str ...

Toggle jQuery to hide a div and update its CSS styling

There is an anchor with the class of "hide-btn1" that I want to trigger a series of actions when clicked: The rcol-content should hide and the text should change from Hide to Show The #container width needs to increase to 2038px The table.status-table wi ...

Tips on changing textbox values in Page Object Model for both incrementing or decrementing

I need to enter a numeric value into this textbox, but I am having trouble using xpath because the id keeps changing. Below are the image and source code related to this issue. https://i.stack.imgur.com/yPKsg.jpg Below is the source code where I was able ...

Verify whether a div element is styled in a specific manner

Upon initial load of my website, if the page is maximized or in fullscreen mode, the comBrand div will have specific CSS properties applied. However, during a resize event, I use the .css() function to adjust the properties of this element so it doesn&apos ...

Tips for avoiding the display of the overall scrollbar while utilizing grid with an overflow:

In my react-redux application, I am utilizing the material ui Grid component for layout design. The structure of my code is as follows: <div> <Grid container direction="row" spacing={1}> <Grid item xs={3}> ...

Best Practices for Managing Long Running Tasks in ASP.NET 4.0 with C#

How should I handle processes that require a long execution time? I want to avoid running them on the server because it may cause the page to time out. I have researched various methods such as AJAX, Threading, and Web Services, but I do not have experien ...