动态地asp.net 4.5 C#中添加一个按钮单击事件 [英] add a button click event dynamically in asp.net 4.5 c#

查看:136
本文介绍了动态地asp.net 4.5 C#中添加一个按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,这篇文章[1]:<一href=\"http://stackoverflow.com/questions/6187944/how-can-i-create-dynamic-button-click-event-on-dynamic-button\">How我可以在动态按钮创建动态按钮单击事件

该解决方案不是为我工作,我创建了一个动态按钮,这是一个asp里面:表控制器

我也尝试保存在会话我的动态元素,和会话值分配给在Page_Load的对象,但是这是行不通的。

一些想法

修改

  ...
        Button按钮=新按钮();
        button.ID =BtnTag;
        button.Text =标签generieren
        button.Click + = button_TagGenerieren;        tabellenZelle.Controls.Add(按钮);
        会话[表] =表;
    }    公共无效button_TagGenerieren(对象发件人,EventArgs的发送)
    {
        TableRowCollection tabellenZeilen = qvTabelle.Rows;
        的for(int i = 0; I&LT; tabellenZeilen.Count;我++)
        {
            ...
        }
    }  保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!Page.IsPostBack)
        {            如果(会话[表]!= NULL)
            {
                表=(表)会议[表];
                会话[表] = NULL;
             }
        }
    }


解决方案

这不是存储每一个控制成会话状态一个很好的做法。

唯一的问题,我发现是你需要重新装入相同的id的控件,当页面回发到服务器。否则,这些控件将是空的。

 &LT; ASP:占位符=服务器ID =PLACEHOLDER1/&GT;
&LT; ASP:标签=服务器ID =Label1的/&GT;保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    LoadControls();
}私人无效LoadControls()
{
    VAR按钮=新的Button {ID =BtnTag,文本=标签generieren};
    button.Click + = button_Click;
    PlaceHolder1.Controls.Add(按钮);
}私人无效button_Click(对象发件人,EventArgs的发送)
{
    Label1.Text =BtnTag按钮被点击
}

请注意:如果你不知道该按钮的ID(这是在运行时动态生成的),要保存在的ViewState这些ID 这样的 - 的 http://stackoverflow.com/a/14449305/296861

I have some questions to this post [1]: How can i create dynamic button click event on dynamic button

The solution is not working for me, I created dynamically an Button, which is inside in an asp:table controller.

I have try to save my dynamic elements in an Session, and allocate the Session value to the object in the Page_Load, but this is not working.

Some ideas

edit:

        ...
        Button button = new Button();
        button.ID = "BtnTag";
        button.Text = "Tag generieren";
        button.Click += button_TagGenerieren;

        tabellenZelle.Controls.Add(button);
        Session["table"] = table;
    }

    public void button_TagGenerieren(object sender, EventArgs e)
    {
        TableRowCollection tabellenZeilen = qvTabelle.Rows;
        for (int i = 0; i < tabellenZeilen.Count; i++)
        {
            ...
        }
    }

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (Session["table"] != null)
            {
                table = (Table) Session["table"];
                Session["table"] = null;
             }
        }
    }

解决方案

It is not a good practice to store every control into Session state.

Only problem I found is you need to reload the controls with same Id, when the page is posted back to server. Otherwise, those controls will be null.

<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
<asp:Label runat="server" ID="Label1"/>

protected void Page_Load(object sender, EventArgs e)
{
    LoadControls();
}

private void LoadControls()
{
    var button = new Button {ID = "BtnTag", Text = "Tag generieren"};
    button.Click += button_Click;
    PlaceHolder1.Controls.Add(button);
}

private void button_Click(object sender, EventArgs e)
{
    Label1.Text = "BtnTag button is clicked";
}

Note: If you do not know the button's id (which is generated dynamically at run time), you want to save those ids in ViewState like this - http://stackoverflow.com/a/14449305/296861

这篇关于动态地asp.net 4.5 C#中添加一个按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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