GridView自定義分頁---派生類(5)

来源:互联网 发布:淘宝淘宝网首页 编辑:程序博客网 时间:2024/05/02 04:50

 /// <summary>
        /// 重新綁定所有資料,不做分頁
        /// </summary>
        private void DataBindForExcel()
        {
            SetDataSource(true);

            TotalPage = -1;

            this.DataBind();
        }

        /// <summary>
        /// 重新綁定所有資料
        /// </summary>
        public void ReDataBind()
        {
            SetDataSource(false);
            
            this.DataBind();
        }

        #region 清除gridview子元件

        /// <summary>
        /// 設定Cell的文字為元件的Text屬性
        /// </summary>
        private void RenderDataToText()
        {
            string[] arrIdx = ExportExcelClearControlIndex.Split(',');

            foreach (GridViewRow row in this.Rows)
            {
                foreach (string idx in arrIdx)
                {
                    TableCell cell = row.Cells[Convert.ToInt16(idx)];

                    if (cell.HasControls())
                    {
                        string cellText = "";

                        foreach (Control ctl in cell.Controls)
                        {
                            string value = "";

                            if (ctl.GetType().GetProperty("SelectedItem") != null)
                            {
                                value =
                                    ctl.GetType().GetProperty("SelectedItem").GetValue(ctl, null).ToString();
                            }
                            else if (ctl.GetType().GetProperty("Text") != null)
                            {
                                value =
                                    ctl.GetType().GetProperty("Text").GetValue(ctl, null).ToString();
                            }

                            if (value.Trim().Length > 0)
                            {
                                cellText += TrimHref(value.Trim()) + "  ";
                            }
                        }

                        cell.Text = cellText;
                    }
                    else
                    {
                        cell.Text = TrimHref(cell.Text);
                    }
                  
                }
            }
        }

        /// <summary>
        /// 移除連結字申
        /// </summary>
        /// <param name="value">欄位文字</param>
        /// <returns>已移除連結字申的欄位文字</returns>
        private string TrimHref(string value)
        {
            if (value.ToLower().IndexOf("<a") > -1 && value.ToLower().IndexOf("</a>") > -1)
            {
                string pattent = @"<a(.*?)</a>";

                List<string> linkInfos = new List<string>();

                MatchCollection matches = Regex.Matches(value, pattent, RegexOptions.IgnoreCase);

                foreach (Match match in matches)
                {
                    linkInfos.Add(Regex.Replace(
                        Regex.Replace(match.Value, "<a(.*?)>", "", RegexOptions.IgnoreCase),
                        "</a>", "", RegexOptions.IgnoreCase));
                }

                return String.Join(Environment.NewLine, linkInfos.ToArray());
            }

            return value;
        }
 
        #endregion
    }
}