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?