Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
we have created a user control that displays the Gantt chart.
and we are trying to add n number of controls by creating the user control Dynamically through code but we are facing the problem of alligning the control one below other. i.e it shows the control but one control within another could any one help us how to resolve this Problem.
Please find the Code for creating Dynamic UserControl through code
C#
    public void insertDynamicControl()
    {
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["PlazmaGlobal1"].ConnectionString);
        SqlCommand cmd = new SqlCommand("select ProjectID,ProjectName from Dim_Project", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds, "Dim_Project");              

        //System.Web.UI.HtmlControls.HtmlGenericControl dynDiv =
        // new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
        //dynDiv.ID = "dynDivCode";
        //dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
        //dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");
        //dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px");
        //dynDiv.InnerHtml = "I was created using Code Behind";
        //this.Controls.Add(dynDiv);

        HtmlGenericControl Brek = new HtmlGenericControl("<Br/>");

        for (int i = 0; i < ds.Tables[0].Rows.Count;i++)
        {
            System.Web.UI.HtmlControls.HtmlGenericControl dynDiv =
             new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
            dynDiv.ID = "dynDivCode"+i.ToString();
            dynDiv.Style.Add(HtmlTextWriterStyle.Position, "300"+(i)+"px");

            Panel p1 = new Panel();
            p1.ID = "description_HeaderPanel"+i.ToString();

            //p1.Controls.Add(Brek);
            ImageButton ImB = new ImageButton();
            ImB.ID = "Description_ToggleImage"+i.ToString();
            ImB.ImageUrl = "~/images/collapse.jpg";
            ImB.AlternateText = "collapse";

            //ImB.OnClientClick = "Description_ToggleImage_Click1";
            ImB.Click += new ImageClickEventHandler(Description_ToggleImage_Click);
                      
            
            p1.Controls.Add(ImB);

            //p1.Controls.Add(Brek);
            dynDiv.Controls.Add(p1);
            form1.Controls.Add(dynDiv);

            Panel p2 = new Panel();
            p2.ID = "description_ContentPanel"+i.ToString();

            EventCalendar.EventCalendarControl E3 = new EventCalendar.EventCalendarControl();
            E3.ID = "EventCalendarControl"+i.ToString();
            //E3.Visible = true ;

            //p2.Controls.Add(Brek);
            p2.Controls.Add(E3);

            dynDiv.Controls.Add(p2);
            form1.Controls.Add(dynDiv);

            AjaxControlToolkit.CollapsiblePanelExtender coll = new AjaxControlToolkit.CollapsiblePanelExtender();
            coll.ID = "cpeDesc"+i.ToString();
            coll.TargetControlID = "description_ContentPanel"+i.ToString();
            coll.ExpandControlID = "description_HeaderPanel"+i.ToString();
            coll.CollapseControlID = "description_HeaderPanel"+i.ToString();
            coll.Collapsed = false;
            coll.ImageControlID = "description_ToggleImage"+i.ToString();
            coll.SuppressPostBack = true;
            form1.Controls.Add(coll);
        }
}
Posted
Updated 5-May-11 1:23am
v3
Comments
Oshtri Deka 5-May-11 7:18am    
Tag your question as ASP.Net.

1 solution

It's kind of hard to give any accurate help without an idea of what it's supposed to look like, but I rearranged the for loop contents a bit, and found an extra call to Add(dynDiv). Here's my reorg attempt:

C#
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    // create the controls
    ImageButton ImB = new ImageButton();
    ImB.ID = "Description_ToggleImage" + i.ToString();
    ImB.ImageUrl = "~/images/collapse.jpg";
    ImB.AlternateText = "collapse";
    ImB.Click += new ImageClickEventHandler(Description_ToggleImage_Click);

    EventCalendar.EventCalendarControl E3 = new EventCalendar.EventCalendarControl();
    E3.ID = "EventCalendarControl" + i.ToString();

    AjaxControlToolkit.CollapsiblePanelExtender coll = new AjaxControlToolkit.CollapsiblePanelExtender();
    coll.ID = "cpeDesc" + i.ToString();
    coll.TargetControlID = "description_ContentPanel" + i.ToString();
    coll.ExpandControlID = "description_HeaderPanel" + i.ToString();
    coll.CollapseControlID = "description_HeaderPanel" + i.ToString();
    coll.Collapsed = false;
    coll.ImageControlID = "description_ToggleImage" + i.ToString();
    coll.SuppressPostBack = true;

    // create the panels
    Panel p1 = new Panel();
    p1.ID = "description_HeaderPanel" + i.ToString();
    Panel p2 = new Panel();
    p2.ID = "description_ContentPanel" + i.ToString();

    // create the container div
    System.Web.UI.HtmlControls.HtmlGenericControl dynDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
    dynDiv.ID = "dynDivCode" + i.ToString();
    dynDiv.Style.Add(HtmlTextWriterStyle.Position, "300" + (i) + "px");

    // add the controls to the panels
    p1.Controls.Add(ImB);
    p2.Controls.Add(E3);

    // add the panels to the container div
    dynDiv.Controls.Add(p1);
    dynDiv.Controls.Add(p2);

    //add the div to the form
    form1.Controls.Add(dynDiv);
    form1.Controls.Add(Brek);

    // add the ajax thing to the form
    form1.Controls.Add(coll);
    form1.Controls.Add(Brek);
}


BTW, if you run the page and let it render, and then view the page source, you'll likely see any problems, and be able to modify your code accordingly.
 
Share this answer
 
v2

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