vs2005 web application页面动态添加控件获取属性值

时间:2022-07-30 00:56:38

我在使用web site做测试时,动态添加控件时分配一个ID号,在服务器 端按ID号就可以用Request.Form[ID]来获取,可是在web application里却怎么也不行了,原因是自动加了一个ClientID,ID号就变了,我不知道怎样使用我给加的那个,找了好些资料,也是没有结果,没办法,只有用Request.Form集合来取值了,代码如下:

 BinGuan b = new BinGuan();
            b.ListInfo(userid);

            DateTime dt = new DateTime();
            for (int i = 0; i < 12; i++)
            {
                string str = "";
                if (i > 0) str = "<br>";
                TimeSpan t1 = new TimeSpan(i, 0, 0, 0);
                dt = DateTime.Now + t1;
                Label l = new Label();
                l.Text = str + dt.ToShortDateString() + " ";
                p1.Controls.Add(l);

                TextBox txt = new TextBox();
                string cid = "txt";
                if (i < 10)
                {
                    cid += "0" + i.ToString();
                }
                else
                {
                    cid += i.ToString();
                }
               
                txt.ID = cid;
                txt.Width = 40;

                int nums = 0;
                try
                {
                    nums =int.Parse( b.Dt_bg.Rows[i]["nums"].ToString());
                }
                catch
                {
                }

                if (nums != 0)
                {
                    txt.Text = nums.ToString();
                }
                else
                {
                    txt.Text = "";
                }
                p1.Controls.Add(txt);
            }

            b = null;

以上代码是添加控件的,控件的容器是一个panel控件,名称为p1

以下代码是获取值的,因以上的控件是按循环分配的ID号,因此获取此同样按循环先取ID

NameValueCollection coll = Request.Form;
            for (int i = 0; i < coll.Keys.Count; i++)
            {
                DateTime dt = new DateTime();
                for (int j = 0; j < 12; j++)
                {
                    string cid = "txt";
                    if (j < 10)
                    {
                        cid += "0" + j.ToString();
                    }
                    else
                    {
                        cid += j.ToString();
                    }
                    if (coll.AllKeys[i].ToString().IndexOf(cid) >= 0)
                    {
                        int nums = 0;
                        try
                        {
                            nums = int.Parse(Request.Form[i].ToString());
                        }
                        catch
                        { }
                        TimeSpan t1 = new TimeSpan(j, 0, 0, 0);
                        dt = DateTime.Now + t1;
                        Response.Write(coll.AllKeys[i].ToString()+" "+nums.ToString()+" "+dt.ToShortDateString() + "<br>");

                        BinGuan b = new BinGuan();
                        b.UpdateChuangWei(Session["user"].ToString(), dt.ToShortDateString(), nums);
                        b = null;
                    }
                }

            }

这种方法实现了动态批量添加控件,并在添加时根据从数据库中提取的值对控件赋值,在服务器端接收控件的值并且添加到数据库中,当然,相应的存储过程也是需要先写好的。