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
查看全文
相关阅读:
SpringCloud系列——SSO 单点登录
SpringBoot系列——Redis
基于“formData批量上传的多种实现” 的多图片预览、上传的多种实现
SpringCloud系列——Bus 消息总线
SpringCloud系列——Config 配置中心
SpringCloud系列——Zuul 动态路由
SpringCloud系列——Ribbon 负载均衡
SpringCloud系列——Feign 服务调用
SpringCloud系列——Eureka 服务注册与发现
ZXing 生成、读取二维码(带logo)
原文地址:https://www.cnblogs.com/cfam/p/563795.html
最新文章
Eclipse中web项目部署至Tomcat步骤
Extjs4 store load 有中文字符提交后台乱码解决方法
ExtJs控件属性配置详细
extJs常用的四种Ajax异步提交
Java日期时间使用总结
ExtJs 中Viewport的介绍与使用
使用pl/sql的文本导入器时如何设置主键自增长
tomcat 防火墙如何设置
Tomcat服务器的安装配置图文教程(推荐)
Tomcat7.0安装配置详细(图文)
热门文章
github子目录下载
【openfire集群】集群插件hazelcast配置
【openfire】ubuntu上安装配置
【转载】聊天数据库表设计
openfire插件删除
smack_用户的基本操作crud
openfire配置中常见错误
【转载】openfire插件开发一helloWorld(亲测可用)
【转载】MyEclipse搭建openfire4.1.3开发环境(亲试可用)
【转载】Web.xml配置详解
Copyright © 2011-2022 走看看