方法一:
1。加一個gridview:
<asp:gridview ;
            tb.EnableViewState = true;
            tb.AutoPostBack = true;
           
            e.Row.Cells[c - 1].Controls.Add(lt);
            e.Row.Cells[c - 1].Controls.Add(tb);
        }
    }

方法二:
實現ITemplate接口的InstantiateIn方法
    public class TextboxTemplate : ITemplate
    {
        /// <summary>
        /// TODO: 自定義TextBox模板列;
        /// </summary>
        private static int Count = 0;
        private bool _isnumber=false;
        private bool _isnull=false;
        private WebControl _wcl;
        private Type _ty;

        public bool ValidateIsNumber
        {
            get
            {
                return _isnumber;
            }
            set
            {
                if (_isnumber != value)
                {
                    _isnumber = value;
                }
            }
        }
        public bool ValidateIsNull
        {
            get
            {
                return _isnull;
            }
            set
            {
                if (_isnull != value)
                {
                    _isnull = value;
                }
            }
        }
        public WebControl Control
        {
            get
            {
                return _wcl;
            }
            set
            {
                _wcl = value;
            }
        }
        public TextboxTemplate(Type tp)
        {
            Count++;
            this._ty = tp;
        }

        public void InstantiateIn(Control container)
        {
            Assembly ass = Assembly.GetAssembly(_ty);
            _wcl = ass.CreateInstance(_ty.ToString()) as WebControl;
            this.Control = _wcl;
            this.Control.ID = "tb" + Count;

            if (this.ValidateIsNull)
            {
                RequiredFieldValidator rf = new RequiredFieldValidator();
                rf.ErrorMessage = "請輸入數據!";
                rf.Text = "*";
                rf.ControlToValidate = this.Control.ID;
                rf.SetFocusOnError = true;
                rf.ValidationGroup = "vg0";
                container.Controls.Add(rf);
            }
            if (this.ValidateIsNumber)
            {
                RegularExpressionValidator re = new RegularExpressionValidator();
                re.ValidationExpression = @"^-?\d+(\.\d{2})?$";
                re.ErrorMessage = "請輸入有效的價格!";
                re.Text = "*";
                re.ControlToValidate = this.Control.ID;
                re.SetFocusOnError = true;
                re.ValidationGroup = "vg0";
                container.Controls.Add(re);
            }
            container.Controls.Add(this.Control);
        }
    }

運用示例:
    protected void Button3_Click(object sender, EventArgs e)
    {
        TemplateColumn tc = new TemplateColumn();
        TextBox tb = new TextBox();
        tc.ItemTemplate = new TextboxTemplate(tb.GetType());
        tc.HeaderText = "單價";
        DataGrid2.Columns.Add(tc);       
    }

相关文章:

  • 2022-01-05
  • 2022-02-23
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2021-11-28
  • 2021-12-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2021-12-27
相关资源
相似解决方案