Reputation: 1115
I have 2 dropdown lists, both bound. The first dropdown list populates on load. The second drop-down list is dependant on the selection made in the first, so it is not populated until a selection is made in the first. I need to remove an item from the second list if it appears but when I put the code below into the onchange event for the first drop-down list, it doesn't work because the list hasn't had time to populate.
callerTypeDD.Items.Remove(callerTypeDD.Items.FindByText("Member"));
Any help appreciated.
Upvotes: 0
Views: 1760
Reputation: 1115
Thanks for the replies everyone. Actually found a really simple solution. If we databind the second dropdown list(callerTypeDD) inside the onChange event for the first downdown list(skillDD), and then search and remove the value we need to exclude, it will work...
protected void skillDD_SelectedIndexChanged(object sender, EventArgs e)
{
filterPanelCallerType.Visible = true;
skillDD.Enabled = false;
callerTypeDD.DataBind();
//remove Employer/Broker, Member option. The select statement has been set up to display these lines if either are selected.
callerTypeDD.Items.Remove(callerTypeDD.Items.FindByText("Employer/Broker, Member"));
}
Upvotes: 2
Reputation: 655
use an onload event, so for example
First dropdown
<asp:DropDownList runat="server" ID="ddl1" AutoPostBack="true" DataTextField="Field"
DataValueField="ID" OnSelectedIndexChanged="someFunction" AppendDataBoundItems="true" />
Second Dropdown
<asp:DropDownList runat="server" ID="callerTypeDD" AutoPostBack="true" DataTextField="Field"
DataValueField="ID" OnSelectedIndexChanged="someFunction" AppendDataBoundItems="true" onLoad="someOtherFunction"/>
Then in your .cs file create a method for that onload
protected void someOtherFunction(object sender, EventArgs e)
{
callerTypeDD.Items.Remove(callerTypeDD.Items.FindByText("Member"));
}
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.onload(v=vs.90).ASPX
Upvotes: 1