简体   繁体   中英

Javascript Dependent DropDownLists in an ASP.NET page

I have a .NET registration form that populates some DDLs from the database and has others hard coded. The requirement calls for a series of three dependent DDLs: One for Organization and two for Job Title. Depending on which Organization is chosen, some Job Titles may have sub-choices like so:

DDL1    DDL2    DDL3
A       a       n/a
B       a       c
C       b       n/a

In some cases the same choice in DDL2 will require a choice from DDL3; in some cases the choice from DDL2 will be sufficient. For example, If the choice from DDL1 was Hospital, DDL 2 would contain Physician, and DDL3 would contain Pediatrician and I would need to submit the values from DDL1 and DDL3. But, if DDL1 was Lab, the Physician choice from DDL2 would suffice and I would submit DDL1 and DDL2. I have a javascript file that handles all the choices perfectly but I'm having trouble getting the values sent to the DB because of postback issues.

I've tried several fixes including UpdatePanels and Triggers but I can't quite get it right.

I found this and it looks like it fits my needs nearly perfectly. Still tweaking to get it to work.

http://code.msdn.microsoft.com/CSASPNETCascadingDropDownLi-0a3f1ecf#content

I've got a simple XML example created:

<Countries>

 <Country name="China">
   <Region name="East China">
    <City name="ShangHai" guid="12345"></City>
    <City name="SuZhou" guid="23456"></City>
    <City name="NianJing" guid="34567"></City>
    <City name="QingDao" guid="45678"></City>
    <City name="ZheJiang" guid="56789"></City>
   </Region>
 </Country>

</Countries>

Here's the C# to get the City name to populate the DDL:

        /// <summary>
    ///  Get city based on region
    /// </summary>
    /// <param name="strRegion">The region name</param>
    /// <returns>The list contains city</returns>
    public static List<string> GetCityByRegion(string strRegion)
    {
        XmlDocument document = new XmlDocument();
        document.Load(strXmlPath);
        XmlNodeList nodeList = document.SelectNodes(
            "Countries/Country/Region[@name='" + strRegion + "']/City");
        List<string> list = new List<string>();
        foreach (XmlNode node in nodeList)
        {
            list.Add(node.Attributes["name"].Value);
        }
        return list;
    }

How do I get the guid value into the .NET form since that's the value I need to submit to the DB?

You can use a loop.

if(DDL1.SelectedValue.Equals("") || (DDL2.SelectedValue.Equals(""))
{
//your message
}
else if (DDL1.SelectedValue.Equals("") || (DDL3.SelectedValue.Equals(""))
{
//your message
}

... and so on.

Just add some codes in it that will satisfy your need. You can also use "&&" if you need to have a condition where both are to be satisfied.

Hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM