当Enabled=false时,DropDownList的SelectedValue不是我选择的值

发布于 2024-08-28 02:10:40 字数 756 浏览 8 评论 0原文

我在网络表单上有几个 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 技术交流群。

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

发布评论

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

评论(4

哆啦不做梦 2024-09-04 02:10:40

这是设计使然。禁用控件的值不会发布。使用其他一些机制。

This is by design. Values for disabled controls are not posted. Use some other mechanism.

浮光之海 2024-09-04 02:10:40

Alternatively, you can read/write to a List<> or Dictionary<>; and assign the list to your DropDownList datasource whenever the DropDownList box gets enabled.

皇甫轩 2024-09-04 02:10:40

我认为这很简单,就是没有启用的问题。如果未启用,则无法保存值,并恢复为默认值。

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.

枕梦 2024-09-04 02:10:40

要添加 KMån 所说的内容,您还可以使用视图状态而不是列表。

to add to what KMån said, you could also use viewstates instead of a list.

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