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

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

问题描述

我试了几个方案来解决这个问题,但没有一次成功。 基本上,我有员工的表与用户动态地增加员工直通更新面板的选择。每一位员工都被添加为 的LinkBut​​ton ,这个按钮会火的 ajaxToolkit:modalpopupextender 窗口,通过 的OnClick 事件,该窗口将显示雇员的详细信息。 问题是,当我点击员工姓名的弹出窗口将显示的可是细节的习惯。

下面是code中,我创建的按钮,并把它放在桌上:

 的LinkBut​​ton lbtn =新的LinkBut​​ton();
                    lbtn.ID = employee_arry [我] +_lbtn+我;
                    lbtn.Text = employee_arry [I]
                    lbtn.Click + =新的EventHandler(this.employee_info);
                    lbtn.CausesValidation = FALSE;
                    lbtn.Attributes.Add(RUNAT,服务器);
                    cell.Controls.Add(lbtn);
 

和这里是employee_info方式:

  //该信息将被从数据库中抽取...
公共无效employee_info(对象发件人,EventArgs的)
    {
        name.Text =员工姓名;
        dept.Text =员工部;
        jobt.Text =员工职位;
        email.Text =员工的电子邮件;
        tel.Text =员工的电话;
        ModalPopupExtender1.Show();
    }
 

解决方案

检查这个答案

<一个href="http://stackoverflow.com/a/11127064/1268570">http://stackoverflow.com/a/11127064/1268570

这说明了动态控件的行为

您需要考虑的:

  • 动态控制应在preINIT事件被创建,当你不使用的是母版页,如果你是,那么在Init事件创建控件
  • 避免设置,可以在这些事件中每一个职位来改变属性,因为当视图状态的应用(在后事件)的属性将被覆盖
  • 如果
  • 动态控制必须创建每次页面发布时间,避免这种this.CreatemyDynamicControls()(this.IsPostBack!);
  • 当您在preINIT或init事件创建控件,它们的状态会自动在后事件,这意味着在LoadComplete事件的控件将包含自己的状态恢复,甚至当你创建它们又在每个集后期甚至当你没有明确设定自己的状态。 注意此行为是不同的,当你正在处理在设计时创建的控件,在这种情况下,在国家已设置的事件是Load事件
  • 在事件订阅应该在PageLoadComplete之前出现,否则将不会引发

编辑1

如果你还没有找到一个解决方案,这是一个办法做到这一点(全工作的例子):

ASPX

 &LT; ASP:的ScriptManager =服务器/&GT;
    &LT; ASP:UpdatePanel的ID =UpdatePanel1=服务器ViewStateMode =已启用&GT;
        &LT;的ContentTemplate&GT;
            &LT; ASP:面板=服务器ID =myPanel&GT;
            &LT; / ASP:面板&gt;&LT; BR /&GT;
            &LT; ASP:按钮的ID =Button1的文本=添加控制=服务器的OnClick =addControl_Click/&GT;&LT; BR /&GT;
            &LT; ASP:标签ID =lblMessage=服务器/&GT;
        &LT; /的ContentTemplate&GT;
    &LT; / ASP:UpdatePanel的&GT;
 

code背后

 保护INT NumberOfControls
    {
        得到
        {
            如果(的ViewState [C] == NULL)
            {
                返回0;
            }

            返回int.Parse(的ViewState [C]的ToString());
        }
        组
        {
            的ViewState [C] =值;
        }
    }

    保护无效addControl_Click(对象发件人,EventArgs的)
    {
        this.NumberOfControls ++;
        this.myPanel.Controls.Add(新文字的{text =&LT; BR /&gt;中});
        this.myPanel.Controls.Add(this.CreateLinkBut​​ton(this.NumberOfControls));
    }

    保护无效PAGE_ preLOAD(对象发件人,EventArgs的)
    {
        this.CreateDynamicLinkBut​​tons();
    }

    私人无效CreateDynamicLinkBut​​tons()
    {
        的for(int i = 0; I&LT; this.NumberOfControls;我++)
        {
            this.myPanel.Controls.Add(新文字的{text =&LT; BR /&gt;中});
            this.myPanel.Controls.Add(this.CreateLinkBut​​ton第(i + 1));
        }
    }

    私人LinkBut​​ton的CreateLinkBut​​ton(INT指数)
    {
        变种L =新的LinkBut​​ton的{text =myLink的+ index.ToString(),ID =myLinkID+ index.ToString()};
        l.Click + =(X,Y)=&GT;
        {
            this.lblMessage.Text + =&LT; BR /&GT; ID:+(X为LinkBut​​ton的).ID;
        };

        返回L;
    }
 

输出

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);

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();
    }

解决方案

Check this answer

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

This explains the behavior of dynamic controls

You need to consider:

  • Dynamic controls should be created in the PreInit event when you are not working with a master page, if you are, then create the controls in the Init event
  • Avoid setting properties that can be changed in each post in these events because when the view state is applied (in a post event) the properties will be overridden
  • Dynamic controls must be created every time the page is posted, avoid this if(!this.IsPostBack) this.CreatemyDynamicControls();
  • When you create the controls in the PreInit or Init events, their states will be automatically set in a post event, which means in the LoadComplete event your controls will contain their state back even when you create them again in each post and even when you did not explicitly set their state. Note this behavior is different when you are dealing with controls created at design time, in that case, the event where the state has been set is the Load event
  • Event subscription should occur before the PageLoadComplete or they will not be raised

Edit 1

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

ASPX

    <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>

Code Behind

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

Output

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

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