LadyPhoenix
New Member
Alright so i got the code from someone before and adapted it to my own liking. However, i may be misinterpreting their code, but on the 3rd click of the button the table gets all out of wack. I'm making a dynamic textbox with table and saving it to session after every click of the "Click here to add an accident". 2nd Click:
3rd Click:
I've tried debugging it and it just doesn't make sense to why it would do this. My code is correct I believe, I'm not sure what is going wrong.\[code\]public partial class employment_driversapplication_History : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}protected int TotalNumberAdded{ get { return (int)(ViewState["TotalNumberAdded"] ?? 0); } set { ViewState["TotalNumberAdded"] = value; }}protected void AccidentButton_Click(object sender, EventArgs e){ TotalNumberAdded++; // AddControls(TotalNumberAdded); BuildControls();}protected void PrevPage_Click(object sender, EventArgs e){ Response.Redirect("employment_driversapplication_personalinfo.aspx");}private IList<TextBox> AddedControls = new List<TextBox>();private IList<Label> AddedControlsLabel = new List<Label>();protected override void CreateChildControls(){ BuildControls(); base.CreateChildControls();}private void BuildControls(){ AccidentPlaceHolder.Controls.Add(new LiteralControl("<table><tr>")); AccidentPlaceHolder.Controls.Add(new LiteralControl("<br />")); for (var Nl = 0; Nl < TotalNumberAdded; Nl++) { var idNL = String.Format("NatureLabel{0}", Nl); //Check if control was already added //only create controls that are new for this postback if (AccidentPlaceHolder.FindControl(idNL) == null) { var NLabel = new Label() { ID = idNL }; NLabel.Text = "Nature Of Accident: "; AccidentPlaceHolder.Controls.Add(new LiteralControl("<td class='title-text'>")); AccidentPlaceHolder.Controls.Add(NLabel); AddedControlsLabel.Add(NLabel); } } for (var x = 0; x < TotalNumberAdded; x++) { var idN = String.Format("NatureTextBox{0}", x); //Check if control was already added //only create controls that are new for this postback if (AccidentPlaceHolder.FindControl(idN) == null) { var NtextBox = new TextBox() { ID = idN }; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>")); AccidentPlaceHolder.Controls.Add(NtextBox); AddedControls.Add(NtextBox); } } for (var DL = 0; DL < TotalNumberAdded; DL++) { var idDL = String.Format("DateLabel{0}", DL); //Check if control was already added //only create controls that are new for this postback if (AccidentPlaceHolder.FindControl(idDL) == null) { var DLabel = new Label() { ID = idDL }; DLabel.Text = "Date: "; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'>")); AccidentPlaceHolder.Controls.Add(DLabel); AddedControlsLabel.Add(DLabel); } } for (var d = 0; d < TotalNumberAdded; d++) { var idD = String.Format("DateTextBox{0}", d); if (AccidentPlaceHolder.FindControl(idD) == null) { var DtextBox = new TextBox() { ID = idD }; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>")); AccidentPlaceHolder.Controls.Add(DtextBox); AddedControls.Add(DtextBox); AccidentPlaceHolder.Controls.Add(new LiteralControl("</td></tr>")); } } for (var FL = 0; FL < TotalNumberAdded; FL++) { var idFL = String.Format("FatalLabel{0}", FL); //Check if control was already added //only create controls that are new for this postback if (AccidentPlaceHolder.FindControl(idFL) == null) { var FLabel = new Label() { ID = idFL }; FLabel.Text = "Fatalities: "; AccidentPlaceHolder.Controls.Add(new LiteralControl("<tr><td class='title-text'>")); AccidentPlaceHolder.Controls.Add(FLabel); AddedControlsLabel.Add(FLabel); } } for (var f = 0; f < TotalNumberAdded; f++) { var idF = String.Format("FatalTextBox{0}", f); if (AccidentPlaceHolder.FindControl(idF) == null) { var FtextBox = new TextBox() { ID = idF }; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>")); AccidentPlaceHolder.Controls.Add(FtextBox); AddedControls.Add(FtextBox); } } for (var IL = 0; IL < TotalNumberAdded; IL++) { var idIL = String.Format("InjuryLabel{0}", IL); //Check if control was already added //only create controls that are new for this postback if (AccidentPlaceHolder.FindControl(idIL) == null) { var ILabel = new Label() { ID = idIL }; ILabel.Text = "Injuries: "; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'>")); AccidentPlaceHolder.Controls.Add(ILabel); AddedControlsLabel.Add(ILabel); } } for (var i = 0; i < TotalNumberAdded; i++) { var idI = String.Format("InjuryTextBox{0}", i); if (AccidentPlaceHolder.FindControl(idI) == null) { var ItextBox = new TextBox() { ID = idI }; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>")); AccidentPlaceHolder.Controls.Add(ItextBox); AddedControls.Add(ItextBox); AccidentPlaceHolder.Controls.Add(new LiteralControl("</td></tr>")); } } AccidentPlaceHolder.Controls.Add(new LiteralControl("</table>"));}protected override void OnPreRender(EventArgs e){ foreach (var ctrl in AddedControls) { var key = ctrl.ID.Replace("TextBox", String.Empty); Session[key] = ctrl.Text; } foreach (string session in Session.Keys) { System.Diagnostics.Debug.WriteLine(String.Format("{0} = {1}", session, Session[session])); } base.OnPreRender(e);}}\[/code\]Any Help would be appreciated...i've been looking and debugging it for a few hours and nothing has fixed it.

