复合控件中的 DropDownList 在回发时丢失所选值
我正在构建一个复合服务器控件,当前只有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为您在每次页面加载时添加 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.