惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
P
Privacy International News Feed
W
WeLiveSecurity
Spread Privacy
Spread Privacy
S
Schneier on Security
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
Cisco Talos Blog
Cisco Talos Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
Lohrmann on Cybersecurity
P
Privacy & Cybersecurity Law Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
人人都是产品经理
人人都是产品经理
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
月光博客
月光博客
博客园_首页
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AWS News Blog
AWS News Blog
WordPress大学
WordPress大学
AI
AI
酷 壳 – CoolShell
酷 壳 – CoolShell
Hacker News: Ask HN
Hacker News: Ask HN
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
P
Proofpoint News Feed
The Hacker News
The Hacker News
The Cloudflare Blog
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cloudbric
Cloudbric
C
Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
Jina AI
Jina AI
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
B
Blog

博客园 - 萍踪侠影

c#过滤HTML代码 - 萍踪侠影 - 博客园 也來開發一款日歷控件 制作activeX控件 在aspx頁中獲取DataGrid行序號 - 萍踪侠影 - 博客园 在MasterPage中檢驗User是否登入 - 萍踪侠影 - 博客园 [轉載]Run As Administrator Internet Explorer 7中的层叠样式表兼容性 Microsoft Office Web 组件 PivotTable 基础 ASP讀取excel文件 - 萍踪侠影 - 博客园 一個Ajax.NET的查詢實例 js校验的通用工具 將字串轉換為日期型數據的例子 - 萍踪侠影 - 博客园 將datagrid控件內容輸出到excel文件 - 萍踪侠影 - 博客园 C#常用函数集 IPv6 简介 初探AJAX.NET Visual C# 3.0 新特性概览 遍历XML引擎版本以适应代码 微调您的 Web 站点以适应 Windows XP Service Pack 2
以編程方式為GridView添加欄位 - 萍踪侠影 - 博客园
萍踪侠影 · 2006-12-19 · via 博客园 - 萍踪侠影

方法一:
1。加一個gridview:
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" datasourceid="SqlDataSource1" OnRowCreated="GridView1_RowCreated"><Columns>
<asp:BoundField ReadOnly="True" DataField="ID" InsertVisible="False" SortExpression="ID" HeaderText="ID"></asp:BoundField>
</Columns>
</asp:gridview>

2。兩個事件寫法:

    protected void Button1_Click1(object sender, EventArgs e)
    {
         //添加一個欄位;
        TemplateField tf = new TemplateField();
        tf.HeaderText = "TEST";
        GridView1.Columns.Add(tf);
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        ListItemType lit = (ListItemType)e.Row.RowType;
        if (lit == ListItemType.AlternatingItem || lit == ListItemType.Item)
        {
           //將TextBox加入到新欄位中;
            int c = e.Row.Cells.Count;
            Literal lt = new Literal();
            lt.Text = "<input type=text>";
            TextBox tb = new TextBox();
            tb.ID = "tb";
            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);       
    }