动态添加控件[可重复同一控件]

遇到的问题:任何一个控件(包括自定义控件),均不能实现重复添加到页面的功能。下面的思路能够重复生成,但是在还是无法实现相应控件的事件而动态生成的问题。

解决思路:用两个ascx控件,其中下面第一个是第二个ShopTableStyle1.ascx的容器,并在第一个中写入2中的代码

1:在ascx文件中 添加<asp:PlaceHolderID="ctrlList" runat="server"></asp:PlaceHolder>控件

2:在ASCX.CS中编写如下代码:

  System.Web.UI.Control Ctrl1 = new Control();
  System.Web.UI.Control Ctrl2 = new Control();

                ArrayList al = new ArrayList();

                //考虑页面内容 目前暂时手工增加机构 555,556,557,696,697,699
                //al = CF.Base.Access.Start.GetPubUserIDtoShop(5);
                al.Add(555);
                al.Add(556);
                al.Add(557);
                al.Add(869);
                al.Add(315);

                for (int i = 0; i < al.Count; i++)
                {
                    if (i % 2 == 0)
                    {
                        Ctrl1 = LoadControl("/ctrl/ShopTableStyle1.ascx");
                        ((ShopTableStyle1)Ctrl1).AssID = int.Parse(al[i].ToString());//AssID为定义在控件中的属性
                        ((ShopTableStyle1)Ctrl1).EnableViewState = false;
                        this.ctrlList.Controls.Add(Ctrl1);
                    }
                    else
                    {
                        Ctrl2 = LoadControl("/ctrl/ShopTableStyle2.ascx");
                        ((ShopTableStyle2)Ctrl2).AssID = int.Parse(al[i].ToString());
                        ((ShopTableStyle2)Ctrl2).EnableViewState = false;
                        this.ctrlList.Controls.Add(Ctrl2);
                    }
                }

 

给一段我的实现

    System.Web.UI.Control Ctrl1 = new Control();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Ctrl1 = this.LoadControl("condition.ascx");
            Ctrl1.EnableViewState = true;
            ((RadioButtonList)Ctrl1.FindControl("RadioButtonList1")).Items.Clear();
            divConditionUserControl.Controls.Add(Ctrl1);
            ViewState["0"] = 0;

        }
       
        if (ViewState.Count!=1)
        {
            for (int i = 0; i < ViewState.Count; i++)
            {
               
                 Ctrl1 =this.LoadControl("condition.ascx");                
                 Ctrl1.EnableViewState = true;
                 divConditionUserControl.Controls.Add(Ctrl1);
                 //Label6.Text= ((RadioButtonList)(Ctrl1.FindControl("RadioButtonList1"))).Items.Count.ToString();
            }
        }
    }

    static int j = 1;
    protected void btnAddConditions_Click(object sender, EventArgs e)
    {

       
        ViewState[j++.ToString()] = j++;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值