⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridviewpageable.cs

📁 gridview你超强功能很好的东东
💻 CS
📖 第 1 页 / 共 4 页
字号:
            base.RenderControl(writer);
        }

        private void CreatePagerable(HtmlTextWriter writer)
        {
            if (AllowPagerable == true)
            {
                //table开始
                writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, ColorFormat(PageBorderColor));
                writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, PageBorderWidth.ToString());

                if (PageBackColor == Color.Empty && this.HeaderRow != null)
                { //背景色
                    writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, ColorFormat(this.HeaderRow.BackColor));
                }
                else
                {
                    writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, ColorFormat(PageBackColor));
                }

                if (PageFontSize == FontUnit.Empty && this.HeaderRow != null)
                { //字体大小
                    writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, HeaderRow.Font.Size.ToString());
                }
                else
                {
                    writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, PageFontSize.ToString());
                }

                if (PageFontSize == FontUnit.Empty && this.HeaderRow != null)
                { //字体色
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorFormat(HeaderRow.ForeColor));
                }
                else
                {
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorFormat(PageFontColor));
                }

                if (this.HeaderRow != null)
                {
                    writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, HeaderRow.Font.Name);
                }

                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, this.Width.ToString());
                writer.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, "none");
                writer.AddAttribute(HtmlTextWriterAttribute.Align, this.HorizontalAlign.ToString());
                writer.AddAttribute(HtmlTextWriterAttribute.Class, PagerableCssClass);
                writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
                writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
                writer.RenderBeginTag(HtmlTextWriterTag.Table);
                //tr开始
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, PageNowWidth.ToString());
                writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "left");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(RenderPageNow());
                writer.RenderEndTag();

                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, PageRowWidth.ToString());
                writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(RenderPageRow());
                writer.RenderEndTag();

                writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "right");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(RenderFirst());
                writer.Write("  ");
                writer.Write(RenderBack());

                if (this.ShowNumericButton == true)
                {
                    writer.Write(" ");
                    RenderNumericButton(writer);
                    writer.Write(" ");
                }

                writer.Write("  ");
                writer.Write(RenderNext());
                writer.Write("  ");
                writer.Write(RenderLast());
                writer.RenderEndTag();

                //跳转栏开始
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, PageJumpWidth.ToString());
                writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "right");
                writer.AddStyleAttribute(HtmlTextWriterStyle.VerticalAlign, "middle");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                //跳转框
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
                writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID);
                writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
                writer.AddAttribute("onkeydown", "return CheckNum(event)");
                writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, JumpTextBoxBorderWidth.ToString());
                writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, ColorFormat(JumpTextBoxBorderColor));
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, JumpTextBoxWidth.ToString());
                writer.AddStyleAttribute(HtmlTextWriterStyle.Height, JumpTextBoxHeight.ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
                writer.RenderEndTag();
                writer.Write("&nbsp");

                //跳转按钮
                if (JumpButtonType == BUTTON_TYPE.Text)
                { //文本模式
                    writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
                    writer.AddAttribute(HtmlTextWriterAttribute.Value, "GO");
                    writer.AddAttribute(HtmlTextWriterAttribute.Name, "jumpbutton");
                    writer.AddAttribute(HtmlTextWriterAttribute.Id, "jumpbutton");
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink(this, "jump$"));
                    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, JumpButtonBorderWidth.ToString());
                    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, ColorFormat(JumpButtonBorderColor));
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Width, JumpButtonWidth.ToString());
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Height, JumpButtonHeight.ToString());
                    writer.RenderBeginTag(HtmlTextWriterTag.Input);
                    writer.RenderEndTag();
                }
                else
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, ConverImageUrl(JumpButtonImage));
                    writer.AddAttribute(HtmlTextWriterAttribute.Name, "jumpbutton");
                    writer.AddAttribute(HtmlTextWriterAttribute.Id, "jumpbutton");
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink(this, "jump$"));
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                }

                //跳转栏结束
                writer.RenderEndTag();

                //tr结束
                writer.RenderEndTag();
                //table结束
                writer.RenderEndTag();
            }
        }

        private string ColorFormat(Color color)
        {
            string str = color.Name;

            if (str.ToUpper().StartsWith("FF"))
            {
                str = "#" + str.Remove(0, 2);
            }

            return str;
        }

        #endregion

        #region 基类属性

        public override Unit Width
        {
            get
            {
                if (base.Width == Unit.Empty && AllowChangeCellWidth == false)
                {
                    return Unit.Percentage(100);
                }
                return base.Width;
            }
            set
            {
                base.Width = value;
            }
        }

        [Browsable(false)]
        public override PagerSettings PagerSettings
        {
            get
            {
                return base.PagerSettings;
            }
        }

        [Browsable(false)]
        public override bool AllowPaging
        {
            get
            {
                return true;
            }
            set
            {
                base.AllowPaging = value;
            }
        }

        [Browsable(false)]
        public override int PageCount
        {
            get
            {
                return base.PageCount;
            }
        }

        [Browsable(false)]
        public override int PageIndex
        {
            get
            {
                return base.PageIndex;
            }
            set
            {
                base.PageIndex = value;
            }
        }

        [Browsable(false)]
        public override int PageSize
        {
            get
            {
                return base.PageSize;
            }
            set
            {
                base.PageSize = value;
            }
        }

        /// <summary>
        /// 是否允许改变每列的宽度
        /// </summary>
        [DefaultValue(false)]
        [Description("是否允许改变每列的宽度")]
        public bool AllowChangeCellWidth
        {
            get
            {
                object o = ViewState["AllowChangeCellWidth"];
                if (o != null)
                {
                    return (bool)o;
                }
                return false;
            }
            set
            {
                ViewState["AllowChangeCellWidth"] = value;
            }
        }

        #endregion

        #region 呈现控件

        /// <summary>
        /// 呈现控件的HTML开始标记
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            string divid   = string.Format("{0}_table_Container", this.ClientID);
            string tbodyid = string.Format("{0}_GridBody_Cells", this.ClientID);

            if (AllowChangeCellWidth == true)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "tableContainer");
                writer.AddAttribute(HtmlTextWriterAttribute.Id, divid);
                writer.AddAttribute("onmousedown", string.Format("TableOnMouseDown('{0}');", divid));
                writer.AddAttribute("onmouseup", string.Format("TableOnMouseUp('{0}');", tbodyid));
                writer.AddAttribute("onmousemove", "TableOnMouseMove();");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
            }

            base.RenderBeginTag(writer);
        }

        /// <summary>
        /// 呈现控件的HTML结束标记
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderEndTag(HtmlTextWriter writer)
        {  
            base.RenderEndTag(writer);

            if (AllowChangeCellWidth == true)
            {
                writer.RenderEndTag();
            }
        }

        /// <summary>
        /// 呈现控件
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (AllowChangeCellWidth == true)
            {
                int i = 0;

                if (HeaderRow != null)
                {
                    foreach (TableCell cell in HeaderRow.Cells)
                    {
                        string str = cell.Text;
                        cell.ID = i.ToString();
                        cell.Text = string.Format("<div class=\"gridCell_standard\" columnIndex=\"{0}\">{1}</div>", i, str);
                        i++;
                    }
                }
            }

            base.Render(writer);
        }

        /// <summary>
        /// 呈现控件内容
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            string tbodyid = string.Format("{0}_GridBody_Cells", this.ClientID);

            if (AllowChangeCellWidth == true)
            {   
                //启用每列可被改变
                string str = string.Empty;

                this.RenderBeginTag(writer);

                if (this.HeaderRow != null)
                {
                    this.HeaderRow.RenderControl(writer);
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Id, tbodyid);
                writer.RenderBeginTag(HtmlTextWriterTag.Tbody);

                if (Rows != null)
                {
                    foreach (GridViewRow row in this.Rows)
                    {
                        foreach (TableCell cell in row.Cells)
                        {
                            if (cell.HasControls())
                            {
                                cell.Controls.AddAt(0, new LiteralControl("<div class=\"gridCell_standard\">"));
                                cell.Controls.Add(new LiteralControl("</div>"));
                            }
                            else
                            {
                                str = cell.Text;
                                cell.Text = string.Format("<div class=\"gridCell_standard\">{0}</div>", str);
                            }
                        }
                        row.RenderControl(writer);
                    }
                }

                writer.RenderEndTag();

                if (this.FooterRow != null)
                {
                    this.FooterRow.RenderControl(writer);
                }
                
                this.RenderEndTag(writer);

                if (AllowPagerable == true)
                {
                    CreatePagerable(writer);
                }

                //writer.Write(this.CreateJavascript());
            }
            else
            {
                //base.RenderBeginTag(writer);
                base.RenderContents(writer);
                if (AllowPagerable == true)
                {
                    CreatePagerable(writer);
                }
                //base.RenderEndTag(writer);
                
            }
        }

        #endregion

        #region 呈现样式

        /// <summary>
        /// 呈现页内容之前事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_PreRenderComplete(object sender, EventArgs e)
        {
            //样式文件
            if (this.AllowChangeCellWidth == true)
            {
                this.RegisterCssFile(Page.ClientScript.GetWebResourceUrl(this.GetType(), "GridViewPageable.gridview.css"));
            }

            string str = string.Format("{0}_gridviewini", this.ClientID);

            Page.ClientScript.RegisterClientScriptResource(this.GetType(), "GridViewPageable.gridview.js");

            //列宽拖动脚本
            if (!Page.ClientScript.IsStartupScriptRegistered(str) && AllowChangeCellWidth == true)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), str, this.CreateJavascript(), true);
            }

            //注册脚本块 跳转按钮
            if (!Page.ClientScript.IsClientScriptBlockRegistered("CheckNum"))
            {
                string js = BuildScript();
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CheckNum", js, true);
            }
        }

        /// <summary>
        /// 引发PreRender事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.Page.PreRenderComplete += new EventHandler(this.Page_PreRenderComplete);
        }

        /// <summary>
        /// 注样式文件
        /// </summary>
        /// <param name="path"></param>
        private void RegisterCssFile(string path)
        {
            HtmlLink link1 = new HtmlLink();
            link1.Attributes["type"] = "text/css";
            link1.Attributes["rel"] = "stylesheet";
            link1.Attributes["href"] = path;
            this.Page.Header.Controls.Add(link1);
        }

        #endregion

    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -