Problem with ClickEvent

H

Helmut

I have I think rather small Problem, if somebody out there could help
me please???

private void AddButton_Click(object sender, System.EventArgs e)
{
TextBox txt = new TextBox();
txt.Width = 200;
txt.Height = 25;
txt.ID = "txt" + Panel1.Controls.Count;
txt.Text = txt.ID;

Button aBtn = new Button();
aBtn.Width = 25;
aBtn.Height = 25;
aBtn.ID = "aBtn" + Panel1.Controls.Count;
aBtn.Text = "[+]";
aBtn.Click += new EventHandler(AddButton_Click);

Button rBtn = new Button();
rBtn.Width = 25;
rBtn.Height = 25;
rBtn.ID = "rBtn" + Panel1.Controls.Count;
rBtn.Text = "[-]";
rBtn.Click += new EventHandler(RemoveButton_Click);

Panel1.Controls.Add(txt);
Panel1.Controls.Add(aBtn);
Panel1.Controls.Add(rBtn);
}

But ny Problem is that
the newly added row does not recognize button click events. And i
will get crazy when i dont find the solution soon!!!!!!

10000 THANKs in Advanced
 
T

Tonix

Hi,

I doubt if those controls you dynamically add will be still there on next
postback.

My suggestion:

private void AddButton_Click(object sender, System.EventArgs e)
{
this.ViewState["ControlsAdded"] = true;
}

override protected void OnInit(EventArgs e)
{
if (true == this.ViewState["ControlsAdded"])
{
TextBox txt = new TextBox();
txt.Width = 200;
txt.Height = 25;
txt.ID = "txt" + Panel1.Controls.Count;
txt.Text = txt.ID;

Button aBtn = new Button();
aBtn.Width = 25;
aBtn.Height = 25;
aBtn.ID = "aBtn" + Panel1.Controls.Count;
aBtn.Text = "[+]";
aBtn.Click += new EventHandler(AddButton_Click);

Button rBtn = new Button();
rBtn.Width = 25;
rBtn.Height = 25;
rBtn.ID = "rBtn" + Panel1.Controls.Count;
rBtn.Text = "[-]";
rBtn.Click += new EventHandler(RemoveButton_Click);

Panel1.Controls.Add(txt);
Panel1.Controls.Add(aBtn);
Panel1.Controls.Add(rBtn);
}

//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

Hope it helps.
Tonix


Helmut said:
I have I think rather small Problem, if somebody out there could help
me please???

private void AddButton_Click(object sender, System.EventArgs e)
{
TextBox txt = new TextBox();
txt.Width = 200;
txt.Height = 25;
txt.ID = "txt" + Panel1.Controls.Count;
txt.Text = txt.ID;

Button aBtn = new Button();
aBtn.Width = 25;
aBtn.Height = 25;
aBtn.ID = "aBtn" + Panel1.Controls.Count;
aBtn.Text = "[+]";
aBtn.Click += new EventHandler(AddButton_Click);

Button rBtn = new Button();
rBtn.Width = 25;
rBtn.Height = 25;
rBtn.ID = "rBtn" + Panel1.Controls.Count;
rBtn.Text = "[-]";
rBtn.Click += new EventHandler(RemoveButton_Click);

Panel1.Controls.Add(txt);
Panel1.Controls.Add(aBtn);
Panel1.Controls.Add(rBtn);
}

But ny Problem is that
the newly added row does not recognize button click events. And i
will get crazy when i dont find the solution soon!!!!!!

10000 THANKs in Advanced
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top