Good morning,

I've searched the web for a week to find a solution to a problem, but I can't figure it out, so now I'll try to post directly what the problem is and hope that someone could explain me how to go ahead. I'm new to Asp.Net, and some years have passed since I used to programme in C/C++.

I have an aspx page with two "Controller" textboxes.
If you insert a number X in TB1, on the OnTextChanged event, a Placeholder is populated with X TextBoxes (the number you entered).
If you insert a number H in TB2, on the OnTextChanged event, a Placeholder2 is populated with H TextBoxes (the number you entered).

The problem is that each time you enter a number you have a postback, hence after the first time, you loose all the data and the TextBox Controls in the PlaceHolder already populated. I cannot use Ajax UPdatePanel, so I know I should save the user data with the viewstate or Session, but i don't have idea of how to recall them to repopulate the PlaceHolder after Postback.

Here is my code C#(I tried to leave just the interesting parts).

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CharmingUmbria._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">

<title>Web software Charming Umbria</title>
<script type="text/javascript" src="../js/mootools/mootools.v1.11.js"></script>
<script type="text/javascript" src="../js/mootools/slimbox.js"></script>
<script type="text/javascript" src="../js/mootools/fixpng.js"></script>
<script src="../js/global.js" type="text/javascript"></script>
<link href="../css/slimbox.css" rel="stylesheet" type="text/css" media="screen" />
<link href="../css/screen.css" rel="stylesheet" type="text/css" media="screen" />
<link href="../css/print.css" rel="stylesheet" type="text/css" media="print" />

<script type="text/javascript" src="../js/mootools/calendar1.js"></script>
<link href="../css/dashboard.css" rel="stylesheet" type="text/css" media="screen" />

</head>
<body>
    <form id="form1" runat="server">

        
<div id="contenutiPrinc">

	<h1 style="width: 447px">Benvenuto</h1>
	
	<div class="rigaPrinc">
	    <fieldset class="simple">
	    <legend>Ricerca Disponibilità</legend>
            <table>
                <tr>
                <td valign="middle">
                         <label for="labelpax">Pax </label>
                         <asp:TextBox ID="paxssetb" runat="server" CssClass="input dashboard" ToolTip="Inserire il numero di partecipanti"></asp:TextBox></td>
                <td valign="middle">
                         <label for="labelcld">Children</label>
                         <asp:TextBox ID="cldtb" Text="0" runat="server" CssClass="input dashboard" OnTextChanged="cldtb_OnTextChanged" AutoPostBack="true" ToolTip="Inserire il numero di bambini"></asp:TextBox>
                         <asp:HiddenField ID="cldhf" runat="server" value="false"/>
                         </td>

                <td valign="middle" rowspan="1" colspan="3">
                    <label for="pnlEnterNumberslbl" id="agelb"  runat="server" visible="false">Birthdates</label>
                    <asp:Panel id="pnlEnterNumbers" runat="server">
	                   <asp:PlaceHolder id="myPlaceHolder" runat="server" />
                    </asp:Panel>
                    </td>

                </tr>
                    
                <tr><td colspan=4 valign="middle"><label for="labelsolu">Required Rooms</label> 
                    <asp:TextBox ID="alloggitb" runat="server" CssClass="input" OnTextChanged="alloggitb_OnTextChanged" AutoPostBack="true"
                        ToolTip="Inserire il numero di alloggi in cui il numero totale di pax dovrà essere suddiviso" 
                        Width="33px"></asp:TextBox>  
                   </td>
                </tr>
                <tr>
                <td colspan="5">
                    <asp:Panel id="AlloggiPnl" runat="server">
	                   <asp:PlaceHolder id="MyPlaceHolder1" runat="server" />
                    </asp:Panel>
                    </td>
                </tr>
                <tr>
                    <td colspan="5">
                       <asp:ImageButton ID="CercaBtn" runat="server" ImageUrl="../ima/bottoni/bottone-cerca.png" CssClass="fixPNG" /></td>
                </tr>
                
            </table>
	    </fieldset>	
	</div>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CharmingUmbria.classi;
using System.Web.UI.MobileControls;
using CommonLibrary.Helpers;
using System.Globalization;

namespace CharmingUmbria
{
  public partial class _Default : System.Web.UI.Page
  {
		protected override void OnInit(EventArgs e)
		{
            
                this.Load += new EventHandler(Page_Load);
                this.CercaBtn.Click += new ImageClickEventHandler(CercaBtn_Click);
                base.OnInit(e);

  		}
		protected void Page_Load(object sender, EventArgs e)
        {
        } 
       
