Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

please consider this code:

<asp:TemplateField>
     <ItemTemplate>
         <asp:LinkButton runat="server" ID="linkmodel" Text='<%#Eval("MenuItem") %>' 
                         CommandName='<%#Eval("CommandName") %>'
                         OnCommand="linkmodel_Click" 
                         OnClientClick="return confirm('Are You Sure')">
         </asp:LinkButton>
     </ItemTemplate>
</asp:TemplateField>

How to add this column programmatically using C#?

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...