练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class checkboxlist : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["pre"] = "";//上一次被选项
            ViewState["now"] = "";//当前被选择项
            this.CheckBoxList1.DataSource = GetMianShouClassList();
            this.CheckBoxList1.DataBind();
        }
    }
    public static List<info> GetMianShouClassList()
    {
        XmlNodeList nodelist = GetUrl(1);
        List<info> adslist = new List<info>();
        foreach (XmlNode node in nodelist)
        {
            info ads = new info();
            ads.Id = node.Attributes["id"].Value;
            ads.Des = node.Attributes["des"].Value;
            ads.Value = node.InnerText;
            adslist.Add(ads);
        }
        return adslist;
    }
    public static List<info> GetMianShouZClassList(int category)
    {
        XmlNodeList nodelist = GetItemUrl(category);
        List<info> adslist = new List<info>();
        foreach (XmlNode node in nodelist)
        {
            info ads = new info();
            ads.Id = node.Attributes["id"].Value;
            ads.Des = node.Attributes["des"].Value;
            ads.Value = node.InnerText;
            adslist.Add(ads);
        }
        return adslist;
    }
    public static XmlNodeList GetUrl(int id)
    {
        XmlHelper xtool = new XmlHelper("E:\\项目http://www.cnblogs.com/epwqgdnvrhok/admin/file://test//XMLFile.xml");
        return xtool.getNodes("/ml/baomingbanchi/category[@id='" + XmlHelper.getStrId(id) + "']/item");
    }
    public static XmlNodeList GetItemUrl(int category)
    {
        XmlHelper xtool = new XmlHelper("E:\\项目http://www.cnblogs.com/epwqgdnvrhok/admin/file://test//XMLFile.xml");
        string url = "/ml/Subjects/category[@id='" + XmlHelper.getStrId(category) + "']/item";
        XmlNodeList list = xtool.getNodes("/ml/Subjects/category[@id='" + XmlHelper.getStrId(category) + "']/item");
        return xtool.getNodes("/ml/Subjects/category[@id='" + XmlHelper.getStrId(category) + "']/item");
    }
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string a = this.CheckBoxList1.SelectedValue;
       
        string preid="",now = "",first="";
        if (ViewState["pre"].ToString() == "")//第一次被点击时
        {
            int id = int.Parse(this.CheckBoxList1.SelectedValue);
            ViewState["pre"] = this.CheckBoxList1.SelectedValue+",";
            ViewState["now"] = this.CheckBoxList1.SelectedValue+",";
        }
        else
        {
            foreach (ListItem aa in CheckBoxList1.Items)
            {
                if (aa.Selected == true)
                {
                    now += aa.Value + ",";
                }
            }
            ViewState["now"] = now;
        }
        string nowid = GetNowSelectId(ViewState["pre"].ToString(), ViewState["now"].ToString());
        ViewState["pre"] = ViewState["now"];
        Response.Write("当前ID"+nowid);
        if (nowid != "")
        {
            int ii = int.Parse(nowid.Replace(",", ""));
            this.CheckBoxList2.DataSource = GetMianShouZClassList(ii);
            this.CheckBoxList2.DataBind();
        }
    }
    //返回当前点击CHECKBOX的ID
    private string GetNowSelectId(string preid,string now)
    {
        string nowid = "";
        if (preid == now)
        {
            nowid = preid;
        }
        else
        {
            string[] prellist = preid.Split(',');
            string[] nowlist = now.Split(',');
            for (int i = 0; i < nowlist.Length; i++)
            {
                for (int j = 0; j < prellist.Length; j++)
                {
                    if (prellist[j] == nowlist[i])
                    {
                        nowlist[i] = "";
                    }
                }
            }
            for (int k = 0; k < nowlist.Length; k++)
            {
                if (nowlist[k] != "")
                {
                    nowid = nowlist[k];
                }
            }
                //nowid = nowlist.ToString();
                //if (now.Contains(preid))
                //{
                //    nowid = now.Replace(preid, "");
                //}
        }
        return nowid;
    }
}


private void FindSelecedControl ( Control control ) {
if ( control is Button ) {
Button bt = ( Button )control;
bt.Enabled = false;
}
else {
for ( int i = 0; i < control.Controls.Count; i++ ) {
FindSelecedControl ( control.Controls[ i ] );
}
}
}

posted @ 2009-03-21 00:05  悟〈--觉  阅读(143)  评论(0编辑  收藏  举报