Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

dynamicly created buttons

$
0
0

i need to generate buttons dinamicly in code behind, when i press "add more " button  it all works fine but when i press one of  buttons that was generated program adds one more button and i cant understand why that hapening

 public static class MyClass
    {
        public static bool clicado;
        public static int valor { get; set; }
        public static int valor2 { get; set; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MyClass.valor = 0;
            MyClass.clicado = true;
        }

        if (MyClass.clicado == true && Page.IsPostBack)
        {
            MyClass.valor++;
            MyClass.clicado = false;
        }
        //else if (MyClass.valor == 0)
        //{
        //    MyClass.valor = 1;
        //}

        //if (MyClass.clicado == true)
        //{
        //    for (int i = 0; i > MyClass.valor; i++)
        //    {
        //    }
        //}
        //MyClass.clicado = false;
        CreateButtons();
        MyClass.valor2 = 0;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        MyClass.clicado = true;
         //MyClass.valor++;
    }
    private void CreateButtons()
    {
        for (int i = 0; i < MyClass.valor;i++ )
        {
            Button button = new Button();
            button.Text = "ola";
            button.ID = "ola_" + MyClass.valor2++;
            button.Click += new EventHandler(ShowMessage);
            button.Attributes.Add("runat", "server");
            resultHolder.Controls.Add(button);
        }

    }
  
    void ShowMessage(Object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('isto é um teste');", true);
    }


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>