dynamically created list of link buttons, link buttons not posting back

ぐ巨炮叔叔 提交于 2019-11-30 15:21:51

Code must run on each postback:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        int listItemIds = 1;

        for (int i = 0; i < 10; i++)
        {
            var li = new HtmlGenericControl("li");
            var lnk = new LinkButton();

            lnk.ID = "lnk" + listItemIds;
            lnk.Text = "text" + i;
            lnk.Click += Clicked;
            //lnk.Command += new CommandEventHandler(lnkColourAlternative_Click);
            //lnk.Click 
            li.Controls.Add(lnk);
            ul1.Controls.Add(li);
            listItemIds++;
        }
    }

    private void Clicked(object sender, EventArgs e)
    {
        var btn = sender as LinkButton;
        btn.Text = "Clicked";
    }

Do this sort of thing OnInit and make sure you recreate the controls on every postback.

See this KB article for an example - a bit outdated but the methodology is still the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!