当Enabled=false时,DropDownList的SelectedValue不是我选择的值
我在网络表单上有几个 DropDownList,每个都指向一个 SqlDataSource。其中一些数据源使用这些 DropDownList 之一作为选择参数,将列表过滤到特定类别。
对于某些用户,将自动设置和禁用其中两个 DropDownList。
当我提交启用了下拉列表的表单时,它工作正常,但是当它们被禁用时,DropDownList 的 SelectedValue 将被重置为列表中的第一个值。
我的 DropDownList 的构造如下:
<asp:DropDownList ID="ddlManager" runat="server" DataSourceID="dsManagers"
EnableViewState="false" DataValueField="ManagerID" DataTextField="MgrName"
AppendDataBoundItems="false" ondatabound="ddlManager_DataBound" >
下拉列表在数据绑定事件上添加一个额外的项目,如下所示:
protected void ddlManager_DataBound(object sender, EventArgs e)
{
this.ddlManager.Items.Insert(0, new ListItem("--Manager--", "--Manager--"));
}
任何人都可以解释为什么会发生这种情况吗?
谢谢
I have a few DropDownLists on a webform each pointing to a SqlDataSource. Some of these datasources use one of these DropDownLists for a select parameter, filtering the list to specific categories.
For certain users two of these DropDownLists are to be automatically set and disabled.
When I submit the form with the drop downs enabled it works fine but when they are disabled the SelectedValue of the DropDownList is being reset to the first one in the list.
My DropDownLists are constructed as follows:
<asp:DropDownList ID="ddlManager" runat="server" DataSourceID="dsManagers"
EnableViewState="false" DataValueField="ManagerID" DataTextField="MgrName"
AppendDataBoundItems="false" ondatabound="ddlManager_DataBound" >
The drop down lists add an extra item on the data bound event as follows:
protected void ddlManager_DataBound(object sender, EventArgs e)
{
this.ddlManager.Items.Insert(0, new ListItem("--Manager--", "--Manager--"));
}
Can anyone shed any light as to why this is happening?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是设计使然。禁用控件的值不会发布。使用其他一些机制。
This is by design. Values for disabled controls are not posted. Use some other mechanism.
或者,您可以读取/写入
List
或Dictionary
;并将列表分配给您的DropDownList每当启用 DropDownList 框时,都会生成
数据源。Alternatively, you can read/write to a
List<>
orDictionary<>
; and assign the list to yourDropDownList
datasource whenever the DropDownList box gets enabled.我认为这很简单,就是没有启用的问题。如果未启用,则无法保存值,并恢复为默认值。
I think it's quite simply a matter of not being enabled. If it's not enabled it can't hold a value, and reverts to the default.
要添加 KMån 所说的内容,您还可以使用视图状态而不是列表。
to add to what KMån said, you could also use viewstates instead of a list.