LoadPostData NameValueCollection不包括回发的所有控​​件

时间:2016-06-14 15:02:52

标签: c# css asp.net custom-controls

我创建了一个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中获取密钥?

1 个答案:

答案 0 :(得分:0)

要解决此问题:

我改变了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;
    }

还更改了Text属性以使用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;
        }
    }