for (int i = 0; i < j; i++)
        {
            DropDownList t = new DropDownList();
            t.ID = "a" + i.ToString();
            t.DataSource = source;
            t.DataTextField = "ppositions";
            t.DataBind();
            t.Height = 25;
            TextBox d = new TextBox();
            d.ID = "b" + i.ToString();
            d.Width = 40;
            d.Height = 20;
            TextBox c = new TextBox();
            c.ID = "c" + i.ToString();
            c.Width = 40;
            c.Height = 20;
            TextBox f = new TextBox();
            f.ID = "f" + i.ToString();
            f.Width = 40;
            RangeValidator r = new RangeValidator();
            r.ID = "r" + i.ToString();
            r.ControlToValidate = "f" + i.ToString();
            r.ErrorMessage="只能输入数字";
            //r.Type = "Integer";
            this.Panel1.Controls.Add(t);
            this.Panel2.Controls.Add(d);
            this.Panel3.Controls.Add(c);
            this.Panel4.Controls.Add(f);
        }
添加控件是成功了,可老是取不到值,我要做的是往数据库里填数据的模块,谁知道怎么取动态生成控件的值啊? 尽量详细些  最好给个例子看看  感激不尽

解决方案 »

  1.   

    这是个老问题了 ,我也在网上找过答案,但没改出来,这些动态添加的控件是不是不在PAGE上,根本找不到
      

  2.   

    给控件注册一个事件,绑定到自己写的方法上。
    然后方法通过事件处理方法的 object sender 参数获取控件的任何值
      

  3.   

    给一段代码给你参考下哦..
    不知道是不是你要的那个:
    protected void Page_Load(object sender, EventArgs e)
        {
            
            DropDownList drp;
            TextBox txt;
            Label lbl;
            for (int i = 0; i < 4; i++)
            {
                drp = new DropDownList();
                drp.ID = "drp" + i.ToString();
                drp.Width = 120;
                            txt = new TextBox();
                txt.ID = "txt" + i.ToString();
                txt.Width = 120;            lbl = new Label();
                lbl.ID = "lbl" + i.ToString();
                lbl.Width = 120;            this.form1.Controls.Add(drp);
                this.form1.Controls.Add(txt);
                this.form1.Controls.Add(lbl);
                
            }
            if (!IsPostBack)
                bindDrp0();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            DropDownList drp0=(DropDownList) this.Page.FindControl("drp0");
            Label lbl0 = (Label)this.Page.FindControl("lbl0");
            lbl0.Text = drp0.SelectedValue;
        }    private void bindDrp0()
        {
            for (int i = 0;i < 4; i++ )
            {
                DropDownList drp0 = (DropDownList)this.Page.FindControl("drp0");
                drp0.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        }
      

  4.   

    我也会是这样写的,查找控件就是找不到,苍天啊,我搞了1天半了
    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;
    using System.Collections.Generic;
    using OAManagerBLL;
    using OAManagerModel;
    using System.Drawing;public partial class CheckSet : System.Web.UI.Page
    {
        private CheckTimeManager ctm = new CheckTimeManager();
        private List<CheckTime> list = null;
        int listcount = 0;    protected void Page_Load(object sender, EventArgs e)
        {        if (!Page.IsPostBack)
            {
                csDivb.Visible = false;
            }
            CreateList();
        }    //选择日期段,插入数据库并查询出来
        protected void btnSet_Click(object sender, EventArgs e)
        {
            string start = this.txtStart.Text.Trim();
            string end = this.txtEnd.Text.Trim();        if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
            {
                CheckTime ctStart = new CheckTime(start);
                CheckTime ctEnd = new CheckTime(end);
                ctm.AddDates(ctStart, ctEnd);
                list = ctm.GetSomeCheckTime(ctStart, ctEnd);
                if (list != null)
                {
                    listcount = list.Count;
                    ViewState["listcount"] = listcount;
                }
            }
            CreateList();
        }    //动态创建表格行列和lable
        protected void CreateList()
        {
            csDivb.Visible = true;
            Label lbl = null;
            TableRow tr = null;
            TableCell tc = null;        int rs = listcount / 7 + 1;
            string str = "";        tr = new TableRow();
            tr.Cells.Clear();        string[] strings = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
            for (int i = 0; i < strings.Length; i++)
            {
                lbl = new Label();
                tc = new TableCell();
                lbl.Text = strings[i];
                tc.Controls.Add(lbl);
                tr.Cells.Add(tc);
                tc.HorizontalAlign = HorizontalAlign.Center;
                tr.VerticalAlign = VerticalAlign.Middle;
                cstable.Rows.Add(tr);
            }        int n = 0;
            for (int row = 1; row <= rs; row++)
            {
                tr = new TableRow();
                for (int cs = row * 7; cs < (row + 1) * 7; cs++)
                {
                    tc = new TableCell();
                    if (n < listcount)
                    {
                        if (n == 0)
                        {
                            str = list[0].CheckDate.Trim();
                            DateTime dt = Convert.ToDateTime(str);
                            int s = int.Parse(dt.DayOfWeek.ToString("d"));                        if (s == 0)
                            {
                                s = 7;
                            }                        for (int i = 0; i < s; i++)
                            {
                                tc = new TableCell();
                                tc.BackColor = Color.White;
                                tr.Cells.Add(tc);
                                cstable.Rows.Add(tr);
                            }
                            cs = cs + s - 1;
                        }
                        str = list[n].CheckDate.Trim();
                        lbl = new Label();
                        lbl.ID = "lbl" + n.ToString();
                        lbl.Text = str.Substring(0, str.IndexOf(" "));
                        lbl.EnableViewState = true;
                        tc.BackColor = Color.GreenYellow;
                        tc.Controls.Add(lbl);
                        tc.HorizontalAlign = HorizontalAlign.Center;
                        n++;
                    }
                    lbl.Attributes.Add("onClick", "cellClick(this)");//点击改变lable背景颜色
                    tr.Cells.Add(tc);
                }
                cstable.Rows.Add(tr);
                cstable.EnableViewState = true;
                csDivb.EnableViewState = true;
            }    }    //获取背景颜色,根据颜色判定他是休息日还是工作日
        protected void btnOver_Click(object sender, EventArgs e)
        {
            csDivb.Visible = false;
            int i = Convert.ToInt32(ViewState["listcount"]);
            for (int m = 0; m < i; m++)
            {
                Label lb = this.Page.FindControl("lbl" + m.ToString()) as Label;
                if (lb.BackColor == Color.GreenYellow)
                {
                    CheckTime ct = new CheckTime();//日期类
                    ct.CheckDate = lb.Text.Trim();
                    ct.TimeState = new TimeState(1);
                }
                else
                {
                    if (!string.IsNullOrEmpty(lb.Text.Trim()))
                    {
                        CheckTime ct = new CheckTime();
                        ct.CheckDate = lb.Text.Trim();
                        ct.TimeState = new TimeState(2);
                    }
                }
            }
        }
    }
      

  5.   

    http://www.360doc.com/content/10/0326/17/466494_20367875.shtml
    这个不知道对你有没有用 ,我也还没试过