I have gotten this page to store each variable in a session for the dynamic texbox's, however i need these to stay there in the current position if the page is left as well as store the latest data from the textbox's. I have tried putting the reverse of the end code in the load_page event but it doesn't seem to work.
Basically I need everything to stay the same of how it was on page return. I've looked into Viewstate but not 100% sure on how to use it. Any suggestions/sample code would be appreciated. I have a c# book but unfortunately it does not show me many examples on how to use viewstate, I'm pretty sure this is the answer to my problem just don't know how to impliment it into this.C#:\[code\]Protected void Page_Load(object sender, EventArgs e){ if (AddedControls.Count > 0 && !IsPostBack) <----**Code i've tried** { foreach (var ctrl in AddedControls) <----------**Code i've tried** { var key = ctrl.ID.Replace("TextBox", String.Empty); <-----------**Code** ctrl.Text = Session[key].ToString(); <-------------------**I've Tried** } foreach (string session in Session.Keys) { System.Diagnostics.Debug.WriteLine(String.Format("{0} = {1}", session, Session[session])); } } //LoadViewState();}protected int TotalNumberAdded{ get { return (int)(ViewState["TotalNumberAdded"] ?? 0); } set { ViewState["TotalNumberAdded"] = value; }}protected void AccidentButton_Click(object sender, EventArgs e){ TotalNumberAdded++; BuildControls();}protected void PrevPage_Click(object sender, EventArgs e){ Response.Redirect("employment_driversapplication_emphistory.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(){ for (var Nl = 0; Nl < TotalNumberAdded; Nl++) { var idNL = String.Format("NatureLabel{0}", Nl); var idRQ = String.Format("NatureRequired{0}", Nl); var idN = String.Format("NatureTextBox{0}", Nl); var idDL = String.Format("DateLabel{0}", Nl); var idD = String.Format("DateTextBox{0}", Nl); var idFL = String.Format("FatalLabel{0}", Nl); var idF = String.Format("FatalTextBox{0}", Nl); var idIL = String.Format("InjuryLabel{0}", Nl); var idI = String.Format("InjuryTextBox{0}", Nl); var idDR = String.Format("DateRegex{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("<table><tr>")); AccidentPlaceHolder.Controls.Add(new LiteralControl("<br />")); AccidentPlaceHolder.Controls.Add(new LiteralControl("<tr><td class='title-text'>")); AccidentPlaceHolder.Controls.Add(NLabel); AddedControlsLabel.Add(NLabel); //******************************************************************************************** } if (AccidentPlaceHolder.FindControl(idN) == null) { var NtextBox = new TextBox() { ID = idN }; var NRequired = new RequiredFieldValidator() { ID = idRQ }; NRequired.ControlToValidate = NtextBox.ID; NRequired.Text = "Please enter the nature"; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>")); AccidentPlaceHolder.Controls.Add(NtextBox); AccidentPlaceHolder.Controls.Add(NRequired); AddedControls.Add(NtextBox); } //************************************************************************* 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); } //************************************************************************* if (AccidentPlaceHolder.FindControl(idD) == null) { var DtextBox = new TextBox() { ID = idD }; var DRegex = new RegularExpressionValidator() { ID = idDR }; DRegex.Text = "Format: MM/DD/YYY"; DRegex.ValidationExpression = @"^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$"; DRegex.ControlToValidate = DtextBox.ID; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>")); AccidentPlaceHolder.Controls.Add(DtextBox); AccidentPlaceHolder.Controls.Add(DRegex); AddedControls.Add(DtextBox); AccidentPlaceHolder.Controls.Add(new LiteralControl("</td></tr>")); } //************************************************************************* 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); } //************************************************************************** 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); } //************************************************************************** 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); } //**************************************************************************** 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.ToString(); } foreach (string session in Session.Keys) { System.Diagnostics.Debug.WriteLine(String.Format("{0} = {1}", session, Session[session])); } base.OnPreRender(e);}protected void NextButton_Click(object sender, EventArgs e){ Response.Redirect("employment_driversapplicaiton_emphistory.aspx");}}\[/code\]Any help would be appreicated, I know my acceptance % isn't good but its because when i first started this i was not aware of what it was Please help.Also, if viewstate is not the way to go, let me know, as i said i've done some research but not sure exactly how to use it.Thanks
Basically I need everything to stay the same of how it was on page return. I've looked into Viewstate but not 100% sure on how to use it. Any suggestions/sample code would be appreciated. I have a c# book but unfortunately it does not show me many examples on how to use viewstate, I'm pretty sure this is the answer to my problem just don't know how to impliment it into this.C#:\[code\]Protected void Page_Load(object sender, EventArgs e){ if (AddedControls.Count > 0 && !IsPostBack) <----**Code i've tried** { foreach (var ctrl in AddedControls) <----------**Code i've tried** { var key = ctrl.ID.Replace("TextBox", String.Empty); <-----------**Code** ctrl.Text = Session[key].ToString(); <-------------------**I've Tried** } foreach (string session in Session.Keys) { System.Diagnostics.Debug.WriteLine(String.Format("{0} = {1}", session, Session[session])); } } //LoadViewState();}protected int TotalNumberAdded{ get { return (int)(ViewState["TotalNumberAdded"] ?? 0); } set { ViewState["TotalNumberAdded"] = value; }}protected void AccidentButton_Click(object sender, EventArgs e){ TotalNumberAdded++; BuildControls();}protected void PrevPage_Click(object sender, EventArgs e){ Response.Redirect("employment_driversapplication_emphistory.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(){ for (var Nl = 0; Nl < TotalNumberAdded; Nl++) { var idNL = String.Format("NatureLabel{0}", Nl); var idRQ = String.Format("NatureRequired{0}", Nl); var idN = String.Format("NatureTextBox{0}", Nl); var idDL = String.Format("DateLabel{0}", Nl); var idD = String.Format("DateTextBox{0}", Nl); var idFL = String.Format("FatalLabel{0}", Nl); var idF = String.Format("FatalTextBox{0}", Nl); var idIL = String.Format("InjuryLabel{0}", Nl); var idI = String.Format("InjuryTextBox{0}", Nl); var idDR = String.Format("DateRegex{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("<table><tr>")); AccidentPlaceHolder.Controls.Add(new LiteralControl("<br />")); AccidentPlaceHolder.Controls.Add(new LiteralControl("<tr><td class='title-text'>")); AccidentPlaceHolder.Controls.Add(NLabel); AddedControlsLabel.Add(NLabel); //******************************************************************************************** } if (AccidentPlaceHolder.FindControl(idN) == null) { var NtextBox = new TextBox() { ID = idN }; var NRequired = new RequiredFieldValidator() { ID = idRQ }; NRequired.ControlToValidate = NtextBox.ID; NRequired.Text = "Please enter the nature"; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>")); AccidentPlaceHolder.Controls.Add(NtextBox); AccidentPlaceHolder.Controls.Add(NRequired); AddedControls.Add(NtextBox); } //************************************************************************* 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); } //************************************************************************* if (AccidentPlaceHolder.FindControl(idD) == null) { var DtextBox = new TextBox() { ID = idD }; var DRegex = new RegularExpressionValidator() { ID = idDR }; DRegex.Text = "Format: MM/DD/YYY"; DRegex.ValidationExpression = @"^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$"; DRegex.ControlToValidate = DtextBox.ID; AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>")); AccidentPlaceHolder.Controls.Add(DtextBox); AccidentPlaceHolder.Controls.Add(DRegex); AddedControls.Add(DtextBox); AccidentPlaceHolder.Controls.Add(new LiteralControl("</td></tr>")); } //************************************************************************* 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); } //************************************************************************** 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); } //************************************************************************** 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); } //**************************************************************************** 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.ToString(); } foreach (string session in Session.Keys) { System.Diagnostics.Debug.WriteLine(String.Format("{0} = {1}", session, Session[session])); } base.OnPreRender(e);}protected void NextButton_Click(object sender, EventArgs e){ Response.Redirect("employment_driversapplicaiton_emphistory.aspx");}}\[/code\]Any help would be appreicated, I know my acceptance % isn't good but its because when i first started this i was not aware of what it was Please help.Also, if viewstate is not the way to go, let me know, as i said i've done some research but not sure exactly how to use it.Thanks