0

I created HTML generic controls as (ul) and (il) at runtime. Then I want to loop inside my page to get all ul and il created at run time, but when i click the button then all the controls I created disappear. How do I save it in my page to loop over it?

protected void AddHtmlGroups( int groups,ArrayList judids ,int judnum)
{
    for (int i = 1; i <= groups; i++)
    {
        HtmlGenericControl myUnOrderedList = new HtmlGenericControl("ul");
        HtmlGenericControl fieldset = new HtmlGenericControl("fieldset ");

        HtmlGenericControl legend = new HtmlGenericControl("legend  ");

        legend.InnerText="المجموعه " + i.ToString();
        legend.Attributes.Add("class", "group");
        myUnOrderedList.Controls.Add(legend);
        myUnOrderedList.ID = i.ToString()+"g";

        myUnOrderedList.Attributes.Add("class", "profList column");

        for (int j = 1; j <= judnum; j++)
        {
            if (judids.Count > 0)
            {
                HtmlGenericControl listItem = new HtmlGenericControl("li");
                listItem.ID = judids[0].ToString();
                string judname = getjudgeName(Convert.ToInt32(judids[0]));
                if (judname != null)
                {
                    listItem.InnerText = judname;
                }
                judids.RemoveAt(0);

                myUnOrderedList.Controls.Add(listItem);
            }
        }

        fieldset.Controls.Add(myUnOrderedList);

        Panel1.Controls.Add(fieldset);
        Panel1.Visible = true;
    }
}
Phil
  • 42,255
  • 9
  • 100
  • 100
ahmed naguib
  • 145
  • 14

1 Answers1

1

when i click the button then all the controls I created disappear

Presumably you are clicking a button which causes a postback, which causes the page to be recreated. You must rebuild your dynamic control tree every time the page loads from OnInit or OnLoad (use OnLoad if you need access to ViewState).

Once your control tree is recreated, you can execution actions against it, such as reading the items, adding an item, removing an item, etc.

Often control trees need to "remember" their state between postbacks. You can use:

  • ViewState (use it only in limited quantities, because it greatly increases the size of the page)

  • Session state (I use a queue to make sure each session can only have a limited number of objects).

  • Database (use if you want to persist the changes and/or if you can recreate the control tree from the database).

Example

This is a very simple example which demonstrates the flow with Session as the state mechanism. Remember that if you use Session it consumes server memory and can be accessed from any page within that session (so if the user opens two of the same page at once, you can cause conflicts).

protected override void OnLoad( EventArgs e)
{
    // build every time the page loads
    BuildTree();
}

private void BuildTree()
{
    // clear existing dynamic controls
    this.MyOutputPanel.Controls.Clear();

    // if there is a value in session, use it
    if( Session["Number"] != null )
    {
        int number = (int)Session["Number"];

        // create some dynamic controls
        for( int i = 0; i < number; i++ )
        {
            var div = new HtmlGenericControl( "div" );
            this.MyOutputPanel.Controls.Add( div );
        }
    }

    // otherwise, do nothing
}

protected void btnDoSomething_Click( object sender, EventArgs e )
{
    // 1. Get the value from the user and store it in Session
    Session["Number"] = Convert.ToInt32( this.txtFoo.Text );

    // 2. rebuild your tree of controls
    BuildTree();
}
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • thank u 4 reply but method i posted that created these html conytols i send aparameters to it i know these parameter when user enter it as ul number so how i can to recreate it in page load i wanna when it created saved in page cause idonnot know number of controls without user enter spesific data – ahmed naguib Apr 07 '12 at 01:05
  • ok thank u really thank u but i call AddHtmlGroups method and pass parameters user entered like number of ul and number of il inside it then user sort il inside ul and click confirm when user click confirm page load execute and all il an ul i created remove how i can save it in page before user click confirm – ahmed naguib Apr 07 '12 at 01:32
  • It sounds like you need to create an object for storing this data, and put that in session. Your object could contain whatever data you need, and you can build your controls from that. – Tim M. Apr 07 '12 at 03:24
  • y but can you explain how can i store in object just placeholder contain this or what object cal hold these dynamic controls – ahmed naguib Apr 07 '12 at 07:47
  • i dontot need to create them again as user move ils and click confirm so i need the las arrangment of il an ul not create it again – ahmed naguib Apr 07 '12 at 15:16
  • i will explain to you i created these controls when user click button1 and enter ul number. call method addhtmlgroups() to render il and ul ,then these il an ul sortable user move il from ul to another then click submit i want when user click submit take the last shape of il and ul and loop inside it but when click submit page postback and all htmlcontrol diappear – ahmed naguib Apr 08 '12 at 09:10