【问题标题】:XML Dropdown with Names and Values带有名称和值的 XML 下拉列表
【发布时间】:2014-03-17 10:11:05
【问题描述】:

我有三个相关的级联下拉列表。它们是从 XML 文件填充的,该文件适用于列表项,但我需要列表项中包含的值,以便可以将它们传递给数据库。

这是我重组的 XML 文档中的一个节点:

    <Organizations>
  <Organization>
    <name value="A0EAD6A8813E474B9F9A3E0C64D72F61">Hospital</name>
    <JobTitle1>
      <name value="-2">Physician</name>
      <JobTitle2>
        <name value="E5B838C3AA304813B3EA4E28EE33702F">Anesthesiologist</name>
       </JobTitle2>
      <JobTitle2>
        <name value="36C9F22DC54347CEA39C142FE91E8B36">A and E</name>
       </JobTitle2>
      <JobTitle2>
        <name value="35E798C18DC04ECFB022DCF0E6DBEB51">Cardiologist</name>
       </JobTitle2>
      <JobTitle2>
        <name value="343B2555E45142E096546A3781680C02">Dental Surgeon</name>
       </JobTitle2>
      <JobTitle2>
        <name value="A5C669F6DB4D41DA8409960D6952023C">Diabetologist/Endocrinologist</name>
       </JobTitle2>
      <JobTitle2>
        <name value="2B01B4C29253459797CB596FE81C3AF4">ENT</name>
       </JobTitle2>
      <JobTitle2>
        <name value="6C761F3F81CB421BBD0746914031EF19">General Medicine</name>
       </JobTitle2>
      <JobTitle2>
        <name value="9DB49A58B8484E73AA24227DCDAD4C96">Intensivist</name>
       </JobTitle2>
      <JobTitle2>
        <name value="F80D023B174A421A82A140691BCD9E22">Neurologist</name>
       </JobTitle2>
      <JobTitle2>
        <name value="3F95529E15764EB1A55255DCEDDB6C4A">Pediatrician</name>
       </JobTitle2>
      <JobTitle2>
        <name value="9FC573D63A884C04A8675E86B43EABB9">Psychiatrist</name>
       </JobTitle2>
      <JobTitle2>
        <name value="7230D95FE4844A7785D25A73D5240BB9">Respiratory</name>
       </JobTitle2>
      <JobTitle2>
        <name value="E784A9F594BF468D85C51BBD5A9B0F5D">Trainee</name>
       </JobTitle2>
      <JobTitle2>
        <name value="431614E6148A4F639F912ED08803BD79">Other Physician</name>
       </JobTitle2>
    </JobTitle1>
    <JobTitle1>
      <name value="111AEF6C39984713AA10E6F1D051F97E">Nurse</name>
    </JobTitle1>
    <JobTitle1>
      <name value="90CF5DB3D06A4B759F21A3F20B5F5DD4">Physiologist</name>
    </JobTitle1>
    <JobTitle1>
      <name value="55F8C6537718420DB6E4E3A7C087D540">Physiotherapist</name>
     </JobTitle1>
    <JobTitle1>
      <name value="884EB090E9B7469DA3CA8BA2D6FC4F4E">Biomedical Engineer/Technician</name>
     </JobTitle1>
    <JobTitle1>
      <name value="C2383AA850534CD9A4E03019386AA56C">Administrator</name>
     </JobTitle1>
    <JobTitle1>
      <name value="C8997E78569A4410956E5E7C6C1D57F2">Manager</name>
     </JobTitle1>
    <JobTitle1>
      <name value="5196E883DF614A319A3CDF4573C3E858">Supplies/Procurement</name>
     </JobTitle1>
  </Organization>

其他节点类似,这里不用浪费篇幅贴出来了。 DDL 中的所有文本都可以正常工作。

这是获取 DDL 值的 aspx.cs 代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

namespace CSASPNETCascadingDropDownList
{
    public partial class CascadingDropDownListWithPostBack : System.Web.UI.Page
    {
        /// <summary>
        /// Page Load event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Bind Org dropdownlist
                BindddlOrg();
                ddlJobTitle1.Enabled = false;
                ddlJobTitle2.Enabled = false;

                // Insert one item to dropdownlist top
                ddlJobTitle1.Items.Insert(0, new ListItem("Select JobTitle1", "-1"));
                ddlJobTitle2.Items.Insert(0, new ListItem("Select JobTitle2", "-1"));

