command of dynamically created element's cleans all generated datahttp://forums.asp.net/t/1787751.aspx/1?command+of+dynamically+created+element+s+cleans+all+generated+dataMon, 02 Apr 2012 02:52:55 -040017877514910025http://forums.asp.net/p/1787751/4910025.aspx/1?command+of+dynamically+created+element+s+cleans+all+generated+datacommand of dynamically created element's cleans all generated data <p>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 &nbsp;all the data from table&nbsp;are wiped out. Where is the problem?</p> <pre class="prettyprint">protected void Button1_Click(object sender, EventArgs e) { string strSQL = &quot;SELECT * FROM Naudotojai&quot;; 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 = &quot;~/Images/edit.png&quot;; imgBt.Attributes[&quot;class&quot;] = &quot;edit&quot;; imgBt.ID = &quot;Edit&quot; &#43; index; imgBt.Command &#43;= new CommandEventHandler(test); cell.Controls.Add(imgBt); tRow.Cells.Add(cell); cell = new TableCell(); box = new TextBox(); box.Enabled = false; box.Text = reader[&quot;tabelio_nr&quot;].ToString(); box.ID = &quot;Tab&quot; &#43; index; cell.Controls.Add(box); tRow.Cells.Add(cell); TableDestytojai.Rows.Add(tRow); index&#43;&#43;; #endregion } UpdatePanelDestytojai.Update(); } void test(object sender, EventArgs e) { string id = ((ImageButton)sender).ID.Substring(4); TextBox tb = (TextBox)TableDestytojai.FindControl(&quot;Tab&quot;&#43;id); tb.Enabled = true; tb.Attributes[&quot;class&quot;] = &quot;editFieldEnabled&quot;; UpdatePanelDestytojai.Update(); }</pre> <p>As you can see whole table is inside UpdatePanel.</p> <p>Thanks in advance.</p> 2012-04-01T00:32:20-04:004910440http://forums.asp.net/p/1787751/4910440.aspx/1?Re+command+of+dynamically+created+element+s+cleans+all+generated+dataRe: command of dynamically created element's cleans all generated data <p>Dynamically created controls must be re-created upon every postback, i.e.&nbsp;the buttons you are creating in the Button1_Click event is lost when you make another postback by clicking on one of them.</p> <p>When you are&nbsp;creating a dynamic control, you as a developer has the responsibility of re-creating the control upon each postback.</p> 2012-04-01T14:05:53-04:004910786http://forums.asp.net/p/1787751/4910786.aspx/1?Re+command+of+dynamically+created+element+s+cleans+all+generated+dataRe: command of dynamically created element's cleans all generated data <p>Yea, dynamic controls need to be recreated between postbacks. You can use various ways to &quot;recreate&quot; them to their previous state though such as persisting in viewstate, or global cache, then retrieving the persistance info and using it to &quot;restore&quot; 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.</p> 2012-04-02T02:52:55-04:00