在一个加载类里动态生成一个PlaceHolder,要创建各种控件添加到PlaceHolder上,最后可以在页面上创建一个PlaceHolder,就可以显示你所创建的所有控件所组成的一个页面了。
例如:
public class LoadUI
{
        private void AddControl(Table temptable, TableRow tr)
        {
            if (Type.ToUpper() == "TEXTBOX")
            {
                TextBox tb = new TextBox();
                tb.CssClass = Css;
                tb.ID = TableName + "_" + FieldName;
                tb.ReadOnly = Boolean.Parse(ReadOnly);
                tb.MaxLength = int.Parse(MaxLength);
                if (DataFrom == "")
                {
                    tb.Text = "";
                }
                tb.TextMode = (TextBoxMode)Enum.Parse(typeof(TextBoxMode), TextMode);                AddControlToCell(temptable, tr, tb);                
            }
       }}上面函数里创建了一个TextBox控件,.net自带的控件都可以创建了。现在 我想创建一个我自定义的控件,请高手帮助?
如何才可以在这个类里引用并创建一个自定义的控件。

解决方案 »

  1.   

    你的自定义控件是一个dll吗?
    如果是的话,引用那个dll,和textBox一样声明
      

  2.   


    Control c = this.LoadControl("~/UserControl1.ascx");
    AddControlToCell(temptable, tr, c); 
      

  3.   

     /// <summary>
            /// 填充窗体上的button
            /// </summary>
            private void FillButton()
            {
                //设置Button的tag,用这个临时变量
                int temp = 0;
                for (int i = 0; i < 3;i++ )
                {
                    for (int j = 0; j < 6; j++)
                    {
                        Button btn = new Button();
                        btn.Text = names[temp];
                        btn.Visible = false;
                        btn.Tag = temp;
                        btn.Width = 60;
                        btn.Location = new Point(j * 100 + 30, (i + 1) * 40);
                        this.Controls.Add(btn);
                        temp++;
                    }
                }
            }
    参考,这是winform的……
      

  4.   


    这样创建的控件是Control类型的,我不能使用UserControl1特有的属性和方法
    我还是把控件做成dll格式吧