博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【ASP.Net】使用自定义服务器控件
阅读量:7040 次
发布时间:2019-06-28

本文共 1632 字,大约阅读时间需要 5 分钟。

在Visual studio 2015中可以通过Add-->New Item,添加Web Forms Server Control,自动生成的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using 
System;
using 
System.Collections.Generic;
using 
System.ComponentModel;
using 
System.Linq;
using 
System.Text;
using 
System.Threading.Tasks;
using 
System.Web;
using 
System.Web.UI;
using 
System.Web.UI.WebControls;
 
namespace 
WebApplication23
{
    
[DefaultProperty(
"Text"
)]
    
[ToolboxData(
"<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>"
)]
    
public 
class 
WebCustomControl1 : WebControl
    
{
        
[Bindable(
true
)]
        
[Category(
"Appearance"
)]
        
[DefaultValue(
""
)]
        
[Localizable(
true
)]
        
public 
string 
Text
        
{
            
get
            
{
                
String s = (String)ViewState[
"Text"
];
                
return 
((s == 
null
) ? String.Empty : s);
            
}
 
            
set
            
{
                
ViewState[
"Text"
] = value;
            
}
        
}
 
        
protected 
override 
void 
RenderContents(HtmlTextWriter output)
        
{
            
output.Write(Text);
        
}
    
}
}

在Web Page(即在aspx文件)中引用该Server Control,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ Page Language=
"C#" 
AutoEventWireup=
"true" 
CodeBehind=
"WebForm1.aspx.cs" 
Inherits=
"WebApplication23.WebForm1" 
%>
<%@ Register Assembly=
"WebApplication23"  
Namespace=
"WebApplication23" 
TagPrefix=
"ccl" 
%>
 
<!DOCTYPE html>
 
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
    
<title></title>
</head>
<body>
    
<form id=
"form1" 
runat=
"server"
>
    
<div>
      
<ccl:WebCustomControl1 ID=
"ServerControl1" 
runat=
"server" 
Text=
"daniel_test" 
/>
    
</div>
    
</form>
</body>
</html>

在使用Register引用该Server Control后,在ToolBox中就会出现该自定义的服务器控件,如图:

参考链接:

      本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1743721,如需转载请自行联系原作者
你可能感兴趣的文章
dedeCMS php语法在模版中的应用
查看>>
sublime 安装ctag 实现函数跳转
查看>>
sshd问题:A protocol error occurred. Change of username or service not allowed
查看>>
jQuery开发者眼中的AngularJS
查看>>
【DAY9】 关于多线程熊吃蜜Demo1的作业实验
查看>>
Python实现多属性排序
查看>>
nginx 访问日志分析
查看>>
RabbitMQ之消息确认机制(事务+Confirm)
查看>>
给出一个数组,计算数组中少了哪个数据的实现
查看>>
USB-232卡 配置
查看>>
C#窗体程序皮肤设置
查看>>
T-SQL.字符串函数
查看>>
mysql慢查询
查看>>
offices文件打开乱码问题如何处理
查看>>
抓屏程序
查看>>
many-to-many出现的问题
查看>>
第5章 配置邮箱服务
查看>>
node.js的一个简单框架
查看>>
PPT如何保存还原已剪裁图片的原始版本
查看>>
lnmp一键安装之-php
查看>>