Button does not want to call a method (ASP.NET - C #)

First, I would like to thank you for taking the time to read my problem and possibly also help me. :)

Now to my problem ... I create new buttons with C # code and attach them to my panel with the name Panel1:

Button novgumb = new Button();
novgumb.Click += new EventHandler(ButtonOdstrani);
novgumb.CommandArgument = "2";
novgumb.Visible = true;
novgumb.Text = "Test";
Panel1.Controls.Add(novgumb);

A button created this way should call my ButtonOdstrani method:

    public void ButtonOdstrani(object sender, EventArgs e)
    {
        string asd = ((Button)sender).CommandArgument;
        //...
    }

Now, when I put the Button code in PageLoad, it works fine, but when I put it in my method for writing XML on the site, the buttons created in this way will not call the ButtonOdstrani function:

protected void IzpisXML()
    {
        BranjeXML();
        string[] element = ime.Split('$');
        //Button gumbs = new Button();
        //gumbs.Click += new EventHandler(ButtonOdstrani);
        //gumbs.Visible = true;
        //gumbs.Text = ("Odstrani ");
        //Panel1.Controls.Add(gumbs);
        for (; z < stevec/2; z++)
        {                    
            string imeGostilne = element[i];
            string naslov = element[i + 1];
            string telefon = element[i + 2];
            string spletnaStran = element[i + 3];
            string odpiralniOD = element[i + 4];
            string odpiralniDO = element[i + 5];
            string boni = element[i + 6];

            Label labela = new Label();

            labela.Text = "<b>Ime gostilne:</b> " + imeGostilne + "<br /><b>Naslov:</b> " + naslov + "<br><b>Telefon:</b> " + telefon + "<br><b>Spletna stran:</b> " + spletnaStran + "<br><b>Odpiralni cas:</b> " + odpiralniOD + " - " + odpiralniDO + "<br /><b>Študnetski boni:</b> " + boni + "<br />";
            labela.Enabled = true;
            labela.EnableTheming = true;
            labela.EnableViewState = true;
            labela.Visible = true;
            labela.ID = ("Label" + (z + 1));
            Panel1.Controls.Add(labela);

            Button novgumb = new Button();
            novgumb.Click += new EventHandler(ButtonOdstrani);
            novgumb.CommandArgument = "2";
            novgumb.Visible = true;
            novgumb.Text = "Test";
            Panel1.Controls.Add(novgumb);

            Label hr = new Label();
            hr.Text = "<hr />";
            hr.Visible = true;
            hr.Enabled = true;
            hr.EnableTheming = true;
            hr.EnableViewState = true;
            Panel1.Controls.Add(hr);
            i += 7;

        }
    }

Now pay attention to the commented button for the for clause - this does not work. Since "this did not work," I mean that he did not call the ButtonOdstrani function, it just updated the site (PostBack).

.


, :

private Button[] ButtonZaOdstranjevanje;

    protected override void OnInit(EventArgs e)
    {
        ButtonZaOdstranjevanje = new Button[stevec/2];
        base.OnInit(e);
        BranjeXML();

        for (int j=0; j < stevec/2; j++)
        {
            Button novgumb = new Button();
            novgumb.Click += new EventHandler(ButtonOdstrani);
            novgumb.Visible = true;
            novgumb.Text = "Odstrani gostilno";
            ButtonZaOdstranjevanje[j] = novgumb;
        }

    }

, : " " heppens :

ButtonZaOdstranjevanje[j] = novgumb;

, stevec 4, stevec/2 = 2, j 0. - , ?

+1
4

, .

.

+5

Ghosty,

. , . Page_Init, IsPostBack().

. ASP.net.

Mike

+1

ASP.NET

1- OnInit , OnPreInit

2- ,

, ASP.NET . 1 , / , 2 ..

+1

, , IzpisXML() OnInit Panel1 / .:)

, !:)

!

0

Source: https://habr.com/ru/post/1781462/


All Articles