        protected void CercaBtn_Click(object sender, ImageClickEventArgs e) {
        /*some istructions with textbox values entered*/
        }
        protected void cldtb_OnTextChanged(object sender, System.EventArgs e)
        {
            int I, intNumberCount;
            try
            {
                // Get number of items to be entered.
                intNumberCount = System.Convert.ToInt16(cldtb.Text);

                if (intNumberCount &gt; 0)
                {
                    agelb.Visible = true;
                    //Add one textbox to enter each item.
                    System.Web.UI.WebControls.Label lblNumber;
                    System.Web.UI.WebControls.TextBox txtNumber;
                    for (I = 1; I &lt;= intNumberCount; I++)
                    {
                        lblNumber = new System.Web.UI.WebControls.Label();
                        lblNumber.Text = "&lt;strong&gt;Bambino " + I + ":&lt;/strong&gt; ";
                        myPlaceHolder.Controls.Add(lblNumber);

                        txtNumber = new System.Web.UI.WebControls.TextBox();
                        txtNumber.ID = "birthdate" + I;
                        txtNumber.Text = "";
                        txtNumber.CssClass = "input";
                        txtNumber.MaxLength = 8;
                        txtNumber.ToolTip = "Inserire la data di nascita nel formato gg/mm/aa";
                        /*txtNumber.BorderStyle= BorderStyle.Double;	
                        txtNumber.BorderWidth= Unit.Pixel(3);
                        txtNumber.Width = Unit.Pixel(128);
                        txtNumber.Height = Unit.Pixel(22);
                        txtNumber.BorderColor = System.Drawing.Color.FromArgb(233,192,130);*/
                        myPlaceHolder.Controls.Add(txtNumber);

                        System.Web.UI.WebControls.RegularExpressionValidator valid = new System.Web.UI.WebControls.RegularExpressionValidator();
                        valid.ID = "cvbirthdate" + I;
                        myPlaceHolder.Controls.Add(valid);
                        valid.ControlToValidate = "birthdate" + I;
                        valid.ErrorMessage = "Insert the date of birth as dd/mm/yy";
                        valid.Display = ValidatorDisplay.Dynamic;
                        valid.ValidationExpression = "^(((((0[1-9])|([1-2][0-9])|(3[0-1]))))\x2F(((0[1-9])|(1[0-2])))\x2F(([0-9]{2})))$";
                        valid.SetFocusOnError = true;

                        lblNumber = new System.Web.UI.WebControls.Label();
                        lblNumber.Text = "&amp;nbsp;&amp;nbsp;&amp;nbsp;";
                        myPlaceHolder.Controls.Add(lblNumber);
                    }

                    // Add number of items to viewstate
                    ViewState["NumberCount"] = (I - 1);
                    //Session["holdMe"] = myPlaceHolder;
                }
                else agelb.Visible = false;
           }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Error:" + ex.Message);
                agelb.Visible = false;
                return;
            }
      }
        protected void alloggitb_OnTextChanged(object sender, System.EventArgs e) 
      {
          int I, intNumberCount;

          try
          {
              // Get number of items to be entered.
              intNumberCount = System.Convert.ToInt16(alloggitb.Text);

              if (intNumberCount &gt; 0)
              {
                  //Add one textbox to enter each item.
                  System.Web.UI.WebControls.Label lblalloggi;
                  System.Web.UI.WebControls.TextBox txtNumber;
                  for (I = 1; I &lt;= intNumberCount; I++)
                  {
                      lblalloggi = new System.Web.UI.WebControls.Label();
                      lblalloggi.Text = "&lt;strong&gt;Alloggio " + I + ":&lt;/strong&gt; Adulti";
                      MyPlaceHolder1.Controls.Add(lblalloggi);

                      txtNumber = new System.Web.UI.WebControls.TextBox();
                      txtNumber.ID = "adults" + I;
                      txtNumber.Text = "";
                      txtNumber.CssClass = "input";
                      txtNumber.Width = Unit.Pixel(16);
                      txtNumber.MaxLength = 2;
                      txtNumber.ToolTip = "Insert the number of adults in the room";
                      MyPlaceHolder1.Controls.Add(txtNumber);

                      lblalloggi = new System.Web.UI.WebControls.Label();
                      lblalloggi.Text = "&amp;nbsp;&amp;nbsp;&amp;nbsp;Bambini";
                      MyPlaceHolder1.Controls.Add(lblalloggi);

                      txtNumber = new System.Web.UI.WebControls.TextBox();
                      txtNumber.ID = "bimbi" + I;
                      txtNumber.Text = "";
                      txtNumber.CssClass = "input";
                      txtNumber.Width = Unit.Pixel(16);
                      txtNumber.MaxLength = 2;
                      txtNumber.ToolTip = "Insert the number of children in the room";
                      MyPlaceHolder1.Controls.Add(txtNumber);

                      lblalloggi = new System.Web.UI.WebControls.Label();
                      lblalloggi.Text = "&amp;nbsp;&amp;nbsp;&amp;nbsp;";
                      MyPlaceHolder1.Controls.Add(lblalloggi);
                      
                      System.Web.UI.WebControls.RangeValidator val = new System.Web.UI.WebControls.RangeValidator();
                      val.ID = "cvadults"+I;
                      //add control to placeholder
                      MyPlaceHolder1.Controls.Add(val);
                      val.ControlToValidate= "adults"+I;
                      val.ErrorMessage = "Inserire un numero";
                      val.Display = ValidatorDisplay.Dynamic;
                      val.Type = ValidationDataType.Integer;
                      val.MinimumValue = "0";
                      val.MaximumValue = "9999";
                      val.SetFocusOnError = true;

                      val = new System.Web.UI.WebControls.RangeValidator();
                      val.ID = "cvbimbi" + I;
                      //add control to placeholder
                      MyPlaceHolder1.Controls.Add(val);
                      val.ControlToValidate = "bimbi" + I;
                      val.ErrorMessage = "Inserire un numero";
                      val.Display = ValidatorDisplay.Dynamic;
                      val.Type = ValidationDataType.Integer;
                      val.MinimumValue = "0";
                      val.MaximumValue = "9999";
                      val.SetFocusOnError = true;

                      lblalloggi = new System.Web.UI.WebControls.Label();
                      lblalloggi.Text = "&lt;br /&gt;" + Environment.NewLine;
                      MyPlaceHolder1.Controls.Add(lblalloggi);
                  }

                  // Add number of items to viewstate
                  ViewState["AlloggiCount"] = (I - 1);
              }
          }
          catch (Exception ex)
          {
              System.Diagnostics.Trace.WriteLine("Error" + ex.Message);
              return;
          }

      }
  }
}

Solved with Javascript function on client side and a hidden field to pass the dynamic controls values to the server on postbacks.
Maybe not elegant, but it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.