SelectedValue of DropDownList is not the one I selected when Enabled=false
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" Dat开发者_如何学GoaTextField="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
This is by design. Values for disabled controls are not posted. Use some other mechanism.
Alternatively, you can read/write to a List<>
or Dictionary<>
; and assign the list to your DropDownList
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.
to add to what KMån said, you could also use viewstates instead of a list.
精彩评论