不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
WEB应用程序中分组数据的处理方法
编辑:dnawo 日期:2008-09-14
在WEB应用程序中经常会碰到类似下边的分组数据:

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

这样的数据分散在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>
<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>
<!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
}
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();
}
}
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();
}
}
评论: 0 | 引用: 0 | 查看次数: 4373
发表评论
请登录后再发表评论!