码迷,mamicode.com
首页 > 其他好文 > 详细

动态生成服务器控件

时间:2016-05-17 08:35:40      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

public partial class _Default : System.Web.UI.Page
{
 
 
 
    private static Dictionary<string ,string> di=new Dictionary<string ,string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        //自动生成刷新页面前的动态控件
        if (!this.IsPostBack)
        {
            ViewState.Add("count", 0);
            di.Clear();
        }
        else
        {
            int i=0;
            foreach (KeyValuePair<string, string> pair in di)
            {
                if(pair.Key=="TextBox")
                {
                    this.PlaceHolder2.Controls.Add(new LiteralControl("TextBox" + i + " :"));
                     TextBox tb = new TextBox();
                    tb.ID = "TextBox" + i;
                    this.PlaceHolder2.Controls.Add(tb);
                    i++;
                }
                else               
                {
                    this.PlaceHolder2.Controls.Add(new LiteralControl("DropDownList" + i+ " :"));
                    DropDownList ddl = new DropDownList();
                    ListItem li = new ListItem("123", "123");
                    ddl.Items.Add(li);
                    this.PlaceHolder2.Controls.Add(ddl);
                    i++;
                }
            }

 

        }

    }
    private void CreateControl(int number)
    {

 


    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int Max = 0;
        for (int i = 0; i < Int32.Parse(ViewState["count"].ToString()); i++)
        {
            Max += Int32.Parse((this.FindControl("Textbox" + i) as TextBox).Text);
        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.PlaceHolder2.Controls.Add(new LiteralControl("TextBox" + ViewState["count"].ToString() + " :"));
        TextBox tb = new TextBox();
        tb.ID = "TextBox" + ViewState["count"].ToString();
        this.PlaceHolder2.Controls.Add(tb);
        ViewState["count"] = Int32.Parse(ViewState["count"].ToString()) + 1;
        di.Add("TextBox", ViewState["count"].ToString());

        this.PlaceHolder2.Controls.Add(new LiteralControl("DropDownList" + ViewState["count"].ToString() + " :"));
         DropDownList ddl = new DropDownList();
         ListItem li = new ListItem("123","123");
         ddl.Items.Add(li);
         ddl.ID = "DropDownList" + ViewState["count"].ToString();
         this.PlaceHolder2.Controls.Add(ddl);
         ViewState["count"] = Int32.Parse(ViewState["count"].ToString()) + 1;
         di.Add("DropDownList", ViewState["count"].ToString());

    }

动态生成服务器控件

标签:

原文地址:http://www.cnblogs.com/yidianfeng/p/5500203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!