动态创建的链接按钮列表,链接按钮不回发

dynamically created list of link buttons, link buttons not posting back(动态创建的链接按钮列表,链接按钮不回发)
本文介绍了动态创建的链接按钮列表,链接按钮不回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在ul li"列表中动态创建链接按钮.然后我试图将每个链接按钮绑定到一个单击事件,在该事件中我为单击的链接按钮的文本设置一个标签.但是应该触发的事件没有被触发?

Hi I am dynamically creating link buttons in a 'ul li' list. I am then trying to tie each link button to a click event where i set a label to the text of the link button clicked. however the event that should fire doesnt get fired?

    if (!Page.IsPostBack)
        {
int listItemIds = 0;
       foreach (Node productcolour in product.Children)
       {
           HtmlGenericControl li = new HtmlGenericControl("li");
           LinkButton lnk = new LinkButton();
           lnk.ID = "lnk" + listItemIds;
           lnk.Text = productcolour.Name;
           lnk.Click += new EventHandler(Clicked);
           //lnk.Command += new CommandEventHandler(lnkColourAlternative_Click);
           //lnk.Click 
           li.Controls.Add(lnk);
           ul1.Controls.Add(li);
           listItemIds++;
       }
}

上面的内容包含在 if(!page.ispostback) 中,并且标签文本永远不会在其他任何地方设置.活动结束

the above is wrapped within a if(!page.ispostback) and the label text is never set anywhere else. heres to the event

protected void Clicked(object sender, EventArgs e)
{
    LinkButton lno = sender as LinkButton;
    litSelectedColour.Text = lno.Text;

}

推荐答案

代码必须在每次回发时运行:

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

这篇关于动态创建的链接按钮列表,链接按钮不回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)