Adding multiple controls programattically. C#

Category : Uncategorized

It is easy to add one new control to your page but after that you may have troubles.  Here is one option that uses Postbacks and Session.

Basically,  I save a list of textboxes as a session variable and then check them on button click and add them to the form — I also give them unique ID’s so I can do something with them later on in the process…. Here is the code:

        protected void Button1_Click(object sender, EventArgs e)
        {
            //This will be changed if we have controls in session.
            int tbId = 1;
 
            List seshTextboxes = new List();
 
            if (Session["controlsList"] != null)
            {
                List list2 = (List)Session["controlsList"];
 
                //Change the newest Id to the new tb
                tbId = list2.Count + 1;
 
                //Get controls out of session
                foreach (ASPxTextBox tbSesh in list2)
                {
                    seshTextboxes.Add(tbSesh);
                    form1.Controls.Add(tbSesh);
                }
            }
 
            ASPxTextBox tb = new ASPxTextBox();
            tb.ID = "Column" + tbId;
            tb.Text = tbId.ToString();
            seshTextboxes.Add(tb);
            form1.Controls.Add(tb);
 
            Session["controlsList"] = seshTextboxes;
        }

Screenshot:

Dynamic Controls

You can create any number of dynamic controls to your form this way.

How to use a Javascript Confirm box.

Category : Uncategorized

Example Scenario:
Lets say you have a form that has a BACK link. You want to make sure that if the user has made any changes on that form and they click the BACK that they REALLY want to leave the page.

So on your input boxes you can place an onChange event that sets a global variable to true (yes this form has been changed!)… i.e. onchange=”g_bformChanged=true;”

Then on your back button just fire off your function that checks to see if the form has changed… using your CONFIRM box to get a confirmation from your user.

  <script type="text/javascript">
      //Global
    var g_bformChanged = false;
 
   function checkForSaveNeed() {
        if (g_bformChanged) {
            var confirmLeave = confirm("No changes will be saved.  Are you sure you want to leave?");
            if (confirmLeave == true) {
                window.location = "ManageSchools.aspx"
            }            
        }
        else 
            window.location = "ManageSchools.aspx";
     }
</script>