如何动态定位用户控件 [英] how to Position the user control Dynamically

查看:70
本文介绍了如何动态定位用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我们创建了一个显示甘特图的用户控件.
并且我们正在尝试通过代码动态创建用户控件来添加n个控件,但我们面临着将控件彼此冠以的问题.也就是说,它显示了控件,但另一个控件中的一个控件可以帮助我们解决此问题.
请通过代码找到用于创建动态UserControl的代码

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

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

推荐答案

在不了解其外观的情况下很难提供准确的帮助,但是我重新排列了for循环的内容,并发现了对Add(dynDiv)的额外调用.这是我的改组尝试:

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:

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.


这篇关于如何动态定位用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