LoadPostData NameValueCollection 不包括回传上的所有控件

LoadPostData NameValueCollection does not include all controls on postback

我创建了一个 asp.net 自定义用户控件

public class InputMask : CompositeControl, IPostBackDataHandler, IPostBackEventHandler

例如,如果自定义控件位于 div

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

在后面的代码中我有这个:

 inputArea.Visible = False

在回发时 LoadPostData(string postDataKey, NameValueCollection postCollection)postCollection 没有 MaskInput 键。

如果我这样做:

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

postCollection 包含 MaskInput 键。

我理解当使用 css 隐藏 div 时,控件呈现但不显示。然而,当使用 visible=false 时,控件根本不会呈现。

那么,在使用 visible=false 时,是否有解决方法来获取 LoadPostData 中的密钥?

解决此问题:

我更改了 LoadPostData 方法:

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

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;
    }

还更改了文本 属性 以使用 ViewState

发件人:

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

    [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;
        }
    }