Save controls at runtime in asp page?

2019-08-27 12:27发布

问题:

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;
    }
}

回答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();
}