How to add TemplateField programmatically

后端 未结 1 1100
离开以前
离开以前 2020-12-03 14:59

please consider this code:


     
         

        
1条回答
  •  感动是毒
    2020-12-03 15:06

    This might help to get started:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            var linkField = new TemplateField();
            linkField.ItemTemplate = new LinkColumn();
            GridView1.Columns.Add(linkField);
        }
    }
    
    
    class LinkColumn : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            LinkButton link = new LinkButton();
            link.ID = "linkmodel";
            container.Controls.Add(link);
        }
    }
    

    But:

    Although you can dynamically add fields to a data-bound control, it is strongly recommended that fields be statically declared and then shown or hidden, as appropriate. Statically declaring all your fields reduces the size of the view state for the parent data-bound control.

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.templatefield.aspx

    0 讨论(0)
提交回复
热议问题