I am now recreating the controls on page load but there are issues...
If I first explain what I am trying to do...
I have a data reader which I loop through and for each record create a panel and inside the panel a textbox control, next to each textbox control I have a button which should when the user clicks on it add another textbox control below the textbox control
in that panel... should be simple enough....
here is a simpliefied version of my code (just to save space)
while (reader.Read())
{
//Get some values from the data reader
Int32 exCount = 1;
ExtendableArray = this.textBox_Concat.Text;
string[] ExtendableArraySplit = ExtendableArray.Split(',');
p = new Panel();
p.CssClass = "QuestionsDiv";
// Pan = Parent Panel
Pan.Controls.Add(p);
t = new TextBox();
t.ID = "Textbox_Count_Question_" + Convert.ToString(i);
t.ClientIDMode = System.Web.UI.ClientIDMode.Static;
t.Text = "1";
p.Controls.Add(t);
// This increments on every button press
// You can watch this value incrementing when debugging
// I have hard coded 3 only for this example
exCount = 3;
for (Int32 iRows = 1; iRows <= exCount; iRows++)
{
t = new TextBox();
t.ClientIDMode = System.Web.UI.ClientIDMode.Static;
t.Width = 400;
t.ID = "Textbox_Question_" + Convert.ToString(i) + "_" + Convert.ToString(exCount);
p.Controls.Add(t);
}
b = new Button();
b.Text = "Add another...";
b.CommandArgument = ControlID;
b.CommandName =ControlID;
b.OnClientClick = "AddControl(" + (char)34 + "Textbox_Count_Question_" + Convert.ToString(i) + (char)34 + ")";
p.Controls.Add(b);
break;
i++;
}
The code runs through fine when debugging, it loops 3 times when creating the text boxes etc... but 3 text boxes do not show on screen. This all happens in an update panel - not sure that will have an effect
can you move that pan.Control.Add(p) to the end of the code...and see what happens then...I dont find any problem with your code apart from the thing that you added the child panel to parent well before the controls are created in child panel...
Danny Seager
Member
13 Points
36 Posts
Dynamic buttons click/command event
Feb 13, 2013 08:20 PM|LINK
I generally have worked in vb.net but now I'm doing some work in c#
I am creating a series of controls dynamically, 1 of those controls is a button. I want a method to fire on click of the button.
In VB I would use something like
I have tried the following
but non of these seem to fire.
I don't know if the issue is a post back issue because the controls are not created...?
Any help welcome.
MetalAsp.Net
All-Star
112071 Points
18240 Posts
Moderator
Re: Dynamic buttons click/command event
Feb 13, 2013 08:27 PM|LINK
Dynamically created controls have to be recreated on every postback and in page_load at the latest for the events to trigger properly.
Danny Seager
Member
13 Points
36 Posts
Re: Dynamic buttons click/command event
Feb 19, 2013 03:39 PM|LINK
thanks for that.
I am now recreating the controls on page load but there are issues...
If I first explain what I am trying to do...
I have a data reader which I loop through and for each record create a panel and inside the panel a textbox control, next to each textbox control I have a button which should when the user clicks on it add another textbox control below the textbox control in that panel... should be simple enough....
here is a simpliefied version of my code (just to save space)
while (reader.Read()) { //Get some values from the data reader Int32 exCount = 1; ExtendableArray = this.textBox_Concat.Text; string[] ExtendableArraySplit = ExtendableArray.Split(','); p = new Panel(); p.CssClass = "QuestionsDiv"; // Pan = Parent Panel Pan.Controls.Add(p); t = new TextBox(); t.ID = "Textbox_Count_Question_" + Convert.ToString(i); t.ClientIDMode = System.Web.UI.ClientIDMode.Static; t.Text = "1"; p.Controls.Add(t); // This increments on every button press // You can watch this value incrementing when debugging // I have hard coded 3 only for this example exCount = 3; for (Int32 iRows = 1; iRows <= exCount; iRows++) { t = new TextBox(); t.ClientIDMode = System.Web.UI.ClientIDMode.Static; t.Width = 400; t.ID = "Textbox_Question_" + Convert.ToString(i) + "_" + Convert.ToString(exCount); p.Controls.Add(t); } b = new Button(); b.Text = "Add another..."; b.CommandArgument = ControlID; b.CommandName =ControlID; b.OnClientClick = "AddControl(" + (char)34 + "Textbox_Count_Question_" + Convert.ToString(i) + (char)34 + ")"; p.Controls.Add(b); break; i++; }The code runs through fine when debugging, it loops 3 times when creating the text boxes etc... but 3 text boxes do not show on screen. This all happens in an update panel - not sure that will have an effect
Again any help would be appreciated.
ramiramilu
All-Star
95373 Points
14096 Posts
Re: Dynamic buttons click/command event
Feb 19, 2013 05:26 PM|LINK
can you move that pan.Control.Add(p) to the end of the code...and see what happens then...I dont find any problem with your code apart from the thing that you added the child panel to parent well before the controls are created in child panel...
thanks,
JumpStart
Danny Seager
Member
13 Points
36 Posts
Re: Dynamic buttons click/command event
Feb 20, 2013 08:10 AM|LINK
Thanks for the reply.
Changing this didn't seem to make any difference.
I'm at a loss... I can't understand what I'm missing.
Danny Seager
Member
13 Points
36 Posts
Re: Dynamic buttons click/command event
Feb 20, 2013 01:51 PM|LINK
I found a sollution... I had to create the controls first and then go back through them to assign their properties.
Not sure why I have to do this but it works. (if anyone knows why then I'd love to know)