WEB应用程序中分组数据的处理方法

在WEB应用程序中经常会碰到类似下边的分组数据:



这样的数据分散在WEB应用程序各个部分中,如果将他们都写死在页面,一旦要修改某项数据,将会是一件比较麻烦的事情,很自然的我们就想将他们存储到某个地方做为数据源,然后都从这边读取即可,接下来的问题是存储在哪边?最终我的选择是将他们存储在xml文件中。下边是一个简单的例子:

Data.xml:
<?xml version="1.0" encoding="utf-8" ?>
<xml>
  <group>
    <item value="1">选项一</item>
    <item value="2">选项二</item>
    <item value="3">选项三</item>
    <item value="4">选项四</item>
  </group>
</xml>

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" ValidateRequest="false" Inherits="_Default" %>

<!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 runat="server">
<title>WEB应用程序中分组数据的处理方法-Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="dropDownList1" runat="server">
        </asp:DropDownList><br />
        <asp:ListBox ID="listBox1" runat="server"></asp:ListBox><br />
        <asp:CheckBoxList ID="checkBoxList1" RepeatColumns="4" runat="server">
        </asp:CheckBoxList><br />
        <asp:RadioButtonList ID="radioButtonList1" RepeatColumns="4" runat="server">
        </asp:RadioButtonList>
    </div>
    </form>
</body>
</html>

Default.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Xml;

public partial class _Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            XMLDataBind(dropDownList1);
            XMLDataBind(listBox1);
            XMLDataBind(checkBoxList1);
            XMLDataBind(radioButtonList1);
        }
    }

    /// <summary>
    /// XML数据读取到Dictionary对象
    /// </summary>
    /// <returns></returns>
    protected Dictionary<string, string> GetList()
    {
        Dictionary<string, string> dicTemp = new Dictionary<string, string>();
        XmlDocument xmlDocument = new XmlDocument();
        //xmlDocument.LoadXml(XMLCONTENT);
        xmlDocument.Load(Server.MapPath("Data.xml"));
        XmlNodeList xmlNodeList = xmlDocument.SelectNodes("xml/group/item");
        foreach (XmlNode xmlNode in xmlNodeList)
        {
            dicTemp.Add(xmlNode.InnerText, xmlNode.Attributes["value"].InnerText);
        }
        return dicTemp;
    }

    /// <summary>
    /// 根据value属性值来获取节点值
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    protected string GetText(string value)
    {
        XmlDocument xmlDocument = new XmlDocument();
        //xmlDocument.LoadXml(XMLCONTENT);
        xmlDocument.Load(Server.MapPath("Data.xml"));
        XmlNode xmlNode = xmlDocument.SelectSingleNode(string.Format("xml/group/item[@value='{0}']", value));
        if (xmlNode != null)
        {
            return xmlNode.InnerText;
        }
        else
        {
            return string.Empty;
        }
    }

    #region 将Dictionary数据绑定到各个列表控件中
    /// <summary>
    /// DropDownList数据绑定
    /// </summary>
    /// <param name="dropDownList1"></param>
    protected void XMLDataBind(DropDownList dropDownList1)
    {
        dropDownList1.DataSource = GetList();
        dropDownList1.DataTextField = "key";
        dropDownList1.DataValueField = "value";
        dropDownList1.DataBind();
    }

    /// <summary>
    /// ListBox数据绑定
    /// </summary>
    /// <param name="listBox1"></param>
    protected void XMLDataBind(ListBox listBox1)
    {
        listBox1.DataSource = GetList();
        listBox1.DataTextField = "key";
        listBox1.DataValueField = "value";
        listBox1.DataBind();
    }

    /// <summary>
    /// RadioButtonList数据绑定
    /// </summary>
    /// <param name="radioButtonList1"></param>
    protected void XMLDataBind(RadioButtonList radioButtonList1)
    {
        radioButtonList1.DataSource = GetList();
        radioButtonList1.DataTextField = "key";
        radioButtonList1.DataValueField = "value";
        radioButtonList1.DataBind();
    }

    /// <summary>
    /// CheckBoxList数据绑定
    /// </summary>
    /// <param name="checkBoxList1"></param>
    protected void XMLDataBind(CheckBoxList checkBoxList1)
    {
        checkBoxList1.DataSource = GetList();
        checkBoxList1.DataTextField = "key";
        checkBoxList1.DataValueField = "value";
        checkBoxList1.DataBind();
    }
    #endregion
}

Default.aspx.cs(泛型方法版):
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Xml;

public partial class _Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            XMLDataBind<DropDownList>(dropDownList1);
            XMLDataBind<ListBox>(listBox1);
            XMLDataBind<CheckBoxList>(checkBoxList1);
            XMLDataBind<RadioButtonList>(radioButtonList1);
        }
    }

    /// <summary>
    /// XML数据读取到Dictionary对象
    /// </summary>
    /// <returns></returns>
    protected Dictionary<string, string> GetList()
    {
        Dictionary<string, string> dicTemp = new Dictionary<string, string>();
        XmlDocument xmlDocument = new XmlDocument();
        //xmlDocument.LoadXml(XMLCONTENT);
        xmlDocument.Load(Server.MapPath("Data.xml"));
        XmlNodeList xmlNodeList = xmlDocument.SelectNodes("xml/group/item");
        foreach (XmlNode xmlNode in xmlNodeList)
        {
            dicTemp.Add(xmlNode.InnerText, xmlNode.Attributes["value"].InnerText);
        }
        return dicTemp;
    }

    /// <summary>
    /// 根据value属性值来获取节点值
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    protected string GetText(string value)
    {
        XmlDocument xmlDocument = new XmlDocument();
        //xmlDocument.LoadXml(XMLCONTENT);
        xmlDocument.Load(Server.MapPath("Data.xml"));
        XmlNode xmlNode = xmlDocument.SelectSingleNode(string.Format("xml/group/item[@value='{0}']", value));
        if (xmlNode != null)
        {
            return xmlNode.InnerText;
        }
        else
        {
            return string.Empty;
        }
    }

    /// <summary>
    /// 将Dictionary数据绑定到各个列表控件中
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    protected void XMLDataBind<T>(T list) where T:ListControl
    {
        list.DataSource = GetList();
        list.DataTextField = "key";
        list.DataValueField = "value";
        list.DataBind();
    }
}


上一篇: XPath语法
下一篇: 泛型方法和非泛型方法优先级
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 4373
发表评论
登录后再发表评论!