关于.net的自定义控件(请各位大虾指正)
2024-07-10 12:58:12
供稿:网友
关于.net的自定义控件
一:谈到自定义控件,就不得不说@register(这玩艺具体怎么翻译我也不知道,只好用e文,嘿嘿)。
[email protected]语法格式:
<%@register tagprefix="tagprefix" namespace="namespace" assembly="assembly"%> or
<%@register tagprefix="tagprefix" tagname="tagname" src="pathname"%>
2.属性:
tagprefix:把别名和名称空间连接在一起
tagname:把类名和名称空间连接在一起
namespace:哈哈,顾名思义把名称空间和tagprefix连接在一起
src:用户自定义控件的路径
assembly:我们与tagprefix联系的名称
注:assembly的名称不能包含已存在的文件名
(e文好的可看随机文档)
二:自定义控件的建立(.ascx)
.net的自定义控件和asp里的.inc文件非常相似(包括功能也是),我们可以在里面使用html,更可以
连接数据库等等,哎我怎么越说越不明白,还是看看例子吧:
第一个例子只是html文件:
header.ascx
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tr>
<td colspan="2" background="images/grid_background.gif" nowrap>
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tr>
<td colspan="2">
<img src=http://www.163design.net/n/a/"images/most_secretive_place.gif">
</td>
<td align="right" nowrap>
<table cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
<td align="center" width="65">
<a href="login.aspx" class="sitelinkbold"><img src=http://www.163design.net/n/a/"images/sign_in.gif" border="0">
sign in</a>
</td>
<td align="center" width="75">
<a href="orderlist.aspx" class="sitelinkbold"><img src=http://www.163design.net/n/a/"images/account.gif" border="0">
account</a>
</td>
<td align="center" width="55">
<a href="shoppingcart.aspx" class="sitelinkbold"><img src=http://www.163design.net/n/a/"images/cart.gif" border="0">
cart</a>
</td>
<td align="center" width="65">
<a href="instantorder.asmx" class="sitelinkbold"><img src=http://www.163design.net/n/a/"images/services.gif" border="0">
services</a>
</td>
<tr>
</table>
</td>
<td width="10">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" nowrap>
<form method="post" action="searchresults.aspx" id="frmsearch" name="frmsearch">
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tr bgcolor="#9d0000">
<td background="images/modernliving_bkgrd.gif">
<img align="left" src=http://www.163design.net/n/a/"images/modernliving.gif">
</td>
<td width="94" align="right" bgcolor="#9d0000">
<img src=http://www.163design.net/n/a/"images/search.gif">
</td>
<td width="120" align="right" bgcolor="#9d0000">
<input type="text" name="txtsearch" id="txtsearch" size="20">
</td>
<td align="left" bgcolor="#9d0000">
<input type="image" src=http://www.163design.net/n/a/"images/arrowbutton.gif" border="0" id="image1" name="image1">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
这里没什么可说的,大家都非常熟.
第二个例子(和上一个当然不一样了!):
menu.ascx
<%@ control language="c#" %>
<%@ import namespace="system.data.sqlclient" %>
<script runat="server">
//*******************************************************
//
// the page_load event on this page is used to obtain
// from a database a list of all product categories
// and databind it to an asp:datalist control.
//
// to optimize performance, this user control is output
// cached (varying based on the categoryid and selection
// passed through the querystring.
//
//*******************************************************
void page_load(object sender, eventargs e) {
// set the curent selection of list
string selectionid = request.params["selection"];
if (selectionid != null) {
mylist.selectedindex = int32.parse(selectionid);
}
// obtain list of menu categories and databind to list control
ibuyspy.productsdb products = new ibuyspy.productsdb();
mylist.datasource = products.getproductcategories();
mylist.databind();
}
</script>
<table cellspacing="0" cellpadding="0" width="145" border="0">
<tr valign="top">
<td colspan="2">
<a href="default.aspx"><img src=http://www.163design.net/n/a/"images/logo.gif" border="0"></a>
</td>
</tr>
<tr valign="top">
<td colspan="2">
<asp:datalist id="mylist" runat="server" cellpadding="3" cellspacing="0" width="145" selecteditemstyle-backcolor="dimgray" enableviewstate="false">
<itemtemplate>
<asp:hyperlink class="menuunselected" id="hyperlink1" text='<%# databinder.eval(container.dataitem, "categoryname") %>' navigateurl='<%# "productslist.aspx?categoryid=" + databinder.eval(container.dataitem, "categoryid") + "&selection=" + container.itemindex %>' runat="server" />
</itemtemplate>
<selecteditemtemplate>
<asp:hyperlink class="menuselected" id="hyperlink2" text='<%# databinder.eval(container.dataitem, "categoryname") %>' navigateurl='<%# "productslist.aspx?categoryid=" + databinder.eval(container.dataitem, "categoryid") + "&selection=" + container.itemindex %>' runat="server" />
</selecteditemtemplate>
</asp:datalist>
</td>
</tr>
<tr>
<td width="10">
</td>
<td>
<br><br><br><br><br><br>
<a href="docs/docs.htm" target="_blank" class="sitelink">ibuyspy store<br>documentation</a>
</td>
</tr>
</table>
三:我们建好了两个.ascx文件,也就我们自己的控件,那怎么用呢?
看下面:
default.aspx
<%@ page language="c#" %>
<%@ register tagprefix="ibuyspy" tagname="menu" src="_menu.ascx" %>
<%@ register tagprefix="ibuyspy" tagname="header" src="_header.ascx" %>
<script runat="server">
//*******************************************************
//
// the page_load event on this page is used to personalize
// the welcome message seen by returning ibuyspy users.
// it does this by retrieving a client-side cookie
// (persisted on the client in the login.aspx and
// register.aspx pages) and updating a label control.
//
//*******************************************************
void page_load(object sender, eventargs e) {
// customize welcome message if personalization cookie is present
if (request.cookies["ibuyspy_fullname"] != null) {
welcomemsg.text = "welcome " + request.cookies["ibuyspy_fullname"].value;
}
}
</script>
<html>
<head>
<link rel="stylesheet" type="text/css" href="ibuyspy.css">
</head>
<body background="images/sitebkgrdnogray.gif" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tr>
<td colspan="2">
<ibuyspy:header id="header1" runat="server" />
</td>
</tr>
<tr>
<td valign="top" width=145>
<ibuyspy:menu id="menu1" runat="server" />
<img height="1" src=http://www.163design.net/n/a/"images/1x1.gif" width="145">
</td>
<td align="left" valign="top" width="*" nowrap>
<table height="100%" align="left" cellspacing="0" cellpadding="0" width="100%" border="0">
<tr valign="top">
<td nowrap>
<br>
<img align="left" width="24" src=http://www.163design.net/n/a/"images/1x1.gif">
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td class="homehead">
<asp:label id="welcomemsg" runat="server">welcome to ibuyspy.com</asp:label>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
本鸟是边学边看,不足之处请大虾指点([email protected]),谢谢!!
cnet
2000.7.30