使用ajax的webpart,展现了主子表联动关系

 

 

 public class HumanProjectWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        #region 字段

        private GridView DetailGrid;
        private Table searchTable = new Table();
        private List<ImageButton> searchButtonList = new List<ImageButton>();

        private Table UITable = new Table();
        private Label lbl_message;
        private SPList humanList;
        private SPList humanProjectList;

        private string humanListName = "人员列表";
        private string humanProjectListName = "人员项目列表";
        private string mainColumnsTexts="人员,职务";
        private string detailColumnsTexts = "项目名称,开始时间,结束时间,工期,完成百分比";

        UpdatePanel UP;
        #endregion

        #region 属性
        /// <summary>
        /// 图片url
        /// </summary>
        [WebBrowsable(true)]
        [Personalizable(true)]
        [WebDisplayName("图片url"), WebDescription("图片url"), SPWebCategoryName("属性")]
        public string ImageUrl
        {
            get;
            set;
        }
        #endregion

        #region 构造器
        public HumanProjectWebPart()
        {
        }
        #endregion

        #region 保护方法
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ScriptManager SM = ScriptManager.GetCurrent(this.Page);
            if (SM == null)
            {
                SM = new ScriptManager();
                SM.ID = "SM";
                if (this.Page.IsPostBack)
                {
                    Page.ClientScript.RegisterStartupScript(typeof(HumanProjectWebPart),
                        this.ID, "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);


                }

                if (this.Page.Form != null)
                {

                    string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];

                    if (!string.IsNullOrEmpty(formOnSubmitAtt) && formOnSubmitAtt ==
                    "return _spFormOnSubmitWrapper();")
                    {

                        this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";

                    }

                    this.Page.Form.Controls.AddAt(0, SM);

                }

            }
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        
            bool valid = this.IsParamsValide();
            try
            {
                if (valid)
                {
                    this.InitParams();
                    this.CreateUI();
                    this.CreateSearchTable();
                    this.CreateDetailGrid();
                    this.HideMessage();
                }
                else
                {
                    if (this.UP == null)
                    {
                        this.Controls.Add(this.lbl_message);
                    }

                    this.ShowMessage("请正确配置属性");
                }
            }
            catch (Exception ex)
            {
                this.ShowMessage(ex.Message);
            }


        }
        #endregion

        #region 私有方法
        private void HideMessage()
        {
            this.lbl_message.Text = string.Empty;
            this.lbl_message.Visible = false;
        }
        private void InitParams()
        {
            this.humanList = SPContext.Current.Web.Lists[this.humanListName];
            this.humanProjectList = SPContext.Current.Web.Lists[this.humanProjectListName];
        }
        private bool IsParamsValide()
        {
            if(String.IsNullOrEmpty( ImageUrl))
            {
             return false;
            }

            return true;
        }     
        private void ShowMessage(string message)
        {
            this.lbl_message.Text = message;
            this.lbl_message.Visible = true;
        }
        private void CreateUI()
        {
            TableRow row0 = new TableRow();
            this.UITable.Rows.Add(row0);

            TableCell cell00 = new TableCell();
            row0.Cells.Add(cell00);

            TableRow row1 = new TableRow();
            TableCell cell10 = new TableCell();
            row1.Cells.Add(cell10);

            this.UITable.Rows.Add(row1);
         //   this.UITable.BackColor=ColorTranslator.FromHtml(""
            //this.UITable.BackColor = Color.Fuchsia;
            this.Controls.Add(this.UITable);
        }     
        /// <summary>
        ///
        /// </summary>
        private void CreateSearchTable()
        {
            DataTable dtTable = this.GetHumanData();
           
           
           
            string [] mainColumnsTextsArray=mainColumnsTexts.Split(',');

            this.searchTable.CssClass = "HumanProjcectableBorder2";
            this.searchTable.CellSpacing = 1;
            this.searchTable.CellPadding = 3;
           

            TableRow row0 = new TableRow();
            this.searchTable.Rows.Add(row0);
            for (int i = 0; i < mainColumnsTextsArray.Length; i++)
            {
                TableCell headCells = new TableCell();
                Label txt = new Label();
                txt.CssClass= "HumanProjcectHeadFont";
                txt.Text = mainColumnsTextsArray[i];
                headCells.Controls.Add(txt);
                row0.Cells.Add(headCells);

                headCells.CssClass = "HumanProjcectHead";
            }

            TableCell buttonCells= new TableCell();
            Label buttonHeadTxt = new Label();
            buttonHeadTxt.Text = "请选择";
            buttonCells.Controls.Add(buttonHeadTxt);
            row0.Cells.Add(buttonCells);
            buttonHeadTxt.CssClass = "HumanProjcectHeadFont";
            buttonCells.CssClass = "HumanProjcectHead";

            string idDisplayName="人员ID";
            string idInternalName=this.GetListFildInternalName(idDisplayName,this.humanList);

            string discDisplayName = "描述";
            string discDisplayNameInternal = this.GetListFildInternalName(discDisplayName, this.humanList);

            for (int j = 0; j < dtTable.Rows.Count; j++)
            {
                DataRow row = dtTable.Rows[j];
                TableRow rowItem = new TableRow();
                this.searchTable.Rows.Add(rowItem);
               
                for (int k = 0; k < mainColumnsTextsArray.Length+1; k++)
                {
                    TableCell itemCell = new TableCell();
                    if (k < mainColumnsTextsArray.Length)
                    {
                        Label txt = new Label();
                      
                        string displayName = mainColumnsTextsArray[k];
                        string colName = GetListFildInternalName(displayName, this.humanList);
                        txt.Text = row[colName].ToString();
                        itemCell.Controls.Add(txt);
                        rowItem.Cells.Add(itemCell);
                    }
                    else
                    {
                        ImageButton butitem = new ImageButton();
                        butitem.Width = Unit.Pixel(20);
                        butitem.Height = Unit.Pixel(18);
                        //this.Page.Request.Url
                       
                        butitem.ToolTip = row[discDisplayNameInternal].ToString();
                        butitem.ImageUrl = ImageUrl + "man1.jpg";
                        this.searchButtonList.Add(butitem);
                        butitem.ID = "humanButton" + j;
                        butitem.CommandArgument = row[idInternalName].ToString();
                        butitem.Click += new ImageClickEventHandler(butitem_Click);
                        itemCell.HorizontalAlign = HorizontalAlign.Center;
                        itemCell.Controls.Add(butitem);
                        rowItem.Cells.Add(itemCell);

                     
                    }

                    if (j % 2 == 0)
                    {
                        itemCell.CssClass = "HumanProjcectItemRow";
                    }
                    else
                    {
                        itemCell.CssClass = "HumanProjcectAlterItemRow";
                    }
                }           
            }

            this.UITable.Rows[0].Cells[0].Controls.Add(this.searchTable);           
      
        }
        void butitem_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton but = sender as ImageButton;
            string id = but.CommandArgument;
            DataTable dtTable = GetDetailByHumanID(id);

            this.DetailGrid.DataSource = dtTable;
            this.DetailGrid.DataBind();

            if (dtTable == null || dtTable.Rows.Count == 0)
            {
                this.ShowMessage("没有相关的项目信息。");
            }
            else
            {
                this.HideMessage();
            }
        }

        void buttonItem_Click(object sender, EventArgs e)
        {
            ImageButton but = sender as ImageButton;
            string id = but.CommandArgument;

            DataTable dtTable = GetDetailByHumanID(id);

            this.DetailGrid.DataSource = dtTable;
            this.DetailGrid.DataBind();


        }

        private string GetListFildInternalName(string displayName, SPList list)
        {
            string result = list.Fields[displayName].InternalName;
             return result;
        }

        /// <summary>
        ///
        /// </summary>
        private void CreateDetailGrid()
        {
            this.DetailGrid = new GridView();

            this.DetailGrid.CellSpacing = 1;
            this.DetailGrid.CellPadding = 3;
            this.DetailGrid.GridLines = GridLines.None;

            this.DetailGrid.AutoGenerateColumns = false;
            this.DetailGrid.CssClass = "HumanProjcectableBorder1";
            string[] detailColumnsTextsArray = detailColumnsTexts.Split(',');
            for (int i = 0; i < detailColumnsTextsArray.Length; i++)
            {
                BoundField column = new BoundField();
                string displayName = detailColumnsTextsArray[i];
                string colName = GetListFildInternalName(displayName, this.humanProjectList);
                column.DataField = colName;
                column.HeaderText = displayName;
                ///need method
                if (this.humanProjectList.Fields[displayName].Type == SPFieldType.DateTime)
                {
                    column.DataFormatString = "{0: yyyy/M/d}";
                }

                this.DetailGrid.Columns.Add(column);
            }

            this.DetailGrid.RowCreated += new GridViewRowEventHandler(DetailGrid_RowCreated);
            this.DetailGrid.RowDataBound += new GridViewRowEventHandler(DetailGrid_RowDataBound);

            UP = new UpdatePanel();
            UP.ID = "UP";
            UP.ContentTemplateContainer.Controls.Add(this.DetailGrid);

            if (this.lbl_message == null)
            {
                this.lbl_message = new Label();
            }

            UP.ContentTemplateContainer.Controls.Add(this.lbl_message);

            //设定UpdatePanelUpda
            UP.UpdateMode = UpdatePanelUpdateMode.Conditional;

            for (int i = 0; i < this.searchButtonList.Count; i++)
            {
                ImageButton itemButton = this.searchButtonList[i];
                AsyncPostBackTrigger apbk = new AsyncPostBackTrigger();
                apbk.ControlID = itemButton.ID;
                apbk.EventName = "Click";
                UP.Triggers.Add(apbk);
            }

            this.UITable.Rows[1].Cells[0].Controls.Add(UP);
        }

        void DetailGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    cell.CssClass = "HumanProjcectHead";                  
                }
            }
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal)
                {
                    foreach (TableCell cell in e.Row.Cells)
                    {
                        cell.CssClass = "HumanProjcectItemRow";
                    }
                }
                else if (e.Row.RowState == DataControlRowState.Alternate)
                {
                    foreach (TableCell cell in e.Row.Cells)
                    {
                        cell.CssClass = "HumanProjcectAlterItemRow";
                    }
                }
            }

          
        }

        void DetailGrid_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.CssClass = "HumanProjcectableBorder1";
            }
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal)
                {
                    e.Row.CssClass = "HumanProjcectItemRow";
                   
                }
                else if (e.Row.RowState == DataControlRowState.Alternate)
                {
                    e.Row.CssClass = "HumanProjcectAlterItemRow";
                }
            }
        }

        private DataTable GetHumanData()
        {
            DataTable dtTable = this.humanList.Items.GetDataTable();
            return dtTable;
        }

        private DataTable GetDetailByHumanID(string id)
        {
            SPQuery myQuery = new SPQuery();
            string displayId = "所有者";
            string internalID = GetListFildInternalName(displayId, this.humanProjectList);
            myQuery.Query = @"<Where><Eq><FieldRef Name='" + internalID + "'/><Value Type='Text'>" + id + @"</Value></Eq></Where>";

            SPListItemCollection itemCollection = this.humanProjectList.GetItems(myQuery);
            DataTable dtTable = itemCollection.GetDataTable();
            return dtTable;
        }

        #endregion
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值