复合控件中的 DropDownList 在回发时丢失所选值

发布于 2024-08-22 08:33:37 字数 1229 浏览 4 评论 0原文

我正在构建一个复合服务器控件,当前只有一个 TextBox 和一个 DropDownList。下面是代码的所有胎儿的荣耀:

public Address : CompositeControl
{
    private string[] _states = new string[]
    {
        string.Empty, "ACT", "NSW", "VIC", "QLD", "SA", "WA", "NT", "TAS"
    };
    private TextBox _street;
    private DropDownList _state;

    public string Street
    {
        get { return _street.Text; }
        set { _street.Text = value; }
    }
    public string State
    {
        get { return _state.Text; }
        set { _state.Text = value; }
    }

    public Address() : base()
    {
        EnsureChildControls();
    }

    protected override void CreateChildControls()
    {
        _street = new TextBox();
        _state = new DropDownList();

        _state.Items.AddRange(
            Array.ConvertAll<string, ListItem>(_states, ListItem.FromString));

        Controls.Add(_street);
        Controls.Add(_state);
    }
}

控件似乎可以在回发中正常工作,直到设置声明值为止,例如:

<squee:Address runat="server" ID="a" Street="123 Fake St" State="VIC" />

在此之后,文本框继续正常工作,但 _state 成员不会拾取已发布的内容返回值,而只是坚持声明的值。我已经检查了 Request 对象中的原始发布值,列表的 UniqueID 的新值就在那里,但 _state 没有拾取它。

我很确定这将是显而易见的事情,但我只是在这里闲聊。

I am building a composite server control that currently only has a TextBox and a DropDownList. Here is the code in all its foetal glory:

public Address : CompositeControl
{
    private string[] _states = new string[]
    {
        string.Empty, "ACT", "NSW", "VIC", "QLD", "SA", "WA", "NT", "TAS"
    };
    private TextBox _street;
    private DropDownList _state;

    public string Street
    {
        get { return _street.Text; }
        set { _street.Text = value; }
    }
    public string State
    {
        get { return _state.Text; }
        set { _state.Text = value; }
    }

    public Address() : base()
    {
        EnsureChildControls();
    }

    protected override void CreateChildControls()
    {
        _street = new TextBox();
        _state = new DropDownList();

        _state.Items.AddRange(
            Array.ConvertAll<string, ListItem>(_states, ListItem.FromString));

        Controls.Add(_street);
        Controls.Add(_state);
    }
}

The control appears to work correctly across postbacks, that is until declarative values are set, e.g.:

<squee:Address runat="server" ID="a" Street="123 Fake St" State="VIC" />

After this, the text box continues to work correctly, but the _state member does not pick up the posted back values and instead just sticks to the declared value. I've checked the raw posted values in the Request object, and the new value for the list's UniqueID is there, but _state doesn't pick it up.

I'm pretty sure this is going to be something obvious, but I am just spinning my wheels here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾兮 2024-08-29 08:33:37

发生这种情况是因为您在每次页面加载时添加 ListItems。不要在 PostBack 上重新创建列表项,ViewState 将恢复它们。

This is happening because you are adding the ListItems on every page load. Don't recreate the list items on a PostBack, the ViewState will restore them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文