                // Initialize JobTitle2 dropdownlist selected index
                hdfDdlJobTitle2SelectedIndex.Value = "0";
            }
        }

        /// <summary>
        /// Bind Org dropdownlist
        /// </summary>
        /// 
            public class XmlValue {
            public int value {get; set;} //getter and setter are important
            public string name {get; set;}
        }
        public void BindddlOrg()
        {
            List<XmlValue> list = RetrieveDataFromXml.GetAllOrg();
            ddlOrg.DataSource = list;
            ddlOrg.DataBind();
            ddlOrg.Items.Insert(0, new ListItem("Select Organization", "-1"));
        }

        /// <summary>
        /// Show selected value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            // Get the selected index of JobTitle2 dropdownlist
            int iJobTitle2Selected = Convert.ToInt16(hdfDdlJobTitle2SelectedIndex.Value);

            // The result will be showing
            string strResult = string.Empty;
            if (ddlOrg.SelectedIndex == 0)
            {
                strResult = "Please select a Org.";
            }
            else if (ddlJobTitle1.SelectedIndex == 0 && strResult == string.Empty)
            {
                strResult = "Please select a JobTitle1";
            }
            else if (hdfDdlJobTitle2SelectedIndex.Value == "0" && strResult == string.Empty)
            {
                strResult = "Please select a JobTitle2.";
            }
            else
            {
                strResult = "You selected Org: " + ddlOrg.SelectedValue
                    + " ; JobTitle1: " + ddlJobTitle1.SelectedValue
                    + " ; JobTitle2: " + ddlJobTitle2.Items[iJobTitle2Selected].Value;
            }

            LabelResult.Text = strResult;
        }


        /// <summary>
        /// Org dropdownlist SelectedIndexChanged event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlOrg_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Remove JobTitle1 dropdownlist items
            ddlJobTitle1.Items.Clear();
            string strOrg = string.Empty;
            strOrg = ddlOrg.SelectedValue;
            List<string> list = null;

            // Bind JobTitle1 dropdownlist based on Org value
            if (ddlOrg.SelectedIndex != 0)
            {
                list = RetrieveDataFromXml.GetJobTitle1ByOrg(strOrg);
                if (list != null && list.Count != 0)
                {
                    ddlJobTitle1.Enabled = true;
                }

                ddlJobTitle1.DataSource = list;
                ddlJobTitle1.DataBind();
            }
            else
            {
                ddlJobTitle1.Enabled = false;
            }

            ddlJobTitle1.Items.Insert(0, new ListItem("Select JobTitle12", "-1"));

            // Clear JobTitle2 dropdownlist
            ddlJobTitle2.Enabled = false;
            ddlJobTitle2.Items.Clear();
            ddlJobTitle2.Items.Insert(0, new ListItem("Select JobTitle22", "-1"));

            // Initialize JobTitle2 dropdownlist selected index
            hdfDdlJobTitle2SelectedIndex.Value = "0";
        }


        /// <summary>
        /// JobTitle1 dropdownlist SelectedIndexChanged event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlJobTitle1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Bind JobTitle2 dropdownlist based on JobTitle1 value
            string strJobTitle1 = string.Empty;
            strJobTitle1 = ddlJobTitle1.SelectedValue;
            List<string> list = null;
            list = RetrieveDataFromXml.GetJobTitle2ByJobTitle1(strJobTitle1);
            ddlJobTitle2.Items.Clear();
            ddlJobTitle2.DataSource = list;
            ddlJobTitle2.DataBind();
            ddlJobTitle2.Items.Insert(0, new ListItem("Select JobTitle2", "-1"));

            // Initialize JobTitle2 dropdownlist selected index
            hdfDdlJobTitle2SelectedIndex.Value = "0";

            // Enable JobTitle2 dropdownlist when it has items
            if (list.Count > 0)
            {
                ddlJobTitle2.Enabled = true;
            }
            else
            {
                ddlJobTitle2.Enabled = false;
            }
        }
    }
    }

