I have one button to fill table with data and edit buttons. Edit buttons are created dinamically to each row. when button is clicked it should enable edit fields on that row. But instead of it all the data from table are wiped out. Where is the problem?
protected void Button1_Click(object sender, EventArgs e)
{
string strSQL = "SELECT * FROM Naudotojai";
SqlConnection connection = new SqlConnection(strConnection);
SqlCommand command = new SqlCommand(strSQL, connection);
SqlDataReader reader;
connection.Open();
reader = command.ExecuteReader();
TableRow tRow = new TableRow();
TableCell cell = new TableCell();
TextBox box = new TextBox();
ImageButton imgBt = new ImageButton();
int index = 0;
while (reader.Read())
{
#region Fill table
tRow = new TableRow();
cell = new TableCell();
imgBt = new ImageButton();
imgBt.ImageUrl = "~/Images/edit.png";
imgBt.Attributes["class"] = "edit";
imgBt.ID = "Edit" + index;
imgBt.Command += new CommandEventHandler(test);
cell.Controls.Add(imgBt);
tRow.Cells.Add(cell);
cell = new TableCell();
box = new TextBox();
box.Enabled = false;
box.Text = reader["tabelio_nr"].ToString();
box.ID = "Tab" + index;
cell.Controls.Add(box);
tRow.Cells.Add(cell);
TableDestytojai.Rows.Add(tRow);
index++;
#endregion
}
UpdatePanelDestytojai.Update();
}
void test(object sender, EventArgs e)
{
string id = ((ImageButton)sender).ID.Substring(4);
TextBox tb = (TextBox)TableDestytojai.FindControl("Tab"+id);
tb.Enabled = true;
tb.Attributes["class"] = "editFieldEnabled";
UpdatePanelDestytojai.Update();
}
Dynamically created controls must be re-created upon every postback, i.e. the buttons you are creating in the Button1_Click event is lost when you make another postback by clicking on one of them.
When you are creating a dynamic control, you as a developer has the responsibility of re-creating the control upon each postback.
Yea, dynamic controls need to be recreated between postbacks. You can use various ways to "recreate" them to their previous state though such as persisting in viewstate, or global cache, then retrieving the persistance info and using it to "restore" the
recreated controls to their previous state. might be a bit of a pain in the butt though for ones nested in a gridview, dunno.
elementsdynamicallyPostback
When the going get's tough, the tough outsource and take a vacation... lol I wish :(
adminkil
Member
20 Points
21 Posts
command of dynamically created element's cleans all generated data
Apr 01, 2012 12:32 AM|LINK
I have one button to fill table with data and edit buttons. Edit buttons are created dinamically to each row. when button is clicked it should enable edit fields on that row. But instead of it all the data from table are wiped out. Where is the problem?
protected void Button1_Click(object sender, EventArgs e) { string strSQL = "SELECT * FROM Naudotojai"; SqlConnection connection = new SqlConnection(strConnection); SqlCommand command = new SqlCommand(strSQL, connection); SqlDataReader reader; connection.Open(); reader = command.ExecuteReader(); TableRow tRow = new TableRow(); TableCell cell = new TableCell(); TextBox box = new TextBox(); ImageButton imgBt = new ImageButton(); int index = 0; while (reader.Read()) { #region Fill table tRow = new TableRow(); cell = new TableCell(); imgBt = new ImageButton(); imgBt.ImageUrl = "~/Images/edit.png"; imgBt.Attributes["class"] = "edit"; imgBt.ID = "Edit" + index; imgBt.Command += new CommandEventHandler(test); cell.Controls.Add(imgBt); tRow.Cells.Add(cell); cell = new TableCell(); box = new TextBox(); box.Enabled = false; box.Text = reader["tabelio_nr"].ToString(); box.ID = "Tab" + index; cell.Controls.Add(box); tRow.Cells.Add(cell); TableDestytojai.Rows.Add(tRow); index++; #endregion } UpdatePanelDestytojai.Update(); } void test(object sender, EventArgs e) { string id = ((ImageButton)sender).ID.Substring(4); TextBox tb = (TextBox)TableDestytojai.FindControl("Tab"+id); tb.Enabled = true; tb.Attributes["class"] = "editFieldEnabled"; UpdatePanelDestytojai.Update(); }As you can see whole table is inside UpdatePanel.
Thanks in advance.
elements dynamically Postback
mm10
Contributor
6395 Points
1182 Posts
Re: command of dynamically created element's cleans all generated data
Apr 01, 2012 02:05 PM|LINK
Dynamically created controls must be re-created upon every postback, i.e. the buttons you are creating in the Button1_Click event is lost when you make another postback by clicking on one of them.
When you are creating a dynamic control, you as a developer has the responsibility of re-creating the control upon each postback.
elements dynamically Postback
magicmike201...
Contributor
2021 Points
481 Posts
Re: command of dynamically created element's cleans all generated data
Apr 02, 2012 02:52 AM|LINK
Yea, dynamic controls need to be recreated between postbacks. You can use various ways to "recreate" them to their previous state though such as persisting in viewstate, or global cache, then retrieving the persistance info and using it to "restore" the recreated controls to their previous state. might be a bit of a pain in the butt though for ones nested in a gridview, dunno.
elements dynamically Postback