日期:2014-05-17  浏览次数:20417 次

用户控件获得数据的问题
用户控件:
C# code
 public partial class UcCreatTable : System.Web.UI.UserControl
    {
        int Count
        {
            get
            {
                if (ViewState["ControlCount"] == null)
                    ViewState["ControlCount"] = 0;
                return (int)ViewState["ControlCount"];
            }
            set
            {
                ViewState["ControlCount"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ImgBtnAddField_Click(object sender, ImageClickEventArgs e)
        {
            Count++;
            for (int i = 0; i < Count; i++)
            {
                string[] drpItems = { "请选择字段类型", "int", "nvchar", "datetime" };
                Label lb1 = new Label();
                lb1.Text = "字段";
                lb1.ID = ClientID;
                PlaceHolder1.Controls.Add(lb1);
                TextBox txt = new TextBox();
                txt.ID = ClientID;
                txt.Text = "";
                PlaceHolder1.Controls.Add(txt);
                Label lb2 = new Label();
                lb2.Text = "字段";
                lb2.ID = ClientID;
                PlaceHolder1.Controls.Add(lb2);
                DropDownList drplist = new DropDownList();
                drplist.ID = ClientID;
                foreach (string item in drpItems)
                {
                    drplist.Items.Add(item);
                }
                PlaceHolder1.Controls.Add(drplist);
                Literal lt1 = new Literal();
                lt1.ID = ClientID;
                lt1.Text = "<br/>";
                PlaceHolder1.Controls.Add(lt1);
            }
        }
               
    }

调用:
C# code
Control plh = UcCreatTable1.FindControl("PlaceHolder1");
            Control txt = UcCreatTable1.FindControl("txtTableName");
           Label1.Text= plh.GetType().ToString();
           Label2.Text = txt.ID.ToString();


            //三层调用插入数据没问题,现在就是不能获取数据
           foreach (Control ctl in plh.Controls)
           {
               Field field1 = new Field();
               field1.Field_TableName = ((TextBox)txt).Text;
               Literal1.Text = ((TextBox)txt).Text.ToString();
               if (ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") && ctl.ID != null)
               {

                   field1.Field_FieldName = ((TextBox)ctl).Text;
                   Literal2.Text = ((TextBox)ctl).Text.ToString();
               }
               if (ctl.GetType().ToString().Equals("System.Web.UI.WebControls.Lable") && ctl.ID != null)
               {
                   field1.Field_Type = ((DropDownList)ctl).Text;
                   Literal3.Text = ((DropDownList)ctl).Text.ToString();
               }
               new FieldBll().AddNews(field1);

           }
        }

如果直接写上数据,插入没问题,但是现在好像不能获取数据