从用户控件(ASCX)变更网页(ASPX)一些值
问题与要求是这样的。网页ASPX有一个Label,此也是TextBox的Title。
还有几个用户控件ASCX,每个ASCX都有一个属性。网页会根据实际情况的需要而去动态加载这些用户控件。在加载时,网页ASPX的TextBox的Title的Label的文字会根据加载的用户控件的属性而改变。写得有点呦口。
下面Insus.NET在几个用户控件中,只列写一个,因为语法一样,只是属性值不一样。
UserControlA
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserControlA : System.Web.UI.UserControl
{
private string _TextBoxTitle;
public string TextBoxTitle
{
get { return _TextBoxTitle; }
set { _TextBoxTitle = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
this._TextBoxTitle = "Name";
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserControlA : System.Web.UI.UserControl
{
private string _TextBoxTitle;
public string TextBoxTitle
{
get { return _TextBoxTitle; }
set { _TextBoxTitle = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
this._TextBoxTitle = "Name";
}
}
当用户控件动态加载至aspx之后,运行:
在aspx网页,动态把用户控件ascx的属性赋值给Label。
View Code
protected void Page_Load(object sender, EventArgs e)
{
this.UserControlA1.PreRender += new EventHandler(UserControlA1_PreRender);
}
void UserControlA1_PreRender(object sender, EventArgs e)
{
string strLabel = this.UserControlA1.TextBoxTitle;
this.Label1.Text = strLabel;
}
{
this.UserControlA1.PreRender += new EventHandler(UserControlA1_PreRender);
}
void UserControlA1_PreRender(object sender, EventArgs e)
{
string strLabel = this.UserControlA1.TextBoxTitle;
this.Label1.Text = strLabel;
}