分享
 
 
 

3级分类中的Checkbox

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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);?????}????}???}??}?}}?

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有