这是从 XML 中获取值的 cs 文件代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace CSASPNETCascadingDropDownList
{
    /// <summary>
    /// Summary description for CSRetrieveDataFromXml
    /// </summary>
    public class RetrieveDataFromXml
    {
        /// <summary>
        /// Constructor 
        /// </summary>
        public RetrieveDataFromXml()
        {
            //
            // TODO: Add constructor logic here
            //
        }


        /// <summary>
        /// Get the data source path, in this case, it is the XML file in the 
        /// App_Data folder
        /// </summary>
        private static string strXmlPath = HttpContext.Current.Server.MapPath(
            "App_Data") + "\\CustomDataSourceDeep.xml";

        /// <summary>
        /// Get all orgs
        /// </summary>
        /// <returns>The List contains organization</returns>


/* this block works */
 /*       public static List<string> GetAllOrg()
        {
            XmlDocument document = new XmlDocument();
            document.Load(strXmlPath);
            XmlNodeList nodeList = document.SelectNodes("Organizations/Organization");
            List<string> list = new List<string>();
            foreach (XmlNode node in nodeList)
            {
               list.Add(node.Attributes["name"].InnerText);
            }
            return list;
        }*/
/* end of working block */
/*
         public static List<string> GetAllOrg()
       {
           XmlDocument document = new XmlDocument();
           document.Load(strXmlPath);
           XmlNodeList nodeList = document.SelectNodes("Organizations/Organization");
           List<string> list = new List<string>();
           foreach (XmlNode node in nodeList)
           {
               list.Add(node.SelectSingleNode("name").InnerText, node.Attributes["value"].Value);
           }
           return list;
       }
*/
     public class XmlValue {
     public string value {get; set;} //getter and setter are important
     public string name {get; set;}
}
          public static List<XmlValue> GetAllOrg()
    {
        XmlDocument document = new XmlDocument();
        document.Load(strXmlPath);
        XmlNodeList nodeList = document.SelectNodes("Organizations/Organization");
        List<XmlValue > list = new List<XmlValue >();
        foreach (XmlNode node in nodeList)
        {
            list.Add(new XmlValue(){
            value = node.Attributes["name"].Value,
            name = node.InnerText
                 });
         }
    return list;
}    

        /// <summary>
        /// Get JobTitle1 based on Org
        /// </summary>
        /// <param name="strOrg">The Org name</param>
        /// <returns>The list contains JobTitle1</returns>
        public static List<string> GetJobTitle1ByOrg(string strOrg)
        {
            XmlDocument document = new XmlDocument();
            document.Load(strXmlPath);
            XmlNodeList nodeList = document.SelectNodes(
                "Organizations/Organization[@name='" + strOrg + "']/JobTitle1");
            List<string> list = new List<string>();
            foreach (XmlNode node in nodeList)
            {
                list.Add(node.Attributes["name"].Value);
            }

            return list;
        }


        /// <summary>
        ///  Get JobTitle2 based on JobTitle1
        /// </summary>
        /// <param name="strJobTitle1">The JobTitle1 name</param>
        /// <returns>The list contains JobTitle2</returns>
        public static List<string> GetJobTitle2ByJobTitle1(string strJobTitle1)
        {
            XmlDocument document = new XmlDocument();
            document.Load(strXmlPath);
            XmlNodeList nodeList = document.SelectNodes(
                "Organizations/Organization/JobTitle1[@name='" + strJobTitle1 + "']/JobTitle2");
            List<string> list = new List<string>();
            foreach (XmlNode node in nodeList)
            {
                list.Add(node.Attributes["name"].Value);
            }
            return list;
        }
    }
}

这是为第一个 DDL 生成的源代码:

<option value="-1">Select Org</option>
<option selected="selected" value="Hospital">Hospital</option>
<option value="Sleep Lab">Sleep Lab</option>
<option value="Ventilation Home Service">Ventilation Home Service</option>
<option value="Community Health Centre">Community Health Centre</option>
<option value="Home Care Provider">Home Care Provider</option>
<option value="Med">Med</option>
<option value="Other">Other</option>

我需要的是这个:

<option value="-1">Select Org</option>
<option selected="selected" value="A0EAD6A8813E474B9F9A3E0C64D72F61">Hospital</option>
<option value="DD8BE886FA18401E9CF30AC64B49ACD8">Sleep Lab</option>
<option value="F8182C928BE14239A576895D39591D38e">Ventilation Home Service</option>
<option value="E3865431ECF9499692E79DBF2FBAA3B2">Community Health Centre</option>
<option value="711AA0D43D6346FC9AD38659D9CB8840">Home Care Provider</option>
<option value="D54FAE898ECF4AB4A92B99C5AA536910">ResMed</option>
<option value="4E44CDB4F7E5426E99368194BBB73282">Other</option>

如何让 DDL 继续正确填充,但仍然给我需要传递给数据库的值?

已编辑:

我现在收到此错误:

错误 1 ​​无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”

【问题讨论】:

    标签: c# javascript sql-server xml drop-down-menu


    【解决方案1】:

    你可以有一个对象(不确定地图可以用作ItemSource)

        private class XmlValue {
         public int value {get; set:} //getter and setter are important
         public string name {get; set:}
    }
    

    解析您的 XML:

      public static List<XmlValue> GetAllOrg()
        {
            XmlDocument document = new XmlDocument();
            document.Load(strXmlPath);
            XmlNodeList nodeList = document.SelectNodes("Organizations/Organization");
            List<XmlValue > list = new List<XmlValue >();
            foreach (XmlNode node in nodeList)
            {
                list.Add(new XmnValue(){
                value = node.Attributes["name"].Value,
                name = node.Attributes["name"].Name
                     });
             }
        return list;
    }    
    

    您的 DDL:

    public void BindddlOrg()
        {
            List<XmlValue > list = RetrieveDataFromXml.GetAllOrg();
    
            ddlOrg.ItemsSource = list;
    
            ddlOrg.Items.Insert(0, new XmlValue (){nqme = "Select Org", value=-1});
        }
    

    最后,要绑定其他两个 ddl,请使用 ItemSelected。

    希望对你有帮助,

    【讨论】:

    • 现在我明白了:错误 1 ​​无法将类型 'System.Collections.Generic.List' 隐式转换为 'System.Collections.Generic.List'跨度>
    • 试试:list.Select(x => x.name).toList()
    • 我有三个文件:XML 文件、用于检索 XML 的retrieve.cs 文件和 aspx.cs 代码隐藏文件。错误发生在 RetrieveDataFromXml.GeAllOrg() 行后面的代码中。我在哪里放 list.Select(x => x.name).toList()?
    • 您可以更新您的问题中的代码吗?为什么你还有一个 List
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多