我试图让这项工作在很多方面起作用,我感到很沮丧,因为它们都不起作用。这是我得到的最接近的。我正在尝试从XML文件中填充三个级联下拉列表。我尝试过不同的XML配置,但我仍然不确定我使用的是否正确。如果从第二个下拉列表中进行特定选择,则仅填充第三个下拉列表。否则它应该是不活动的。
第一个下拉列表填充正常但第二个下拉列表在启用后为空白。
这是我的aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CascadingDropDownList.aspx.cs" Inherits="CSASPNETCascadingDropDownList.CascadingDropDownListWithPostBack" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>View CascadingDropDownList With PostBack</title>
<%-- The follow javascript is the work around of EventValidation in FireFox --%>
<script type="text/javascript">
var hdfDdl = null; // Save city dropdownlist selected option
var ddlJobTitle2 = null;
// Restore the selected option in city dropdownlist when page is rendering
window.onload = function () {
hdfDdl = document.getElementById('hdfDdlJobTitle2SelectedIndex');
ddlJobTitle2 = document.getElementById('ddlJobTitle2');
ddlJobTitle2.selectedIndex = hdfDdl.value;
EnableOrDisableButton(false);
}
// Save city dropdownlist selected option when selected option is changed in city dropdownlist
function onChange() {
hdfDdl.value = ddlJobTitle2.selectedIndex;
}
// Enable or diasble submit button
function EnableOrDisableButton(ToF) {
document.getElementById('Button1').disabled = ToF;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table class="style2">
<tr>
<td style="width:20px"></td>
<td style="width:440px" colspan="2"> Place of Employment</td>
<td> Professional Affiliation</td>
<td> Specialty</td>
</tr>
<tr>
<td style="width:20px"> </td>
<td colspan="2"><asp:DropDownList runat="server" id="ddlOrg" style="width:220px" AutoPostBack="true" OnSelectedIndexChanged="ddlOrg_SelectedIndexChanged">
</asp:DropDownList>
<br /><asp:Label ID="LabelOrg" runat="server"></asp:Label>
</td>
<td><asp:DropDownList runat="server" id="ddlJobTitle1" style="width:220px" AutoPostBack="true" OnSelectedIndexChanged="ddlJobTitle1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<asp:Label ID="LabelJobTitle1" runat="server"></asp:Label>
</td>
<td><asp:DropDownList runat="server" id="ddlJobTitle2" onChange="onChange()" style="width:220px">
</asp:DropDownList>
<br />
<asp:Label ID="LabelJobTitle2" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width:20px"></td>
<td style="width:220px"><asp:HiddenField runat="server" id="hdfDdlOrgSelectedIndex"></asp:HiddenField><br /><div id="OrgVal"></div></td>
<td style="width:220px"></td>
<td><asp:HiddenField runat="server" id="hdfDdlJobTitle1SelectedIndex"></asp:HiddenField><br /><div id="JobTItle1Val"></div></td>
<td><asp:HiddenField runat="server" id="hdfDdlJobTitle2SelectedIndex"></asp:HiddenField><br /><div id="JobTitle2Val"></div>
</td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"
Width="66px" />
<br />
<br />
<asp:Label ID="LabelResult" runat="server"></asp:Label>
</form>
</body>
</html>
这是文件后面的cs代码:
using System;
using System.Data;
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)
{
PopulateDDLFromXMLFile();
//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";
}
}
//populates the dropdownlist from xml file
public void PopulateDDLFromXMLFile()
{
DataSet ds = new DataSet();
ds.ReadXml(MapPath("App_Data") + "\\XMLFile1.xml");
//get the dataview of tables, which is default table name
DataView dv = ds.Tables["organization"].DefaultView;
DataView dj = ds.Tables["job"].DefaultView;
DataView dp = ds.Tables["specialty"].DefaultView;
//or we can use:
//DataView dv = ds.Tables[0].DefaultView;
//Now sort the DataView by column name "Name"
// dv.Sort = "Name";
//now define datatext field and datavalue field of dropdownlist
ddlOrg.DataTextField = "name";
ddlOrg.DataValueField = "value";
ddlJobTitle1.DataTextField = "name";
ddlJobTitle1.DataValueField = "value";
ddlJobTitle2.DataTextField = "name";
ddlJobTitle2.DataValueField = "value";
//now bind the dropdownlist to the dataview
ddlOrg.DataSource = dv;
ddlOrg.DataBind();
ddlJobTitle1.DataSource = dj;
ddlJobTitle1.DataBind();
ddlJobTitle2.DataSource = dp;
ddlJobTitle2.DataBind();
//insert new list item as helper
ddlOrg.Items.Insert(0, new ListItem("Select Organisation", "-1"));
ddlJobTitle1.Items.Insert(0, new ListItem("Select Professional Affiliation", "-1"));
ddlJobTitle2.Items.Insert(0, new ListItem("Select Specialty", "-1"));
}
/// <summary>
/// Bind Org dropdownlist
/// </summary>
protected void ddlOrg_SelectedIndexChanged(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("App_Data") + "\\XMLFile1.xml");
ddlJobTitle1.DataTextField = "name";
ddlJobTitle1.DataValueField = "value";
DataView view = new DataView(ds.Tables["job"]);
view.RowFilter = "value='" + ddlOrg.SelectedValue + "'";
ddlJobTitle1.Enabled = true;
ddlJobTitle1.DataSource = view;
ddlJobTitle1.DataBind();
}
/// <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;
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("App_Data") + "\\XMLFile1.xml");
ddlJobTitle1.DataTextField = "name";
ddlJobTitle1.DataValueField = "value";
DataView view = new DataView(ds.Tables["specialty"]);
view.RowFilter = "value='" + ddlJobTitle1.SelectedValue + "'";
ddlJobTitle2.Items.Clear();
ddlJobTitle2.DataSource = view;
ddlJobTitle2.DataBind();
ddlJobTitle2.Items.Insert(0, new ListItem("Select Specialty", "-1"));
// Initialize JobTitle2 dropdownlist selected index
hdfDdlJobTitle2SelectedIndex.Value = "0";
// Enable JobTitle2 dropdownlist when it has items
ddlJobTitle2.Enabled = true;
}
/// <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 Professional Affiliation";
}
else if (hdfDdlJobTitle2SelectedIndex.Value == "0" && strResult == string.Empty)
{
strResult = "Please select a Specialty.";
}
else
{
strResult = "You selected Org: " + ddlOrg.SelectedValue
+ " ; JobTitle1: " + ddlJobTitle1.SelectedValue
+ " ; JobTitle2: " + ddlJobTitle2.Items[iJobTitle2Selected].Value;
}
LabelResult.Text = strResult;
}
}
}
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<dropdowns>
<organizations>
<organization>
<value>A0EAD6A8813E474B9F9A3E0C64D72F61</value>
<name>Hospital</name>
</organization>
<organization>
<value>DD8BE886FA18401E9CF30AC64B49ACD8</value>
<name>Sleep Lab</name>
</organization>
<organization>
<value>F8182C928BE14239A576895D39591D38</value>
<name>Ventilation Home Service</name>
</organization>
<organization>
<value>E3865431ECF9499692E79DBF2FBAA3B2</value>
<name>Community Health Centre</name>
</organization>
<organization>
<value>711AA0D43D6346FC9AD38659D9CB8840</value>
<name>Home Care Provider</name>
</organization>
<organization>
<value>D54FAE898ECF4AB4A92B99C5AA536910</value>
<name>ResMed</name>
</organization>
<organization>
<value>4E44CDB4F7E5426E99368194BBB73282</value>
<name>Other</name>
</organization>
</organizations>
<jobs>
<job_organization name="Hospital">
<job>
<value>-2</value>
<name>Physician</name>
</job>
<job>
<value>111AEF6C39984713AA10E6F1D051F97E</value>
<name>Nurse</name>
</job>
<job>
<value>90CF5DB3D06A4B759F21A3F20B5F5DD4</value>
<name>Physiologist</name>
</job>
<job>
<value>55F8C6537718420DB6E4E3A7C087D540</value>
<name>Physiotherapist</name>
</job>
<job>
<value>884EB090E9B7469DA3CA8BA2D6FC4F4E</value>
<name>Biomedical Engineer/Technician</name>
</job>
<job>
<value>C2383AA850534CD9A4E03019386AA56C</value>
<name>Administrator</name>
</job>
<job>
<value>C8997E78569A4410956E5E7C6C1D57F2</value>
<name>Manager</name>
</job>
<job>
<value>5196E883DF614A319A3CDF4573C3E858</value>
<name>Supplies/Procurement</name>
</job>
</job_organization>
<job_organization name="Sleep Lab">
<job>
<value>D59C926B83A04E5297112FB599A8715F</value>
<name>Physician</name>
</job>
<job>
<value>111AEF6C39984713AA10E6F1D051F97E</value>
<name>Nurse</name>
</job>
<job>
<value>90CF5DB3D06A4B759F21A3F20B5F5DD4</value>
<name>Physiologist</name>
</job>
<job>
<value>3A5AAA4E5D474F3AAFC24D46B63BA073</value>
<name>Respiratory Technician</name>
</job>
<job>
<value>C2383AA850534CD9A4E03019386AA56C</value>
<name>Administrator</name>
</job>
<job>
<value>C8997E78569A4410956E5E7C6C1D57F2</value>
<name>Manager</name>
</job>
<job>
<value>ML.BASE.job.None</value>
<name>Other</name>
</job>
</job_organization>
<job_organization name="Ventilation Home Service">
<job>
<value>D59C926B83A04E5297112FB599A8715F</value>
<name>Physician</name>
</job>
<job>
<value>111AEF6C39984713AA10E6F1D051F97E</value>
<name>Nurse</name>
</job>
<job>
<value>55F8C6537718420DB6E4E3A7C087D540</value>
<name>Physiotherapist</name>
</job>
<job>
<value>3A5AAA4E5D474F3AAFC24D46B63BA073</value>
<name>Respiratory Technician</name>
</job>
<job>
<value>6E0D7BE08120464381FD70F4E07EAB4E</value>
<name>Care Assistant</name>
</job>
<job>
<value>C2383AA850534CD9A4E03019386AA56C</value>
<name>Administrator</name>
</job>
<job>
<value>F2D6388FF4CE49CDBFDEAB7D9DBEE9CF</value>
<name>Biomedical Engineer</name>
</job>
<job>
<value>C8997E78569A4410956E5E7C6C1D57F2</value>
<name>Manager</name>
</job>
<job>
<value>ML.BASE.job.None</value>
<name>Other</name>
</job>
</job_organization>
<job_organization name="Community Health Centre">
<job>
<value>D59C926B83A04E5297112FB599A8715F</value>
<name>Physician</name>
</job>
<job>
<value>318560B544674C2994131543FE67FF5E</value>
<name>Dentist</name>
</job>
<job>
<value>111AEF6C39984713AA10E6F1D051F97E</value>
<name>Nurse</name>
</job>
<job>
<value>C65378247CC6484BA56228E8102B5A1B</value>
<name>Technician</name>
</job>
<job>
<value>C2383AA850534CD9A4E03019386AA56C</value>
<name>Administrator</name>
</job>
<job>
<value>C8997E78569A4410956E5E7C6C1D57F2</value>
<name>Manager</name>
</job>
<job>
<value>ML.BASE.job.None</value>
<name>Other</name>
</job>
</job_organization>
<job_organization name="Home Care Provider">
<job>
<value>D59C926B83A04E5297112FB599A8715F</value>
<name>Physician</name>
</job>
<job>
<value>111AEF6C39984713AA10E6F1D051F97E</value>
<name>Nurse</name>
</job>
<job>
<value>55F8C6537718420DB6E4E3A7C087D540</value>
<name>Physiotherapist</name>
</job>
<job>
<value>C65378247CC6484BA56228E8102B5A1B</value>
<name>Technician</name>
</job>
<job>
<value>6E0D7BE08120464381FD70F4E07EAB4E</value>
<name>Care Assistant</name>
</job>
<job>
<value>C2383AA850534CD9A4E03019386AA56C</value>
<name>Administrator</name>
</job>
<job>
<value>C8997E78569A4410956E5E7C6C1D57F2</value>
<name>Manager</name>
</job>
<job>
<value>ML.BASE.job.None</value>
<name>Other</name>
</job>
</job_organization>
<job_organization name="ResMed">
<job>
<value>D59C926B83A04E5297112FB599A8715F</value>
<name>Physician</name>
</job>
<job>
<value>111AEF6C39984713AA10E6F1D051F97E</value>
<name>Nurse</name>
</job>
<job>
<value>55F8C6537718420DB6E4E3A7C087D540</value>
<name>Physiotherapist</name>
</job>
<job>
<value>C65378247CC6484BA56228E8102B5A1B</value>
<name>Technician</name>
</job>
<job>
<value>49CE2F9D94FA4A488D49B551C6C1A1DF</value>
<name>Salesperson</name>
</job>
<job>
<value>51ACA74CB1D74BD890AB19260C188FE8</value>
<name>Product Manager</name>
</job>
<job>
<value>C2383AA850534CD9A4E03019386AA56C</value>
<name>Admin</name>
</job>
<job>
<value>C8997E78569A4410956E5E7C6C1D57F2</value>
<name>Manager</name>
</job>
<job>
<value>EAAD4174C6AC48B19D90949289AB8A39</value>
<name>Authorised ResMed Distributor</name>
</job>
<job>
<value>ML.BASE.job.None</value>
<name>Other</name>
</job>
</job_organization>
<job_organization name="Other">
<job>
<value>5196E883DF614A319A3CDF4573C3E858</value>
<name>Supplies/Procurement</name>
</job>
</job_organization>
</jobs>
<specialties>
<specialty_job value="-2">
<specialty>
<value>E5B838C3AA304813B3EA4E28EE33702F</value>
<name>Anesthesiologist</name>
</specialty>
<specialty>
<value>36C9F22DC54347CEA39C142FE91E8B36</value>
<name>A and E</name>
</specialty>
<specialty>
<value>35E798C18DC04ECFB022DCF0E6DBEB51</value>
<name>Cardiologist</name>
</specialty>
<specialty>
<value>343B2555E45142E096546A3781680C02</value>
<name>Dental Surgeon</name>
</specialty>
<specialty>
<value>A5C669F6DB4D41DA8409960D6952023C</value>
<name>Diabetologist/Endocrinologist</name>
</specialty>
<specialty>
<value>2B01B4C29253459797CB596FE81C3AF4</value>
<name>ENT</name>
</specialty>
<specialty>
<value>6C761F3F81CB421BBD0746914031EF19</value>
<name>General Medicine</name>
</specialty>
<specialty>
<value>9DB49A58B8484E73AA24227DCDAD4C96</value>
<name>Intensivist</name>
</specialty>
<specialty>
<value>F80D023B174A421A82A140691BCD9E22</value>
<name>Neurologist</name>
</specialty>
<specialty>
<value>3F95529E15764EB1A55255DCEDDB6C4A</value>
<name>Pediatrician</name>
</specialty>
<specialty>
<value>9FC573D63A884C04A8675E86B43EABB9</value>
<name>Psychiatrist</name>
</specialty>
<specialty>
<value>7230D95FE4844A7785D25A73D5240BB9</value>
<name>Respiratory</name>
</specialty>
<specialty>
<value>E784A9F594BF468D85C51BBD5A9B0F5D</value>
<name>Trainee</name>
</specialty>
<specialty>
<value>431614E6148A4F639F912ED08803BD79</value>
<name>Other Physician</name>
</specialty>
</specialty_job>
</specialties>
</dropdowns>
什么阻止了第二次下拉填充?
答案 0 :(得分:1)
问题是你的第一个DropDownList有一个选中的值,它不应该在你正在过滤的“job”表中。您应该将代码更改为:
private void Bind()
{
int jobOrganizationId = 0;
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("App_Data") + "\\XMLFile1.xml");
DataRow[] rows = ds.Tables["job_organization"].Select("name = '" + ddlOrg.SelectedItem.Text + "'");
if(rows.Length > 0 && int.TryParse(rows[0]["job_organization_Id"].ToString(), out jobOrganizationId))
{
ddlJobTitle1.DataTextField = "name";
ddlJobTitle1.DataValueField = "value";
DataView view = new DataView(ds.Tables["job"]);
view.RowFilter = "job_organization_Id='" + jobOrganizationId.ToString() + "'";
ddlJobTitle1.Enabled = true;
ddlJobTitle1.DataSource = view;
ddlJobTitle1.DataBind();
}
}
我建议在Visual Studio中使用DataSet Viewer。从xml生成数据集时非常方便。