dripdown 调用 XML中的数据
//在Web层中 XML文件夹添加 FacilityType.xml 如下:
<?xml version="1.0" encoding="utf-8" ?>
<FacilityType>
<Type id="1" name="类型一"/>
<Type id="1" name="类型二"/>
<Type id="1" name="类型三"/>
</FacilityType>
↓↓↓↓↓↓↓↓↓↓↓
//在Model层中添加FacilityType1Entity.cs 如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
public class FacilityType1Entity
{
private string _Name = string.Empty;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
}
↓↓↓↓↓↓↓↓↓↓↓
//在Web层中public class PageBase : System.Web.UI.Page 添加如下:
/// <summary>
/// 获取项目设施类型
/// </summary>
/// <returns></returns>
public List<FacilityType1Entity> GetAllFacilityType()
{
List<FacilityType1Entity> list = new List<FacilityType1Entity>();
string filePath = Server.MapPath("~/XML/FacilityType.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNodeList nodelist = xmlDoc.SelectSingleNode("FacilityType").ChildNodes;
foreach (XmlNode node in nodelist)
{
string name = node.Attributes["name"].Value;
FacilityType1Entity typeing = new FacilityType1Entity();
typeing.Name = name;
list.Add(typeing);
}
return list;
}
↓↓↓↓↓↓↓↓↓↓↓
//在窗体FactorsAdd.aspx前台添加dropdown控件 如下:
<tr style="padding-bottom:10px">
<td class="style1">项目设施类型<span class="needInput"></span></td>
<td class="style2">
<asp:DropDownList ID="ddlType" οnclick="ClearErrColor(this.id)" οnblur="checkFacilityType(this.value)" οnfοcus="ClearErrColor(this.id)" runat="server" Width="196px" CssClass="input_style">
</asp:DropDownList>
</td>
</tr>
该窗体后台添加如下:
/// <summary>
/// 页面初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
//第一次加载
if (!IsPostBack)
{
//加载下拉框
DataBaseLoad();
}
}
/// <summary>
/// 加载下拉框信息
/// </summary>
protected void DataBaseLoad()
{
try
{
PageBase bdvFacilityTypeList = new PageBase();
//项目设施类型列表
ddlType.DataSource = bdvFacilityTypeList.GetAllFacilityType();
ddlType.DataTextField = "name"; //上面再PageBase中GetAllFacilityType中的string name = node.Attributes["name"].Value;
ddlType.DataBind();
//添加默认选择项
ListItem _liType = new ListItem();
_liType.Text = ""; // _liType.Text = "请选择类型"; 这只是显示的标题
_liType.Value = ""; // _liType.Value = "请选择类型的值"; 这才是该标题对应的值
ddlType.Items.Insert(0, _liType);
ddlType.SelectedValue = ""; //设置默认选择项 ddlType.SelectedValue = "请选择类型的值"; 这才是该标题对应的值
}
catch
{
Alert(upFacilityAdd, GetMessage("LOAD_ERROR"));
}
}