How to use event dynamically created buttons to redirect to another ASP.NET page?

1

I'm trying to call the event from buttons that were created dynamically with a foreach

public void adicionarComanda()
{
    List<Comanda> lc = ControllerComanda.getComanda();

    foreach (Comanda comanda in lc)
    {
        Button bt = new Button();
        bt.Text = comanda.nome_Pessoa;
        bt.CssClass = "botoes";
        bt.Click += btnNome1_Click;
        bt.CommandArgument = comanda.nome_Pessoa;              

        HtmlGenericControl li = new HtmlGenericControl("li");
        li.Controls.Add(bt);
        ulBotoes.Controls.Add(li);
    }

}

And the event:

protected void btnNome1_Click(object sender, EventArgs e)
{
    string nomePessoa = (sender as Button).CommandArgument;
    Session["currentUser"] = nomePessoa.ToString();
    Response.Redirect("~/Mobile/Pages/produtosCategoria.aspx");
} 

How could I solve this problem and make the event work and I be redirected to the desired page? Many thanks to all!

    
asked by anonymous 26.05.2015 / 04:05

1 answer

1

Events are called after Page_Load, so the solution was in the event of adding the buttons put a Response.Redirect () to the same page in the event that adds the dynamic buttons.

protected void btnAdd_Click(object sender, ImageClickEventArgs e)
    {
        string nome = txtNome.Text.Trim();

        ControllerComanda.inserirComanda(nome);

        txtNome.Text = "";
        txtNome.Focus();

        Response.Redirect("~/Mobile/Pages/paginaComandas.aspx");
    }

Calling the same page after insertion will cause the page load to re-occur and create the buttons afterwards.

    
27.05.2015 / 06:59