Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created html generic controls as (ul) and (il) at runtime then i wanna to loop inside my page to get all ul and il created in runtime but when I click button then all controls I created disappear how i save it in my page to can loop in it.

C#
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;
    }
}
Posted
Updated 6-Apr-12 14:54pm
v2

1 solution

Controls that are created at runtime have to be called either OnPageLoad or OnPageInit without checking IsPostBack property of the page. This will gave the controls to retain their associate events and properties on the page. For example,:Assuming that you have a div with an ID div1 that runs as a sever(runat="server") control, The following code will add a Button "Submit" with it's event to a page.
C#
protected void Page_Load(object sender, EventArgs e)
{
    Button btnSubmit = new Button();
    btnSubmit.ID = "btnSubmit";
    btnSubmit.Text = "Submit";
    btnSubmit.Click += new EventHandler(btnSubmit_Click);
    div1.Controls.Add(btnSubmit);
}

protected void btnSubmit_Click(object sender, System.EventArgs e)
{

}
 
Share this answer
 
Comments
nagiub2007 6-Apr-12 20:03pm    
@Wonde Tadesse
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
Wonde Tadesse 6-Apr-12 20:47pm    
Where are you calling AddHtmlGroups method ? o_O, OnPageLoad or OnPageInit.
Either you are passing parameter or not, the method has to be called OnPageLoad or OnPageInit. This is a must. However you may check the parameter is passed or not before calling the method. Simple if condition statement. Like

protected void Page_Load(object sender, EventArgs e)
{
if(ulnumber!=null || ulnumber>0) // And check the rest of the parameters
AddHtmlGroups(,,ulnumber);
}


nagiub2007 6-Apr-12 21:34pm    
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
Wonde Tadesse 6-Apr-12 21:57pm    
If you follow what I wrote as a solution, you'll get the concept easily. In addition, see my previous comment. It's quite simple. Cheers.
nagiub2007 7-Apr-12 11:16am    
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900