Add button dynamically to an UpdatePanel ASPX.NET on Page load

时间:2018-12-03 12:53:19

标签: c# asp.net

That's my ASPX code:

<asp:UpdatePanel ID="buttons" runat="server" UpdateMode="Conditional">
    <ContentTemplate runat="server">
    </ContentTemplate>
</asp:UpdatePanel>

And in code behind:

protected void Page_Load(object sender, EventArgs e)
{
    var lines = ReadMyFile('C:\file.txt');
    foreach (var line in lines)
    {
        LinkButton linkButton = new LinkButton();
        linkButton.Text = line;
        buttons.Controls.Add(linkButton);
    }
}

That's what I'm trying but doesn't work. I have seen that buttons variable is null and that's the reason of the error in the last line of foreach code (because I get an error):

Reference to an object not established as an instance of an object.

0 个答案:

没有答案