首页  ·  知识 ·  云计算
在client端一行一行增加的gridview
佚名  http://city.udn.com/57239/2291682  综合  编辑:dezai  图片来源:网络
id=mainbody> 有時候會想要一行一行地顯現gridview 即按 新增 , 則增加一列例如在檔案上傳時,現只出現一列,使用者可以按 新增上傳欄位來上
 

有時候會想要一行一行地顯現gridview 即按 新增 , 則增加一列
例如在檔案上傳時,現只出現一列,使用者可以按 新增上傳欄位來上傳多個檔案
於是研究寫了這樣一個gridview

 

 using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;

namespace Naonet
{
    [ToolboxData("<{0}:IncrementalGridView runat=server>")]

    public partial class IncrementalGridView : GridView
    {

        string _addRowText = "新增...";
        [DefaultValue("新增...")]
        public virtual string AddRowText
        {
            get { return _addRowText; }
            set { _addRowText = value; }
        }

        Literal _litScript = new Literal();

        HyperLink _addRowLink = new HyperLink();
        public HyperLink AddRowLink
        {
            get { return _addRowLink; }
        }

 

        int _totalRowCount = 0;
        ///


        /// 總列數, 0 為不設定
        ///

        [DefaultValue(0)]
        public virtual int TotalRowCount
        {
            get
            {
                return _totalRowCount;
            }
            set
            {
                if (value <= 0) _totalRowCount = 0;
                else _totalRowCount = value;
            }
        }
        ///
        /// 初始列數
        ///

        int _initialRowCount = 1;
        [DefaultValue(1)]
        public int InitialRowCount
        {
            get
            {

                if (_initialRowCount > TotalRowCount && TotalRowCount != 0)
                    return TotalRowCount;
                else return _initialRowCount;
            }
            set
            {
                if (value <= 0) _initialRowCount = 1;
                else _initialRowCount = value;
            }
        }

        public IncrementalGridView()
            : base()
        {
            this.RowDataBound += new System.Web.UI.WebControls.GridViewRowEventHandler(IncrementalGridView_RowDataBound);

        }

 

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            GridView gv = (GridView)this;


            if (!gv.Page.IsPostBack)
            {
                if (DataSource == null && !IsBoundUsingDataSourceID)
                {
                    if (TotalRowCount > 0)
                        DataSource = new object[TotalRowCount];
                    else DataSource = new object[1];
                    DataBind();
                }
            }


            if (this.FooterRow != null)
            {
                GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
                row.Cells.Clear();
                TableCell cell = new TableCell();
                cell.ColumnSpan = Columns.Count;
                cell.Controls.Add(_addRowLink);
                cell.Controls.Add(_litScript);
                cell.Width = Unit.Percentage(100);
                row.Cells.Add(cell);
                TableRowCollection rows = (TableRowCollection)FooterRow.Parent.GetType().GetProperty("Rows").GetValue(FooterRow.Parent, new object[] { });
                rows.Add(row);
            }

            /*
             * 設定為顯示增加連結   且
             *
             * */

            _addRowLink.NavigateUrl = string.Format("javascript:addGridViewRow{0}()", gv.ClientID);
            _addRowLink.Text = AddRowText;
            _litScript.Text = string.Format(@"

", gv.ClientID,
 _addRowLink.ClientID);
            if (Rows.Count > 1 &&
                (
                (
                TotalRowCount > 0 && (
                (TotalRowCount >= Rows.Count && Rows.Count > InitialRowCount) ||
                (Rows.Count > TotalRowCount && TotalRowCount > InitialRowCount)
                )
                ) ||
                (
                TotalRowCount == 0 && Rows.Count > InitialRowCount
                )
                )
                )
            {


            }
            else
            {
                _addRowLink.Visible = false;
            }

 

        }


        void IncrementalGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView gv = sender as GridView;
                if (e.Row.RowIndex + 1 > InitialRowCount)
                    e.Row.Style[HtmlTextWriterStyle.Display] = "none";
                else e.Row.Style[HtmlTextWriterStyle.Display] = "block";


                if (TotalRowCount != 0 && e.Row.RowIndex + 1 > TotalRowCount)
                    e.Row.Visible = false;
            }

        }
    }
}

本文作者:佚名 来源:http://city.udn.com/57239/2291682
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读