0

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;
             }
        }
    }
5
  • why do you create it dynamically to begin with? Commented May 15, 2013 at 13:42
  • I have a asp:panel which is show in the detail of an "dataset", and the fill the data dynamically with an event Commented May 15, 2013 at 13:44
  • I don't think you need to use the Session at all... if you change the control structure of the page, it will persist automatically. Commented May 15, 2013 at 13:45
  • the funny point is, the Session allocate the right data, but this is not shown on the surface Commented May 15, 2013 at 13:46
  • stackoverflow.com/q/10387856/875454 This might help others out too Commented Jan 9, 2015 at 22:19

3 Answers 3

1

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 - https://stackoverflow.com/a/14449305/296861

Sign up to request clarification or add additional context in comments.

Comments

0

The problem lies in the moment at which te button and it's event are created in the pagelifecycle. Try the page_init event for this.

Comments

0

Create Button in page load

            Button btn = new Button();
            btn.Text = "Dynamic";
            btn.Click += new EventHandler(btnClick);
            PlaceHolder1.Controls.Add(btn)

Button Click Event

protected void btnClick(object sender, EventArgs e)
{
  // Coding to click event
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.