gpt4 book ai didi

c# - 如何以编程方式添加 TemplateField

转载 作者:可可西里 更新时间:2023-11-01 08:05:53 26 4
gpt4 key购买 nike

请考虑这段代码:

<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>

如何使用 C# 以编程方式添加此列?

谢谢

最佳答案

这可能有助于开始:

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);
}
}

但是:

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

关于c# - 如何以编程方式添加 TemplateField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12581088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com