设为首页
优惠IDC
收藏本站
六狼博客
六狼论坛
开启辅助访问
切换到窄版
用户名
Email
自动登录
找回密码
密码
登录
立即注册
只需一步,快速开始
只需一步,快速开始
快捷导航
门户
首页
BBS
云计算
大数据
手机
移动开发android,ios,windows phone,windows mobile
编程
编程技术java,php,python,delphi,ruby,c,c++
前端
WEB前端htmlcss,javascript,jquery,html5
数据库
数据库开发Access,mysql,oracle,sql server,MongoDB
系统
操作系统windows,linux,unix,os,RedHat,tomcat
架构
项目管理
软件设计,架构设计,面向对象,设计模式,项目管理
企业
服务
运维实战
神马
搜索
搜索
热搜:
php
java
python
ruby
hadoop
sphinx
solr
ios
android
windows
centos
本版
帖子
用户
六狼论坛
»
首页
›
WEB前端
›
Html/Css
›
将UserControl生成Html代码 (thanks to Jeffrey Zhao ) ...
返回列表
查看:
105
|
回复:
0
将UserControl生成Html代码 (thanks to Jeffrey Zhao )
[复制链接]
heros
heros
当前离线
积分
57
窥视卡
雷达卡
升级
4.67%
当前用户组为
秀才
当前积分为
57
, 升到下一级还需要 143 点。
15
主题
15
主题
15
主题
秀才
秀才, 积分 57, 距离下一级还需 143 积分
秀才, 积分 57, 距离下一级还需 143 积分
积分
57
发消息
楼主
|
发表于 2013-2-7 21:01:33
|
显示全部楼层
|
阅读模式
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Web;
5
using
System.Web.UI;
6
using
System.IO;
7
using
System.Web.UI.HtmlControls;
8
using
System.Reflection;
9
10
/**/
///
<summary>
11
///
UserControlRender 的摘要说明
12
///
</summary>
13
namespace
System.Web.UI
14
...
{
15
public
class
UserControlRender
16
...
{
17
private
Page _thisPage;
18
public
UserControlRender()
19
...
{
20
//
21
//
TODO: 在此处添加构造函数逻辑
22
//
23
}
24
25
/**/
///
<summary>
26
///
加载指定虑拟路径控件
27
///
</summary>
28
///
<param name="path">
控件路径
</param>
29
///
<returns>
返回控件对象
</returns>
30
private
UserControl LoadUserControl(
string
path)
31
...
{
32
_thisPage
=
new
MyPage();
33
Control control
=
_thisPage.LoadControl(path);
34
return
(UserControl)control;
35
}
36
37
/**/
///
<summary>
38
///
渲染用户控件并生成代码
39
///
</summary>
40
///
<param name="control">
目标控件对象
</param>
41
///
<returns></returns>
42
private
string
RenderControlView(UserControl control)
43
...
{
44
AddRangeMark(control);
45
46
StringWriter writer
=
new
StringWriter();
47
this
._thisPage.Form.Controls.Add(control);
48
this
._thisPage.Server.Execute(
this
._thisPage, writer,
false
);
49
50
return
RemoveMarkFromControlText(writer.ToString());
51
}
52
53
/**/
///
<summary>
54
///
为用户控件内容头尾打上标记
55
///
</summary>
56
///
<param name="control">
要渲染的控件
</param>
57
private
void
AddRangeMark(UserControl control)
58
...
{
59
LiteralControl liStart
=
new
LiteralControl();
60
LiteralControl liEnd
=
new
LiteralControl();
61
62
liStart.Text
=
"
<start/>
"
;
63
liEnd.Text
=
"
<end/>
"
;
64
65
control.Controls.AddAt(
0
, liStart);
66
control.Controls.Add(liEnd);
67
}
68
69
/**/
///
<summary>
70
///
将生成的字符串中开始和结束标记去除
71
///
</summary>
72
///
<param name="content">
控件生成的代码
</param>
73
///
<returns>
去除了开始结束标记后的代码
</returns>
74
private
string
RemoveMarkFromControlText(
string
content)
75
...
{
76
content
=
content.Substring(content.IndexOf(
"
<start/>
"
)
+
9
);
77
content
=
content.Substring(
0
, content.IndexOf(
"
<end/>
"
));
78
return
content;
79
}
80
81
public
string
RenderControlView(
string
path)
82
...
{
83
return
RenderControlView(LoadUserControl(path));
84
}
85
}
86
87
public
class
MyPage : Page
88
...
{
89
HtmlForm form;
90
public
MyPage()
91
...
{
92
if
(
this
.Form
==
null
)
93
...
{
94
form
=
new
HtmlForm();
95
this
.Controls.Add(form);
96
foreach
(MemberInfo item
in
typeof
(Page).FindMembers(MemberTypes.Field, BindingFlags.NonPublic
|
BindingFlags.Instance,
null
,
null
))
//
查找Page中的_form字段,并将创建的form对象赋值给它
97
...
{
98
if
(item.Name
==
"
_form
"
)
99
...
{
100
((FieldInfo)item).SetValue(
this
, form);
101
break
;
102
}
103
}
104
}
105
else
106
...
{
107
form
=
this
.Form;
108
}
109
}
110
111
}
112
}
113
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
千斤顶
显身卡
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
本版积分规则
发表回复
回帖后跳转到最后一页
Copyright © 2008-2020
六狼论坛
(http://it.6wolf.com) 版权所有 All Rights Reserved.
Powered by
Discuz!
X3.4
京ICP备14020293号-2
本网站内容均收集于互联网,如有问题请联系
QQ:389897944
予以删除
快速回复
返回顶部
返回列表