switch (field_type)
       {
           case "datetime":
              c1 = LoadControl(Request.ApplicationPath + "/UserControl/dateControl.ascx");
              ctb.Controls.Add(c1);
              dateControl dttb = (dateControl)c1; //强制转换
              dateControl().stringLbValue(field_title); //Label赋值
              break;
           case "text":
             //..........其它自定义控件..........
       }自定义控件里有Label、TextBox  在修改页面时如何给这些自定义控件赋值、取值、如何确定哪个自定义控件中的TextBox对应相应的值 ,例子 例子!!高手帮忙、先谢谢!!

解决方案 »

  1.   

    switch (field_type)
           {
               case "datetime":
                  c1 = LoadControl(Request.ApplicationPath + "/UserControl/dateControl.ascx");
                  ctb.Controls.Add(c1);
                  dateControl dttb = (dateControl)c1; //强制转换
                    dttb.stringLbValue(field_title); //Label赋值
                  break;
               case "text":
                 //..........其它自定义控件..........
           }
      

  2.   


    要定义控件的属性,也就是get 和 set
    [code=C#]
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class Controls_DatePicker : System.Web.UI.UserControl
    {
        private string _Year, _Month, _Day,_text;
        private int _MinYear, _MaxYear;
        private string _style = "ymd";  //控件显示样式
        const int MAXYEAR = 2015;   //默认年份
        const int MINYEAR = 1990;
        
        #region 设置或返回此控件的值
        /// <summary>
        /// 设置或返回此控件的值
        /// </summary>
        public string Text
        {
            get
            {
                string temp = "";
                if (dwDay.Visible)
                { temp= this.dwYear.Text + "-" + this.dwMonth.Text + "-" + this.dwDay.Text; }
                else
                {
                    if (dwMonth.Visible)
                    {
                        temp= this.dwYear.Text + "-" + this.dwMonth.Text + "-01";
                    }
                    else
                    {
                        temp= dwYear.Text + "-01-01";
                    }
                }
                return temp;
                //if (Galsun.InputCheck.IsTrueDate(temp))
                //{
                //    return temp;
                //}
                //else
                //{
                //    Response.Write("<script>alert('不是有效的日期!');</script>");
                //    return "2007-1-1";
                //}
            }
            set
            {
                _text = value;
                setText(value);
                
            }
        }
        private void setText(string value)
        {
            string date = DateTime.Parse(value).ToString("yyyy-MM-dd");
            _Year = date.Substring(0, 4);
            _Month = date.Substring(5, 2);
            _Day = date.Substring(8, 2);
            this.dwYear.Text = _Year;
            this.dwMonth.Text = _Month;
            this.dwDay.Text = _Day;
        }
        #endregion    #region 返回年份
        /// <summary>
        /// 返回年份
        /// </summary>
        public string Year
        {
            get
            {
                return dwYear.Text;
            }
            set
            {
                dwYear.Text  = value;
            }
        }
        #endregion    #region 设置控件显示样式
        /// <summary>
        /// 设置控件显示样式,如ym ,则只显示年月
        /// </summary>
        public string ShowStyle
        {
            set
            {
                _style = value;
            }    }
        #endregion    
        
        #region 最小年份
        /// <summary>
        /// 最小年份,默认为1990年
        /// </summary>
        public int MinYear
        {
            set
            {
                _MinYear = value;
            }
        }
        #endregion    #region 最大年份
        /// <summary>
        /// 最大年份,默义为2015年
        /// </summary>
        public int MaxYear
        {
            set
            {
                _MaxYear = value;
            }
        }
        #endregion    
        #region 加载
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //设置显示年份
                int maxYear, minYear;
                maxYear = _MaxYear == 0 ? MAXYEAR : _MaxYear;
                minYear = _MinYear == 0 ? MINYEAR : _MinYear;
                if (!IsPostBack)
                {
                    for (int i = minYear; i <= maxYear; i++)
                    {
                        dwYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                    }
                }
                //显示月
                for (int i = 1; i <= 12; i++)
                {
                    string month = i.ToString().Length == 1 ? "0" + i.ToString() : i.ToString();
                    dwMonth.Items.Add(new ListItem(month, month));
                }
                //显示月
                for (int i = 1; i <= 31; i++)
                {
                    string day = i.ToString().Length == 1 ? "0" + i.ToString() : i.ToString();
                    dwDay.Items.Add(new ListItem(day, day));
                }
                //显示格式            if (_style.IndexOf('y') != -1) 
                    dwYear.Visible = true;
                else
                    dwYear.Visible = false;
                if (_style.IndexOf('m') != -1)
                    dwMonth.Visible = true;
                else
                    dwMonth.Visible = false;
                if (_style.IndexOf('d') != -1)
                    dwDay.Visible = true;
                else
                    dwDay.Visible = false;            if (_text == null)
                {
                    _text = DateTime.Now.Date.ToString();
                }
                
                setText(_text);
            }
        }
        #endregion
        
    }
    [/code]
      

  3.   

    不是我想要的结果哦,这个CSDN怎么搞的 老提示没登陆
      

  4.   

    给你的自定义控件设置个属性public String XValue
    {
        get{  return textbox1.text;}
    }
    然后获取这个属性就可以了。
      

  5.   

    之前不是建议你使用属性么? 属性可以设置成对 label 、textbox 的text赋值 也可以设置成对 label 、textbox 本身的获取现在遇到什么问题了
      

  6.   

    c1 = LoadControl(Request.ApplicationPath + "/UserControl/textBoxControl.ascx");
                ctb.Controls.Add(c1);
                textBoxControl tb = (textBoxControl)c1;
    tb.labelValue ="aaaaa"
    但是取控件里面的值就取不上了str=tb.labelValue;一直是空。取的不是页面上带的自定义,好像是新增了个 所以一直取的空 该如何搞呢???
    我控件里面写的public string textboxValue
            {
                get { return TextBox1.Text; }
                set { TextBox1.Text = value; }
            }
            public string labelValue
            {
                get { return Label1.Text; }
                set { Label1.Text = value; }
            }
      

  7.   

    你页面上有几个textBoxControl 调用对应的textBoxControl ID.labelValue
      

  8.   

    textBoxControl  根据数据字段判断的。。不一定多少 
    而且从数据库里判断显示那个自定义控件
      

  9.   

    c1 = LoadControl(Request.ApplicationPath + "/UserControl/textBoxControl.ascx");
    textBoxControl tb = (textBoxControl)c1;
    tb.labelValue ="aaaaa"
    ctb.Controls.Add(c1);
    先附值,再加载试试
      

  10.   

    我猜你取值时是这么写的c1 = LoadControl(Request.ApplicationPath + "/UserControl/textBoxControl.ascx");
                textBoxControl tb = (textBoxControl)c1;
    string value=tb.labelValue;
      

  11.   

    你应该在添加的时候设置IDeg:
    c1 = LoadControl(Request.ApplicationPath + "/UserControl/textBoxControl.ascx");
    c1.ID="";//这个可以和你数据库中的记录挂钩,这样也便于查找对应Control
    ctb.Controls.Add(c1);
    然后在取值时使用 FindControl(id)  eg:c1=FindControl(id);
    textBoxControl tb = (textBoxControl)c1;
    string value=tb.labelValue;ps: FindControl 这个方法效率很低下,你可以自己写个遍历指定控件子元素的方法我只是用FindControl来说明思路