开发者

Selectively show items in a dropdownlist

开发者 https://www.devze.com 2023-04-12 14:42 出处:网络
I have 2 drop down lists - ddlCompanies, and ddlContacts. Lets say - Companies | Company A | Company B | Company C |

I have 2 drop down lists - ddlCompanies, and ddlContacts.

Lets say -

Companies | Company A | Company B | Company C |
----------|-----------|-----------|-----------|
Contacts  | Albo      | Bill      | Charlie   |
          | Alex      | Bob       | Chad      |
          | Aaron     | Brad      | Dennis    |

in ddlCompanys i list - Company A, Company B and Company C in ddlContacts, I load all the contacts, Alex, bob, chad, ...

Now in the user control, i would like to display only those names (in ddlContacts0 that belong to the selected company (ddlCompany)

EDIT:

Code to load the dropdowns

ddlCompanies.DataSource = companies; //companies is instance of  IEnumerable<ListItem>
//Also the text of the list item is the name of the company, and value is the ID
ddlCompanies.DataT开发者_Python百科extField = "Text";
ddlCompanies.DataValueField = "Value";
ddlCompanies.DataBind();

Code for ddlcontacts

ddlContacts.DataSource = allContacts // all contacts is instance of IEnumerable<ListItem> and contains all the contacts for all companies.  
//The text Value Contains the name of the employee, and value is the ID
//If needed, I can look up the company for the contact in the code behind.
ddlContacts.DataTextField = "Text";
ddlContacts.DataValueField = "Value";
ddlContacts.DataBind();

Can some one help me with this-

Thanks,


I would use ASP.NET to write some JavaScript (with <%= %> tags).

var contacts;
contacts['A'] = 'Albo,Alex,Aaron';
contacts['B'] = 'Bill,Bob,Brad';
contacts['C'] = 'Charlie,Chad,Dennis';

This information is extracted from the database and written to the page like that, within <script> tags, by ASP.NET.

Now you can use jQuery to make it so whenever ddlCompanies changes, a function is called which uses the selected value of ddlCompanies to decide which element of the contacts array to use. It then splits this string and puts all the items into ddlContacts.

I won't try to write the exact code here, because I'd probably make mistakes. Comment if you can't make it work for you. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号