asp.net 动态加载用户控件注意
asp.net 动态加载用户控件注意
在asp.net 动态加载用户控件时要注意:
在没加载成功是不能对它设置属性的,以免带来不别要的错误!!
代码如下:
Default.aspx
![]()
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Drawing;
11
public partial class _Default : System.Web.UI.Page
12
{
13![]()
14![]()
15![]()
16
protected void Page_Load(object sender, EventArgs e)
17
{
18![]()
19
20
Control control = LoadControl("~/myControl.ascx"); //加载用户控件
21
this.Panel1.Controls.Add(control); //把它添加到该面板中
22![]()
23
24
myControl myC = control as myControl; //获得实例
25
if (myC == null) //是否用户控件加载成功
26
{
27
PartialCachingControl pcc = control as PartialCachingControl;
28
if (pcc != null) myC = pcc.CachedControl as myControl;
29
}
30
if (myC != null) myC.BackColor = Color.Yellow; //成功设置该控件的样式
31![]()
32![]()
33
}
34
}
35
myControl.ascx
![]()
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Drawing;
12![]()
13
public partial class myControl : System.Web.UI.UserControl
14
{
15
public Color BackColor
16
{
17
get { return TextBox1.BackColor; }
18
set { TextBox1.BackColor = value; }
19
}
20![]()
21
protected void Page_Load(object sender, EventArgs e)
22
{
23
TextBox1.Text = DateTime.Now.ToLongTimeString();
24
}
25
26
protected void Button1_Click(object sender, EventArgs e)
27
{
28
this.TextBox2.Text = this.TextBox1.Text;
29
}
30
}
31![]()
在没加载成功是不能对它设置属性的,以免带来不别要的错误!!
代码如下:
Default.aspx
1
using System;2
using System.Data;3
using System.Configuration;4
using System.Web;5
using System.Web.Security;6
using System.Web.UI;7
using System.Web.UI.WebControls;8
using System.Web.UI.WebControls.WebParts;9
using System.Web.UI.HtmlControls;10
using System.Drawing;11
public partial class _Default : System.Web.UI.Page 12
{13

14

15

16
protected void Page_Load(object sender, EventArgs e)17
{18

19
20
Control control = LoadControl("~/myControl.ascx"); //加载用户控件21
this.Panel1.Controls.Add(control); //把它添加到该面板中22

23
24
myControl myC = control as myControl; //获得实例25
if (myC == null) //是否用户控件加载成功26
{27
PartialCachingControl pcc = control as PartialCachingControl;28
if (pcc != null) myC = pcc.CachedControl as myControl;29
}30
if (myC != null) myC.BackColor = Color.Yellow; //成功设置该控件的样式31

32

33
}34
}35

1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11
using System.Drawing;12

13
public partial class myControl : System.Web.UI.UserControl14
{15
public Color BackColor16
{17
get { return TextBox1.BackColor; }18
set { TextBox1.BackColor = value; }19
}20

21
protected void Page_Load(object sender, EventArgs e)22
{23
TextBox1.Text = DateTime.Now.ToLongTimeString();24
}25
26
protected void Button1_Click(object sender, EventArgs e)27
{28
this.TextBox2.Text = this.TextBox1.Text;29
}30
}31



浙公网安备 33010602011771号