动态创建的 LinkBut​​tons 的 OnClick 事件不起作用 [英] OnClick event of dynamically created LinkButtons is not working

查看:23
本文介绍了动态创建的 LinkBut​​tons 的 OnClick 事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了多种解决方案来解决这个问题,但都没有奏效.基本上,我有一张员工表,用户可以选择通过更新面板动态添加员工.每个员工都被添加为 LinkBut​​ton,此按钮将通过 ajaxToolkit:modalpopupextender 窗口>OnClick 事件,此窗口将显示员工详细信息.问题是当我点击员工姓名时,弹出窗口会显示细节不会.

I’ve tried several solutions for this problem but none of them worked. Basically, I have a table of employees and the user have the choice of adding an employee dynamically thru an update panel. Each employee is being added as LinkButton and this button will fire ajaxToolkit:modalpopupextender window through OnClick event, and this window will show the employee details. The problem is when I click on the employee name the popup window will show up BUT the details wont.

这是我创建按钮并将其放入表格的代码:

Here is the code in which I’m creating the buttons and putting it in the table:

LinkButton lbtn = new LinkButton();
                    lbtn.ID = employee_arry[i] + "_lbtn" + i;
                    lbtn.Text = employee_arry[i];
                    lbtn.Click += new EventHandler(this.employee_info);
                    lbtn.CausesValidation = false;
                    lbtn.Attributes.Add("runat", "server");
                    cell.Controls.Add(lbtn);

这里是employee_info方法:

and here is the employee_info method:

//the info will be pulled from the database…
public void employee_info(object sender, EventArgs e)
    { 
        name.Text = "employee name";
        dept.Text = "employee department";
        jobt.Text = "employee job title";
        email.Text = "employee email";
        tel.Text = "employee telephone";
        ModalPopupExtender1.Show();
    }

推荐答案

查看这个答案

https://stackoverflow.com/a/11127064/1268570

这解释了动态控件的行为

This explains the behavior of dynamic controls

你需要考虑:

  • 当您不使用母版页时,应在 PreInit 事件中创建动态控件,如果是,则在 Init 事件中创建控件
  • 避免在这些事件中设置可以在每个帖子中更改的属性,因为当应用视图状态时(在帖子事件中),这些属性将被覆盖
  • 每次发布​​页面时都必须创建动态控件,避免这种情况 if(!this.IsPostBack) this.CreatemyDynamicControls();
  • 当您在 PreInit 或 Init 事件中创建控件时,它们的状态将在 post 事件中自动设置,这意味着在 LoadComplete 事件中,即使您在每个 post 中再次创建控件,您的控件也会包含它们的状态,甚至当您没有明确设置它们的状态时.请注意,当您处理在设计时创建的控件时,此行为是不同的,在这种情况下,设置状态的事件是 Load 事件
  • 事件订阅应该在 PageLoadComplete 之前发生,否则它们将不会被引发

如果您还没有找到解决方案,这是一种方法(完整的工作示例):

In case you have not found a solution, this is a way to do it (full working example):

    <asp:ScriptManager runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" ViewStateMode="Enabled">
        <ContentTemplate>
            <asp:Panel runat="server" ID="myPanel">
            </asp:Panel><br />
            <asp:Button ID="Button1" Text="add control" runat="server" OnClick="addControl_Click" /><br />
            <asp:Label ID="lblMessage" runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>

代码隐藏

    protected int NumberOfControls
    {
        get
        {
            if (ViewState["c"] == null)
            {
                return 0;
            }

            return int.Parse(ViewState["c"].ToString());
        }
        set
        {
            ViewState["c"] = value;
        }
    }

    protected void addControl_Click(object sender, EventArgs e)
    {
        this.NumberOfControls++;
        this.myPanel.Controls.Add(new Literal { Text = "<br />" });
        this.myPanel.Controls.Add(this.CreateLinkButton(this.NumberOfControls));
    }

    protected void Page_PreLoad(object sender, EventArgs e)
    {
        this.CreateDynamicLinkButtons();
    }

    private void CreateDynamicLinkButtons()
    {
        for (int i = 0; i < this.NumberOfControls; i++)
        {
            this.myPanel.Controls.Add(new Literal { Text = "<br />" });
            this.myPanel.Controls.Add(this.CreateLinkButton(i + 1));
        }
    }

    private LinkButton CreateLinkButton(int index)
    {
        var l = new LinkButton { Text = "MyLink" + index.ToString(), ID = "myLinkID" + index.ToString() };
        l.Click += (x, y) =>
        {
            this.lblMessage.Text += "<br/>ID: " + (x as LinkButton).ID;
        };

        return l;
    }

输出

这篇关于动态创建的 LinkBut​​tons 的 OnClick 事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