您的位置:首页 > 运维架构

从xml文件中绑定数据到DropDownList控件上

2013-09-23 16:11 736 查看
参考了2篇文章:

/article/4967724.html

http://blog.sina.com.cn/s/blog_4d4d3ade01000apj.html

private string _RootPath;
/// <summary>
/// 系统的根目录
/// </summary>
public string RootPath
{
get
{

_RootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath).ToLower();//当前的绝对路径
if (_RootPath.Length == 1)
{
_RootPath = "";
}
return _RootPath;
}
}


private void BindDepConfig()
{
dropdownDep.DataSource = createDataSource();
dropdownDep.DataTextField = "depTextField";
dropdownDep.DataValueField = "depValueField";
dropdownDep.DataBind();

}

private ICollection createDataSource()

{

DataTable dt = new DataTable();

//define the columns of the table
dt.Columns.Add("depTextField",typeof(string));
dt.Columns.Add("depValueField",typeof(string));

//read the content of the xml file into a DataSet
DataSet lanDS = new DataSet();
string filePath = RootPath + "/Data/Xml/ThridDepConfig.xml";
lanDS.ReadXml(filePath);

if(lanDS.Tables.Count > 0)
{
foreach(DataRow copyRow in lanDS.Tables[0].Rows)
{
dt.ImportRow(copyRow);
}
}

DataView dv = new DataView(dt);

return dv;
}


<?xml version="1.0" encoding="utf-8"?>

<depTypes>
<dep>
<depValueField>电教处</depValueField>
<depTextField>电教处</depTextField>
</dep>
<dep>
<depValueField>总务处</depValueField>
<depTextField>总务处</depTextField>
</dep>
<dep>
<depValueField>后勤处</depValueField>
<depTextField>后勤处</depTextField>
</dep>
</depTypes>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: