且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在运行时添加Controlls到GridView?

更新时间:2023-09-18 23:37:22

最简单的是一个占位符控件添加到ItemTemplate中,作为ItemTemplate中没有ID。

Easiest is to add a placeholder control to the ItemTemplate, as ItemTemplate has no ID.

<asp:TemplateField>
    <ItemTemplate>
        <asp:PlaceHolder ID="emails" runat="server"></asp:PlaceHolder>
    </ItemTemplate>
</asp:TemplateField> 

,然后在RowDataBound事件

and then in RowDataBound event

if (e.Row.RowType == DataControlRowType.DataRow)
{
    PlaceHolder emails = e.Row.FindControl("emails") as PlaceHolder;

    if (emails != null)
    {
        LinkButton lbEmail = new LinkButton();
        lbEmail.Text = "your text";
        lbEmail.Click += new EventHandler(SendEmail);

        emails.Controls.Add(lbEmail);
    }
}

当然,例如被简化。您可以轻松地将其扩展到您的需求。

Of course, the example is simplified. You can easily extend it to your needs.