1

first I would like to thank you for taking your time to read my problem and maybe also help me out. :)

Now to my issue... I'm creating new buttons with the C# code and attaching them into my Panel named Panel1:

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

The button created in this way, should call my method ButtonOdstrani:

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

Now when I put the Button code into PageLoad it works fine, but when I put it inside my method for writting the XML on the site, the buttons created in this way won't call the function ButtonOdstrani:

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 note the commented button outside the for sentence - it didn't work eather way. With "it didn't work" i mean, it didn't call the the function ButtonOdstrani, it just refreshed the site (PostBack).

I would really be thankful for any advice solution.


OK, now i've created the the following:

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;
        }

    }

But the problem is that i get an error: "Index was Outside the bounds of the array" this heppens at:

ButtonZaOdstranjevanje[j] = novgumb;

It's not logical since the stevec value is 4, so stevec/2 = 2, and j parameter is set to 0. Does anyone have any idea how to fix this error?

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Ghosty
  • 13
  • 2

4 Answers4

5

when creating dynamic controls you need to re-create on each postback.

see here

Richard Friend
  • 15,800
  • 1
  • 42
  • 60
1

Ghosty,

Dynamic controls are best created in the Init phase of the Page lifecycle. As Richard mentioned, they also need to be created on every post to the page. I recommend putting the code inside of the Page_Init event handler, making sure to put it outside of any existing IsPostBack() checks.

See here for more understanding on the ASP.net Page lifecycle.

Mike

mclark1129
  • 7,532
  • 5
  • 48
  • 84
1

The key points when creating a dynamic control in ASP.NET are

1- The controls should be created in OnInit or better yet an override OnPreInit

2- The controls should be created with the same ID each time they are created

Doing the above will ensure that all the necesary ASP.NET plumbing is taken care of. Point 1 will ensure that this happens on every page request/post back, point 2 ensures that the control events and view state etc. is correctly mapped.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
0

OK, it's working now, I've called the IzpisXML() Method in the OnInit section, and just changed the Panel1 visibility for showing/hiding the Labels & Buttons. :)

I would like to thank you all once again, you were all a great help! :)

Thank You!

Ghosty
  • 13
  • 2