我有三个依赖的级联下拉列表。它们是从XML文件填充的,对于列表项可以正常工作,但是我需要列表项中包含的值,所以我可以将它们传递给DB。
以下是重构的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中的所有文本都正常工作。
这里是aspx.cs代码,用于获取DDL的值:
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'
答案 0 :(得分:0)
您可以拥有一个对象(不确定地图可以用作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。
希望它有所帮助,