zoukankan
html css js c++ java
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
查看全文
相关阅读:
Stepping Number
Replace String
String Permutation
Clock Angle
Keypad Permutation
Replace Words
1、奉加微 PHY6202 Get started
Python3 修改二进制文件
Google Fast Pair
python 校验 BLE resolvable private address
原文地址:https://www.cnblogs.com/cfam/p/563795.html
最新文章
并查集 专题 之 银河战舰的奇妙距离
并查集 专题 之 虚空节点之谜
树状数组——POJ2352
滑雪与时间胶囊 题解 BZOJ2753
题和题解:最"大"路
区间DP模式
House Robber
Happy Number
Remove Linked List Elements
Count Primes
热门文章
Isomorphic Strings
Reverse Linked List
Contains Duplicate
Contains Duplicate II
Longest Common Prefix
Roman to Integer
Battery (Coin Change)
Count And Say
Colorful Number
Spiral Matrix
Copyright © 2011-2022 走看看