3层分类的关系,用DataGrid来显示,这是非常简单的事情。现在在每一个datagrid上增加一个checkbox,用来控制自身分类下的checkbox
代码贴出来,怕以后忘记
///////////////////////////////////////////////////////////////////////////////////////
//添加相关产品分类的页面
///////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using LubeBLL;
using LubeBLL.Category;
using LubeDAL;
namespace Manage
{
?///
?/// newsProductCategory 的摘要说明。
?///
?public class newsProductCategory : System.Web.UI.Page
?{
??#region Web 窗体设计器生成的代码
??protected System.Web.UI.WebControls.Label LBNewsTitle;
??protected System.Web.UI.WebControls.LinkButton BTSave;
??protected System.Web.UI.WebControls.DataGrid dgFirstLevel;
??protected System.Web.UI.WebControls.DataGrid dgThirdLevel;
??protected System.Web.UI.WebControls.CustomValidator validRecordStatus;
??protected System.Web.UI.WebControls.LinkButton BTBackPreview;
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///
??/// 设计器支持所需的方法 - 不要使用代码编辑器修改
??/// 此方法的内容。
??///
??private void InitializeComponent()
??{???
???this.dgFirstLevel.PreRender += new System.EventHandler(this.dgFirstLevel_PreRender);
???this.BTSave.Click += new System.EventHandler(this.BTSave_Click);
???this.BTBackPreview.Click += new System.EventHandler(this.BTBackPreview_Click);
???this.Load += new System.EventHandler(this.Page_Load);
??}
??#endregion
??#region "页面定义的常量"
??//新闻预览的页面
??private const string m_NEWS_PREVIEW_PAGE=@"newsPreView.aspx";
??//出错转跳得默认页面
??private const string m_DEFAULTURL="newsManage.aspx";
??//没有登陆时转跳的页面
??private const string m_NO_POWER_PAGE=@"main.aspx?re=t";
??//更新关系时失败,留在原页面
??private const string m_UPDATE_FAILURE_MESSAGE=@"数据更新失败,请稍候再试";
??#endregion
??private DataSet dsBind;
??private void BTBackPreview_Click(object sender, System.EventArgs e)
??{
???//回到新闻预览页面
???string strBackURL=GetPreViewPageURL();
???Response.Redirect(strBackURL);
??}
??private void Page_Load(object sender, System.EventArgs e)
??{
???// 在此处放置用户代码以初始化页面
???if (!IsPostBack)
???{
????dsBind=new DataSet();
????//CheckPower
????//CheckPower();
????//SetViewState
????SetViewState();
????//DoInitDisplay
????DoInitDisplay();
????//BindData
????BindData();
???}
???//WriteJavaScript();
???WriteJavaScript();
??}
??#region "前台邦定3层关系用到的代码"
??//邦定第二层
??protected ArrayList GetSecondCategory(string strFirstSeq)
??{
???DataTable dtSecond=new DataTable();
???ArrayList arrReturn=new ArrayList();
???dtSecond=dsBind.Tables["SecondCategory"];
???Category item;
???foreach (DataRow dr in dtSecond.Rows)
???{
????item =new Category();
????if (dr["first_seq"].ToString()==strFirstSeq)
????{
?????item.SecondSeq=dr["second_seq"].ToString();
?????item.SecondDesc =dr["SecondDesc"].ToString();
?????item.FirstSeq =dr["first_seq"].ToString();
?????arrReturn.Add(item);
????}
???}
???return arrReturn;
??}
??//邦定第三层
??protected ArrayList GetThirdCategory(string strSecondSeq)
??{
???DataTable dtSecond=new DataTable();
???ArrayList arrReturn=new ArrayList();
???dtSecond=dsBind.Tables["ThirdCategory"];
???Category item;
???foreach (DataRow dr in dtSecond.Rows)
???{
????item =new Category();
????if (dr["second_seq"].ToString()==strSecondSeq)
????{
?????item.ThirdSeq =dr["third_seq"].ToString();
?????item.ThirdDesc=dr["ThirdDesc"].ToString();
?????item.SecondSeq =dr["second_seq"].ToString();
?????arrReturn.Add(item);
????}
???}
???return arrReturn;
??}
??#endregion
??private void WriteJavaScript()
??{
???this.BTSave.Attributes.Add("onclick","return SaveCheck();");
??}
??//设置ViewState
??private void SetViewState()
??{
???//参数news_seq为新闻的seq,rtype 标志显示新闻的关系的种类
???string strNews_seq=Request.QueryString["news_seq"];
???string strTopic_seq=Request.QueryString["topic_seq"];
???string strRelateType=Request.QueryString["rtype"];
???if (strNews_seq!=null && strNews_seq!=""
????&& strTopic_seq!=null && strTopic_seq!=""
????&& strRelateType!=null && strRelateType!=""? )
???{
????ViewState["news_seq"]=strNews_seq;//新闻的Seq
????ViewState["topic_seq"]=strTopic_seq;
????ViewState["rtype"]=strRelateType;//显示新闻的关系的类别
???}
???else
???{
????//没有新闻seq或者没有指定显示关系的类别,转跳到默认的新闻首页
????Response.Redirect(m_DEFAULTURL);
???}
??}
??//DoInitDisplay
??private void DoInitDisplay()
??{
??}
??//BindData
??private void BindData()
??{
???//根据news_seq 和 rtype来显示关系
???try
???{
????ClassNews t =new ClassNews();
????int intNewsSeq=Convert.ToInt32(ViewState["news_seq"].ToString());//news_seq
????dsBind=t.C1_GetProductTypeList(intNewsSeq);
????//得到基本信息
????//显示新闻的标题
????this.LBNewsTitle.Text=dsBind.Tables["NewsInfo"].Rows[0][0].ToString();
????this.dgFirstLevel.DataSource=dsBind;
????this.dgFirstLevel.DataMember=dsBind.Tables["FirstCategory"].TableName;
????this.dgFirstLevel.DataBind();
????if (dsBind.Tables["FirstCategory"].Rows.Count==0)
????{
?????this.BTSave.Visible=false;
????}
????else
????{
?????this.BTSave.Visible=true;
????}
???}
???catch(Exception e)
???{
????string a=e.Message;
???}
???finally
???{
????dsBind.Dispose();
???}
??}
??//得到返回新闻预览画面的url地址
??private string GetPreViewPageURL()
??{
???string strURL=m_NEWS_PREVIEW_PAGE
????+"?news_seq=" + ViewState["news_seq"].ToString()
????+ "&topic_seq="+ ViewState["topic_seq"].ToString()
????+ "&rtype=" +ViewState["rtype"].ToString();
???return strURL;
??}
??//得到所有选择的信息,返回以逗号分隔的字符串
??private ArrayList GetSelectContent()
??{
???System.Web.UI.WebControls.CheckBox chkFirst=new System.Web.UI.WebControls.CheckBox();
???System.Web.UI.WebControls.CheckBox chkSecond=new System.Web.UI.WebControls.CheckBox();
???System.Web.UI.WebControls.CheckBox chkThird=new System.Web.UI.WebControls.CheckBox();
???ArrayList arrReturn=new ArrayList();
???string sIDFirstSelect=null;
???string sIDFirstUnSelect=null;
???string sIDSecondSelect=null;
???string sIDSecondUnSelect=null;
???string sIDThridSelect=null;
???string sIDThirdUnSelect=null;
???foreach (DataGridItem dgFirstItem in this.dgFirstLevel.Items )//1级分类
???{
????chkFirst=(CheckBox)dgFirstItem.FindControl("chkFirstSelect");
????if (chkFirst.Checked)
????{
?????sIDFirstSelect=sIDFirstSelect + dgFirstItem.Cells[0].Text + ",";//1级分类选中的seq
????}
????else
????{
?????sIDFirstUnSelect=sIDFirstUnSelect + dgFirstItem.Cells[0].Text + ",";//1级分类没有选中的seq
????}
????foreach(DataGridItem dgSecondItem in ((DataGrid)(dgFirstItem.Cells[2].Controls[3])).Items )//2级分类的相关列表
????{
?????chkSecond=(CheckBox)dgSecondItem.FindControl("chkSecondSelect");
?????if (chkSecond.Checked)
?????{
??????sIDSecondSelect=sIDSecondSelect + dgSecondItem.Cells[0].Text + ",";//2级分类选中的seq
?????}
?????else
?????{
??????sIDSecondUnSelect=sIDSecondUnSelect + dgSecondItem.Cells[0].Text + ",";//2级分类没有选中的seq
?????}
?????foreach(DataGridItem dgThirdItem in ((DataGrid)(dgSecondItem.Cells[2].Controls[3])).Items )//3级分类的相关列表
?????{
??????chkThird=(CheckBox)dgThirdItem.FindControl("chkThirdSelect");
??????if (chkThird.Checked)
??????{
???????sIDThridSelect=sIDThridSelect + dgThirdItem.Cells[0].Text + ",";//3级分类选中的seq
??????}
??????else
??????{
???????sIDThirdUnSelect=sIDThirdUnSelect + dgThirdItem.Cells[0].Text + ",";//3级分类没有选中的seq
??????}
?????}
????}
???}
???arrReturn.Add(sIDFirstSelect);
???arrReturn.Add(sIDFirstUnSelect);
???arrReturn.Add(sIDSecondSelect);
???arrReturn.Add(sIDSecondUnSelect);
???arrReturn.Add(sIDThridSelect);
???arrReturn.Add(sIDThirdUnSelect);
???for (int i=0 ;i???{????if (arrReturn[i]!=null)????{?????arrReturn[i]=(arrReturn[i].ToString()).Substring(0,arrReturn[i].ToString().Length-1);????}???}??return arrReturn;??}??private void BTSave_Click(object sender, System.EventArgs e)??{???//保存打勾得关系???int intReturn=DoSaveRelation();???if (intReturn!=0)???{????//保存时发生错误,留在当前页面????this.validRecordStatus.ErrorMessage=m_UPDATE_FAILURE_MESSAGE;????this.validRecordStatus.IsValid=false;???}???else???{????//成功操作后????//返回新闻预览画面????string strBackURL=GetPreViewPageURL();????Response.Redirect(strBackURL);???}??}??//保存设定的关系??private int DoSaveRelation()??{???int intReturn=0;???//得到DataGrid中去掉CheckBox中打勾的seq???ArrayList arrSelectSeq=GetSelectContent();???//首先判断strSelectSeq是否为空,为空的话则说明不需要执行下面的操作???//调用接口函数,将选中的关系保存在数据库中???//ClassNews t=new ClassNews();???//string strErr=t.C1_NewsRelatedDelete(Convert.ToInt32(ViewState["news_seq"].ToString()),ViewState["rtype"].ToString(),strSelectSeq);???string strErr="OK";???if (strErr!=ClassCommon.PrintMessage.msg_Successful_DBUpdate)???{????intReturn--;???}???else???{????intReturn=0;???}???return intReturn;??}??private void dgFirstLevel_PreRender(object sender, System.EventArgs e)??{???System.Web.UI.WebControls.CheckBox chkFirst=new System.Web.UI.WebControls.CheckBox();???System.Web.UI.WebControls.CheckBox chkSecond=new System.Web.UI.WebControls.CheckBox();???System.Web.UI.WebControls.CheckBox chkThird=new System.Web.UI.WebControls.CheckBox();???CheckBox allbox=new CheckBox();???CheckBox allboxs=new CheckBox();???CheckBox allboxt=new CheckBox();???foreach (DataGridItem item in this.dgFirstLevel.Controls[0].Controls)//找到第一层全选的checkbox???{????if (item.ItemType == ListItemType.Header)????{?????allbox=(CheckBox)item.FindControl("allbox");?????break;????}???}???foreach (DataGridItem dgFirstItem in this.dgFirstLevel.Items )//1级分类???{????chkFirst=(CheckBox)dgFirstItem.FindControl("chkFirstSelect");????chkFirst.Attributes["onclick"]=String.Format("javascript:CheckParent('{0}');",allbox.ClientID);????foreach (DataGridItem item in ((DataGrid)(dgFirstItem.Cells[2].Controls[3])).Controls[0].Controls)//找到第二层全选的checkbox????{?????if (item.ItemType == ListItemType.Header)?????{??????allboxs=(CheckBox)item.FindControl("allboxs");??????break;?????}????}????foreach(DataGridItem dgSecondItem in ((DataGrid)(dgFirstItem.Cells[2].Controls[3])).Items )//2级分类的相关列表????{?????chkSecond=(CheckBox)dgSecondItem.FindControl("chkSecondSelect");?????chkSecond.Attributes["onclick"]=String.Format("javascript:CheckParent('{0}');",allboxs.ClientID);?????foreach (DataGridItem item in ((DataGrid)(dgSecondItem.Cells[2].Controls[3])).Controls[0].Controls)//找到第三层全选的checkbox?????{??????if (item.ItemType == ListItemType.Header)??????{???????allboxt=(CheckBox)item.FindControl("allboxt");???????break;??????}?????}?????foreach(DataGridItem dgThirdItem in ((DataGrid)(dgSecondItem.Cells[2].Controls[3])).Items )//3级分类的相关列表?????{??????chkThird=(CheckBox)dgThirdItem.FindControl("chkThirdSelect");??????chkThird.Attributes["onclick"]=String.Format("javascript:CheckParent('{0}');",allboxt.ClientID);?????}????}???}??}?}}?