向动态加载的UserControl控件传递参数,用该参数初始化控件中的值
代码如下:
ASPX页面中一个PlaceHolder控件和一个Button控件
CS代码:
public partial class _Default : System.Web.UI.Page { protected Control ctl; string str1; protected void Page_Load(object sender, EventArgs e) { if(IsPostBack) { if (Session["System"] != null) str1 = Session["System"].ToString(); if (ViewState["isLoadControl"] != null && ((bool)ViewState["isLoadControl"]))//再次加载 { this.ctl = this.LoadControl(str1);//加载控件 this.ctl.ID = "TEST"; this.PlaceHolder1.Controls.Add(this.ctl); //在页面上呈现 } } } protected void Button1_Click(object sender, EventArgs e) { BlindHolder("test/test_2.ascx"); } public void BlindHolder(string str) { PlaceHolder1.Controls.Clear(); if (this.PlaceHolder1.Controls.Count == 0) //第一次加载 { this.ctl = this.LoadControl(str); this.ctl.ID = "TEST"; this.PlaceHolder1.Controls.Add(this.ctl); } ViewState["isLoadControl"] = true; Session["System"] = str; }}
public partial class test_test_2 : System.Web.UI.UserControl{ string str; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = "TEST2测试成功!"; }}